위젯을 위로 쌓기
Coddy Flutter 여정의 화면 크기에 맞는 레이아웃 섹션에 포함된 레슨. 30개 중 10번째.
Column은 자식들을 위아래로 배치하고, Row는 나란히 배치합니다. Stack은 책상 위에 종이를 쌓듯이 자식들을 서로 겹쳐 배치합니다.
Stack(
children: [
Container(height: 200, color: Colors.grey),
const Text('On top'),
],
)첫 번째 자식이 먼저 그려져 아래에 놓입니다. 그다음 자식은 앞에 있는 자식들 위에 그려집니다. 따라서 사진이 먼저 놓이고, 사진 위에 표시되는 레이블이 마지막에 놓입니다.
자식들은 Stack의 왼쪽 위 모서리에서 시작하며, Stack의 크기는 가장 큰 자식의 크기와 같습니다. 배지, 레이블 또는 재생 버튼이 사진 위에 표시되는 방식이 바로 이렇습니다.
챌린지
쉬움SALE 배지가 사진 아래에 있습니다. 배지를 사진 위에 놓으세요. 두 요소를 담고 있는 Column을 Stack으로 바꾸고, 배지가 앞에 그려지도록 하세요.
직접 해보기
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('Shop')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Column(
children: [
Container(
height: 200,
color: Colors.blueGrey.shade100,
child: const Center(child: Icon(Icons.image, size: 64)),
),
Container(
padding: const EdgeInsets.all(8),
color: Colors.red,
child: const Text('SALE', style: TextStyle(color: Colors.white)),
),
],
),
const SizedBox(height: 12),
const Text('Running shoes', style: TextStyle(fontSize: 20)),
],
),
),
),
);
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
화면 크기에 맞는 레이아웃의 모든 레슨
직접 연습해 보세요: 온라인 Flutter 컴파일러