Flutter : Stateful And Stateless widgets

This Article is posted by shubham.narkhede8 at 11/20/2019 4:07:20 AM



What is Stateless widgets and Stateful widgets??

1. Stateless Widgets:-

-> Stateless widget is simply which is never build by itseld. It is only simple part of viewing or displaying the widgets. 
-> Stateless widget that describes part of user interface. 
-> A stateless widget has no internal state to manage ex. Icon, IconButton, and Text.
-> Stateless Widgets are static widgets.You just need to pass few properties before initializing Stateless Widgets.They do not depend on any data change or any behavior change.
-> Stateless Widgets can re-render if the input/external data changed.
-> A single Stateless widget can builds in many different BuildContext.
-> The building of Stateless widget process continues recursively until the description of the user interface is fully concrete.
-> Stateless only means that all of its properties are immutable and that the only way to change them is to create a new instance of that widget.

2. Stateful Widgets:-

-> Stateful widgets are useful when the part of the user interface you are describing can change dynamically.
-> Stateful widgets are not static it is dynamic. If user wants to send any input then Stateful widgets are use.
-> THE MAIN AND IMPORTANT THING OF STATEFUL WIDGET IS IT HAS ITS OWN STATE.
     -> STATE - The data used by the widget might change.The data can't be read synchronously when the widget is built.
-> We can simply create a state in build method by createState().
-> A stateful widget is dynamic. The user can interact with a stateful widget,or it changes over time.
-> Ex. Checkbox, Radio, Slider, InkWell, Form
-> Stateful Widgets have an internal state and can re-render if the input data changes or if Widget's state changes.

LIFECYCLE:

1.createState()
-> When Flutter is instructed to build a StatefulWidget, it immediately calls createState(). This method must exist. A StatefulWidget rarely needs to be more complicated than this.

2.mounted == true 
-> All widgets have a bool this.mounted property. It is turns true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.

3.initState()
-> initState is called once and only once. It must also call super.initState().

4.didChangeDependencies()
-> The didChangeDependencies method is called immediately after initState on the first time the widget is built.It will also be called whenever an object that this widget depends on data from is called. For example, if it relies on an InheritedWidget, which updates.

5.build()
-> This method is called often. It is a required, @override and must return a Widget.

6.didUpdateWidget()
-> didUpdateWidget() is called if the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it's being rebuilt with the same runtimeType, then this method is called.

7.setState() 
-> setState() takes a callback which cannot be async. It is for this reason it can be called often as required, because repainting is cheap.

8.deactivate()
->'deactivate()' is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another.

9.dispose()
-> 'dispose()' is called when the State object is removed, which is permanent.

10.mounted == false
-> The state object can never remount, and an error is thrown is setState() is called.



Tags: flutter; dart; android; ios; appbar; flutterUi; Stateless, Stateful, widgets








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com