Container(
width: 100,
height: 100,
color: Colors.blue,
child: const Center(
child: Text("Box"),
),
)
👉 파란색 100x100 박스 생성
| 속성 | 설명 |
|---|---|
| width / height | 크기 |
| color | 배경색 |
| padding | 내부 여백 |
| margin | 외부 여백 |
| alignment | 내부 정렬 |
Column(
children: [
Container(height: 50, color: Colors.red),
Container(height: 50, color: Colors.green),
Container(height: 50, color: Colors.blue),
],
)
👉 위에서 아래로 순서대로 배치됨
Column(
mainAxisAlignment: MainAxisAlignment.center, // 세로 정렬
crossAxisAlignment: CrossAxisAlignment.center, // 가로 정렬
children: [...],
)
Column 또는 Row 안에서 사용👉 "남은 공간을 나눠서 가져간다"
Column(
children: [
Container(height: 100, color: Colors.red),
Expanded(
child: Container(color: Colors.green),
),
],
)
👉 빨간색 100px
👉 나머지는 초록색이 전부 차지
Column(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red),
),
Expanded(
flex: 2,
child: Container(color: Colors.blue),
),
],
)
👉 화면을 1:2 비율로 나눔
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return const MaterialApp(
home: LayoutExample(),
);
}
}
class LayoutExample extends StatelessWidget {
const LayoutExample({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Layout Example")),
body: Column(
children: [
// 상단 고정 영역
Container(
height: 100,
color: Colors.red,
child: const Center(child: Text("Header")),
),
// 중간 확장 영역
Expanded(
child: Container(
color: Colors.green,
child: const Center(child: Text("Content")),
),
),
// 하단 고정 영역
Container(
height: 80,
color: Colors.blue,
child: const Center(child: Text("Footer")),
),
],
),
);
}
}
[ Header (100px) ]
[ Content (남은 전체) ]
[ Footer (80px) ]
| 위젯 | 역할 |
|---|---|
| Container | 박스 (디자인/레이아웃) |
| Column | 세로 정렬 |
| Expanded | 남은 공간 채우기 |
Expanded(
child: Container(),
)
👉 에러 발생
Expanded(
child: Container(height: 100),
)
👉 의미 없음 (Expanded가 우선)
👉 Column은 쌓고, Container는 꾸미고, Expanded는 채운다
Column(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red),
),
Expanded(
flex: 2,
child: Container(color: Colors.green),
),
Flexible(
flex: 1,
child: Container(color: Colors.blue),
),
],
)
👉 비율:
빨강 : 초록 : 파랑 = 1 : 2 : 1