Menu
CoddyTech

Flexible이 자식을 감싸요

Coddy Flutter 여정의 화면 크기에 맞는 레이아웃 섹션에 포함된 레슨. 30개 중 3번째.

Expanded는 자식이 더 적은 공간만 필요로 하더라도 남은 공간을 채우게 합니다. Expanded로 감싼 채팅 말풍선은 한 단어로 된 메시지에도 행 전체에 걸쳐 늘어납니다.

Flexible은 자식에게 많아야 같은 공간을 제공하고, 더 작게 유지할 수 있게 합니다.

Row(
  children: [
    const CircleAvatar(child: Text('A')),
    Flexible(
      child: Container(
        color: Colors.grey.shade300,
        child: const Text('Hello'),
      ),
    ),
  ],
)
Flexible은 남은 공간보다 더 많은 공간을 차지하도록 허용하지 않기 때문입니다.

자식이 공간을 채워야 할 때는 Expanded를 사용하고, 공간을 채울 수도 있을 때는 Flexible을 사용하세요.

challenge icon

챌린지

쉬움

Hi! 말풍선이 화면 전체 너비로 늘어납니다. 각 말풍선의 너비를 메시지에 맞추고, 긴 메시지는 화면 안에서 계속 줄바꿈되도록 하세요.

직접 해보기

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Chat')),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: [
              Row(
                children: [
                  const CircleAvatar(child: Text('S')),
                  const SizedBox(width: 8),
                  Expanded(
                    child: Container(
                      padding: const EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: Colors.grey.shade300,
                        borderRadius: BorderRadius.circular(16),
                      ),
                      child: const Text('Hi!'),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 12),
              Row(
                children: [
                  const CircleAvatar(child: Text('S')),
                  const SizedBox(width: 8),
                  Expanded(
                    child: Container(
                      padding: const EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: Colors.grey.shade300,
                        borderRadius: BorderRadius.circular(16),
                      ),
                      child: const Text(
                        'Are you coming to the park later? We meet at the big tree by the lake.',
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

화면 크기에 맞는 레이아웃의 모든 레슨

직접 연습해 보세요: 온라인 Flutter 컴파일러