Stateful Vs Stateless Widget
I. In Brief:
Stateless: Parent widget help to set Children State (Raised Button, Text and Icon)
Stateful: Not regulated by parent, regulated itself by watching for any changes and updating the state (setState()).
II. Stateless:
Both are stateless. Stateful is associated with a state and is immutable (stateless).
Parent's Widget Relationship with Child's Widget
The parent widget is stateless because it does not care about its child's state. The stateful child itself (or technically Flutter) will take care of its own state.
III. Type of Widget:
Stateless: Raised Button, Text and Icon
Stateful: User interaction; form, slider & widget that changes over time, data feed causes the UI to update; Checkbox, Radio, Slider, InkWell, Form, and TextField
IV. Re-render:
Stateless: Depend on external data
Stateful: depend on internal state and re-render when input data or Widget's state changes.
V. Lifecycle
Stateful:Recycle Widget but not State (Why? More economical)(Why separate? State does not have getters and other methods)
*createState(): Mandatory, this expression is usually suffice.
build(): called often. Return widget
didUpdateWidget(): parent Widget changes
setState(): inform data change. Not async
deactivate() :State remove from Tree (but can be reinserted)
dispose(): State object is removed
mounted == false
mounted == true: BuildContext is the place in the widget tree, assigned to state after create state. bool this.mounted = true. else setState will cause error.
*initState(): initState() {super.initState()}, first called (once and only) when method is c8
- Initialize data that relies on the specific BuildContext for the created instance of the widget.
- Initialize properties that rely on this widgets 'parent' in the tree.
- Subscribe to Streams, ChangeNotifiers, or any other object that could change the data on this widget.
didChangeDependencies(): called after initState() when Widget is built/when object with data this widget depends on. Rarely needed as build is called after
didUpdateWidget(): parent Widget changes
setState(): inform data change. Not async
deactivate() :State remove from Tree (but can be reinserted)
dispose(): State object is removed
mounted == false
Normally widgets have more constructor arguments, each of which corresponds to a property.
3rd Element: Inherited Widget
Comments
Post a Comment