👉 쉽게 말하면
포토샵 레이어처럼 위에 겹쳐 올림
Stack(
children: [
Container(width: 200, height: 200, color: Colors.red),
Container(width: 150, height: 150, color: Colors.green),
Container(width: 100, height: 100, color: Colors.blue),
],
)
👉 결과:
👉 위치 기준:
Stack(
children: [
Container(width: 200, height: 200, color: Colors.grey),
Positioned(
top: 10,
left: 10,
child: Container(width: 50, height: 50, color: Colors.red),
),
Positioned(
bottom: 10,
right: 10,
child: Container(width: 50, height: 50, color: Colors.blue),
),
],
)
👉 결과:
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: StackExample(),
);
}
}
class StackExample extends StatelessWidget {
const StackExample({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Stack Example")),
body: Center(
child: Stack(
children: [
// 배경 이미지
Image.network(
"https://picsum.photos/300",
width: 300,
height: 200,
fit: BoxFit.cover,
),
// 텍스트 오버레이
Positioned(
bottom: 10,
left: 10,
child: Container(
color: Colors.black54,
padding: const EdgeInsets.all(8),
child: const Text(
"배너 텍스트",
style: TextStyle(color: Colors.white),
),
),
),
],
),
),
);
}
}
Stack(
alignment: Alignment.center,
children: [...],
)
👉 기본 위치를 중앙으로 설정 가능
| 항목 | 설명 |
|---|---|
| Stack | 겹치기 |
| Positioned | 위치 지정 |
| 순서 | 나중에 쓰면 위로 올라감 |
Positioned(...) // ❌ 에러
👉 반드시 Stack 안에서만 사용 가능
👉 부모 크기 없으면 위치 이상해짐
👉 Stack은 겹치고, Positioned는 위치 잡는다