StatelessWidget은 상태를 가지지 않기 때문에 생명주기 메서드가 따로 없고, 화면에 그려질 때 호출되는 build() 메서드만 존재한다.
반면, StatefulWidget은 상태 변경에 따른 다양한 생명주기 메서드를 제공한다.

Flutter에서 Lifecycle(생명주기)는
👉 위젯이 생성되고, 화면에 그려지고, 업데이트되고, 제거될 때까지의 흐름을 의미함
특히 StatefulWidget에서 매우 중요
createState()
↓
initState()
↓
didChangeDependencies()
↓
build()
↓
(상태 변경 시)
↓
setState()
↓
build()
↓
didUpdateWidget()
↓
deactivate()
↓
dispose()
State<MyWidget> createState() => _MyWidgetState();
void initState() {
super.initState();
}
👉 사용 예
void didChangeDependencies() {
super.didChangeDependencies();
}
InheritedWidget 변화 감지👉 예:
Widget build(BuildContext context) {
return Container();
}
👉 호출되는 경우
setState(() {
// 상태 변경
});
void didUpdateWidget(covariant MyWidget oldWidget) {
super.didUpdateWidget(oldWidget);
}
👉 사용 예
void deactivate() {
super.deactivate();
}
👉 특징
void dispose() {
super.dispose();
}
👉 사용 예
생성
↓
initState
↓
didChangeDependencies
↓
build
↓
(상태 변화)
↓
setState → build
↓
(부모 변경)
↓
didUpdateWidget → build
↓
제거
↓
deactivate
↓
dispose
| 구분 | initState | build |
|---|---|---|
| 호출 횟수 | 1번 | 여러 번 |
| 용도 | 초기화 | UI 렌더링 |
| API 호출 | ✅ 가능 | ❌ 비추천 |
👉 메모리 누수 방지 핵심
void dispose() {
controller.dispose();
super.dispose();
}
👉 init → build → update → dispose 흐름으로 동작
setState() → build() 재호출 확인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: LifecycleExample(),
);
}
}
class LifecycleExample extends StatefulWidget {
const LifecycleExample({super.key});
State<LifecycleExample> createState() => _LifecycleExampleState();
}
class _LifecycleExampleState extends State<LifecycleExample> {
int count = 0;
void initState() {
super.initState();
print("1️⃣ initState 호출");
}
void didChangeDependencies() {
super.didChangeDependencies();
print("2️⃣ didChangeDependencies 호출");
}
void didUpdateWidget(covariant LifecycleExample oldWidget) {
super.didUpdateWidget(oldWidget);
print("🔄 didUpdateWidget 호출");
}
void deactivate() {
super.deactivate();
print("⚠️ deactivate 호출");
}
void dispose() {
print("❌ dispose 호출");
super.dispose();
}
void _increase() {
setState(() {
count++;
print("👉 setState 호출 (count: $count)");
});
}
Widget build(BuildContext context) {
print("3️⃣ build 호출");
return Scaffold(
appBar: AppBar(title: const Text("Lifecycle Example")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Count: $count", style: const TextStyle(fontSize: 24)),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _increase,
child: const Text("증가"),
),
],
),
),
);
}
}
1️⃣ initState 호출
2️⃣ didChangeDependencies 호출
3️⃣ build 호출
👉 setState 호출 (count: 1)
3️⃣ build 호출
👉 핵심: setState → build 다시 실행
⚠️ deactivate 호출
❌ dispose 호출
initState() 👉 한 번만 실행 (초기화)build() 👉 계속 호출됨 (UI 그림)setState() 👉 상태 변경 + build 재실행dispose() 👉 메모리 정리 (필수)👉 버튼 클릭 → setState → build 재실행 → UI 변경
<import 'package:flutter/material.dart';
import 'life_cicle.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return const MaterialApp(
home: MediaQueryExample(),
);
}
}
class MediaQueryExample extends StatelessWidget {
const MediaQueryExample({super.key});
Widget build(BuildContext context) {
// 📌 MediaQuery 사용
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
final isLandscape = width > height;
return Scaffold(
appBar: AppBar(title: const Text("MediaQuery Example")),
body: Column(
children: [
// 📊 화면 정보 표시
Text("width: $width"),
Text("height: $height"),
Text("가로 모드: $isLandscape"),
const SizedBox(height: 20),
// 📌 반응형 박스
Container(
width: width * 0.8,
height: 100,
color: Colors.blue,
child: const Center(
child: Text("화면 너비의 80%"),
),
),
const SizedBox(height: 20),
// 📌 버튼 → Navigator (context 사용)
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondPage(),
),
);
},
child: const Text("다음 화면"),
),
],
),
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
Widget build(BuildContext context) {
final padding = MediaQuery.of(context).padding;
return Scaffold(
appBar: AppBar(title: const Text("Second Page")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("상태바 높이: ${padding.top}"),
const SizedBox(height: 20),
// Text(
// "헤드라인",
// style: Theme.of(context)
// .textTheme
// .headlineMedium,
// ),
//
// const SizedBox(height: 20),
//
// Text(
// "본문 내용입니다.",
// style: Theme.of(context).textTheme.bodyMedium,
// ),
//
//
// Text(
// "작은 설명",
// style: Theme.of(context).textTheme.bodySmall,
// ),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("뒤로가기"),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const LifecycleExample(),
),
);
},
child: const Text("Life 사이클 페이지"),
),
],
),
),
);
}
}
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: LifecycleExample(),
);
}
}
class LifecycleExample extends StatefulWidget {
const LifecycleExample({super.key});
State<LifecycleExample> createState() => _LifecycleExampleState();
}
class _LifecycleExampleState extends State<LifecycleExample> {
int count = 0;
void initState() {
super.initState();
print("1️⃣ initState 호출");
}
void didChangeDependencies() {
super.didChangeDependencies();
print("2️⃣ didChangeDependencies 호출");
}
void didUpdateWidget(covariant LifecycleExample oldWidget) {
super.didUpdateWidget(oldWidget);
print("🔄 didUpdateWidget 호출");
}
void deactivate() {
super.deactivate();
print("⚠️ deactivate 호출");
}
void dispose() {
print("❌ dispose 호출");
super.dispose();
}
void _increase() {
setState(() {
count++;
print("👉 setState 호출 (count: $count)");
});
}
Widget build(BuildContext context) {
print("3️⃣ build 호출");
return Scaffold(
appBar: AppBar(title: const Text("Lifecycle Example")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Count: $count", style: const TextStyle(fontSize: 24)),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _increase,
child: const Text("증가"),
),
],
),
),
);
}
}