There are mainly two types of widgets in Flutter. So, in this article, we will see what is the lifecycle of Flutter Widgets.
What is the lifecycle of Flutter Widgets?
The widgets which remain constant throughout the lifetime of the app are called stateless widgets. We use them when we want structures to stay the same all over the app. These widgets are immutable, i.e. they cannot be changed. Here Hot Reload can reflect the changes made in the app structure and can be used for verification.
Stateless widgets are just like a single block widget that cannot be planned. They can only be destroyed to create a new one with another set of widget configurations and properties. The life cycle of stateless widgets is simple; there’s only one stage: the build method. As soon as the widget gets built, the build method gets automatically called. You can create whatever look you want to add up in your application.
Stateful widgets are used when we want objects to be updated on the screen. For example: when a user presses a button to trigger an action ( basically- User Interaction). These widgets are mutable, i.e. they can be manipulated as per the requirements. Here, the Hot Reload cannot reflect the changes made; therefore, to serve the purpose, a hot restart is used.
Since stateless widgets can be tracked for their properties and configurations using the state object, we can manage all these variables using the setState() method. The following are the main stages in the lifecycle of a stateful widget:
- init State (): The init State gets triggered implicitly as soon as the State initially gets initialized. It is used when we want something to happen the moment our stateful widget is created.
- build (): The build method gets triggered when the widgets are constructed and appear on the screen. It is used when we want something to happen every single time when our stateful widget gets rebuilt.
- deactivate(): Deactivate method gets called when the stateful widget gets destroyed ( just like destructor). It is used when we want something to happen just before our stateful widget gets destroyed.
Thanks for being with us on a Flutter Journey!
So, in this article, we have seen what is the lifecycle of Flutter Widgets. Also, feel free to comment and provide any other suggestions regarding Flutter.
Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. Also, the portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.