UI에 반영되는 대표 위젯 2가지가 있습니다.
class MyStatelessWidget extends StatelessWidget{
Widget build(BuildContext context) {
return Text('Hello, World');
}
}
class MyStatefullWidget extemds StatefullWidget{
_MyStatefullWidgetState createState() => _MyStatefullWidgetState();
}
class _MyStatefullWidgetState extends State<MyStatefullWidget> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
Widget build(BuildContext context){
return Column(
children:[
Text('Counter: $_counter'),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
);
}}
createState()
새로운 Stateful 위젯이 생성될 때 호출되며, 연결된 State의 인스턴스를 반환
initState()
위젯이 생성되며 처음으로 호출되는 메[]소드, 한번만 호출되고 내부적으로 super.initeState()를 꼭 호출해야함
didChangeDependencies()
initState() 다음에 호출되는 함수로, 상속된 위젯 또는 inheritedWidget을 상속하고 있을때 해당 값이 변경되는 경우 호출됨
build()
화면에 위젯들을 빌드할때 호출됨
setStete()
위젯 내부의 데이터가 변경되었으니 화면을 재 렌더링하기 위해서 호출됨
dispose()
State() 객체가 영구 제거됨
setState()를 호출할 시 build()를 호출하는데, 트리에 있는 모든 위젯이 다시 빌드가 됨
트리가 깊어지고 많아질수록 앱의 성능저하로 이루어짐 -> 효율적인 관리를 하기 위해서 상태 관리 패턴을 사용함