Stateful Widgets
Widget which can change also after the creation, allowing the User Interactivity.
To save state and respond to user interaction and events, we use StatefulWidget.
The State Object stores the WidgetState and can change throughout the widgets lifetime. The text is passed in during instantiation and is not changed.
The color which is in the state object can be changed.
Note * State object is _MyStatefulWidgetState class
our convention of using the underscore indicates that the class is private. Since the only widget that has access to it is the MyStatefulWidget class
For consistency we give the state objects the same name as the StatefulWidget's name.
Similarly, you can always override other methods in StatefulWidget such as dispose.
initState()
dispose()
or only setState() can be called
To save state and respond to user interaction and events, we use StatefulWidget.
Creating StatefulWidget
class MyStatefulWidget extends StatefulWidget{ final String text; MyStatefulWidget({ this.text, }); @override createState() => _MyStatefulWidgetState();}
class _MyStatefulWidgetState extends State<MyStatefulWidget>{ var color = Colors.purple; @override Widget build(BuildContext context) { // TODO: implement build return Container( child: Text(widget.text), color: color, ); } }While the StatefulWidget instance itself is also immutable, it create a state object using createState.
The State Object stores the WidgetState and can change throughout the widgets lifetime. The text is passed in during instantiation and is not changed.
The color which is in the state object can be changed.
Note * State object is _MyStatefulWidgetState class
our convention of using the underscore indicates that the class is private. Since the only widget that has access to it is the MyStatefulWidget class
For consistency we give the state objects the same name as the StatefulWidget's name.
State Object
It contains a set state function that you call each time you want to change any state.Similarly, you can always override other methods in StatefulWidget such as dispose.
initState()
dispose()
or only setState() can be called
Comments
Post a Comment