728x90
Flutter ๊ฐ๋ฐ์ ์์ํ๋ฉด์ ๊ฐ์ฅ ๋จผ์ ๋ง๋๊ฒ ๋๋ ๊ฒ ์ค ํ๋๊ฐ ๋ฐ๋ก StatefulWidget์ ๋๋ค. ๊ฐ๋จํ ์นด์ดํฐ ์ฑ์ ๋ง๋ค๋ฉด์ "์ ์ด๋ ๊ฒ ๋ณต์กํ๊ฒ ๋ ๊ฐ์ ํด๋์ค๋ก ๋๋์ด์ ธ ์์๊น?" ๊ถ๊ธํดํ์ ์ ์์ผ์ ๊ฐ์?
StatefulWidget์ด๋ ๋ฌด์์ธ๊ฐ?
StatefulWidget์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ํ๋ฅผ ๊ฐ์ง๋ ์์ ฏ์ ๋๋ค. ํ์ง๋ง ์ฌ๊ธฐ์ ์ค์ํ ํฌ์ธํธ๊ฐ ์์ต๋๋ค.
// StatefulWidget ํด๋์ค ์์ฒด๋ ๋ถ๋ณ(immutable)!
class StateCounterHomePage extends StatefulWidget {
const StateCounterHomePage({super.key});
@override
State<StateCounterHomePage> createState() => _StateCounterHomePageState();
}
// ์ค์ ์ํ๋ State ํด๋์ค๊ฐ ๊ด๋ฆฌ
class _StateCounterHomePageState extends State<StateCounterHomePage> {
int count = 0; // ์ด๊ฒ์ด ์ง์ง ์ํ!
@override
Widget build(BuildContext context) {
// UI ๊ตฌ์ฑ
}
}
StatefulWidget = ์์ ฏ ํฉํ ๋ฆฌ State = ์ค์ ์ํ + UI + ๋ก์ง
์ค์ ์ฝ๋๋ก ๋ถ์ํด๋ณด๊ธฐ
์นด์ดํฐ ์ฑ์ ํตํด StatefulWidget์ ๊ตฌ์กฐ๋ฅผ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
class _StateCounterHomePageState extends State<StateCounterHomePage> {
// ๋ฐ์ดํฐ (์ํ)
int count = 0;
@override
Widget build(BuildContext context) {
// ํ๋ฉด (UI)
return Scaffold(
appBar: AppBar(title: Text('State Counter App')),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
spacing: 10,
children: [
Text(
'Counter.count : ${count}',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
ElevatedButton(
// ๋ก์ง (์ด๋ฒคํธ ์ฒ๋ฆฌ)
onPressed: () {
setState(() {
count = count + 1;
});
},
child: Text('์นด์ดํธ ์ฆ๊ฐ'),
),
ElevatedButton(
onPressed: () {
setState(() {
count = count - 1;
});
},
child: Text('์นด์ดํธ ๊ฐ์'),
),
],
),
),
),
);
}
}
StatefulWidget์ ์ธ ๊ฐ์ง ์ฑ ์์ด ํ ๊ณณ์ ์์ต๋๋ค
- ๋ฐ์ดํฐ: int count = 0 - ์์ ฏ์ ์ํ
- ํ๋ฉด: build() ๋ฉ์๋ - UI ๊ตฌ์ฑ
- ๋ก์ง: onPressed ์ฝ๋ฐฑ - ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ฐ ์ํ ๋ณ๊ฒฝ
'๐ฅ Bread Basics > Flutter' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] Riverpod ์ํ๊ด๋ฆฌ:: ๊ตฌ์กฐํ๋ ์ ๊ทผ๋ฒ๊ณผ ํ์ ์์ ์ฑ (0) | 2025.06.27 |
---|---|
[Flutter] GetX ์ํ๊ด๋ฆฌ: ๋ฐ์ํ ํ๋ก๊ทธ๋๋ฐ (0) | 2025.06.27 |
[Flutter] Provider ์ํ๊ด๋ฆฌ:: ChangeNotifier๋ฅผ ๋ ์ฒด๊ณ์ ์ผ๋ก (0) | 2025.06.27 |
[Flutter] MVVM ChangeNotifier:: (ํ๋ฉด) / (๋ฐ์ดํฐ, ๋ก์ง) ๋ฐ๋ก (1) | 2025.06.27 |
Dart ์ธ์ด ๊ธฐ๋ณธ๊ธฐ ๋ฐฐ์ฐ๊ธฐ (0) | 2025.04.28 |
mixin, sealed, base ํด๋์ค (0) | 2025.04.25 |
Record(ํํ), Destructuring (0) | 2025.04.25 |
Future, async, await, stream, listen, sink, yield (0) | 2025.04.25 |