Difference Between Stateless and Stateful Widget in Flutter


Introduction

In this article, I will show you what is the difference between Statefull and Stateless Widget.

As you know in Flutter all the UI components are known as widgets.  The widget which contains the code for a single screen of the app can   be just of two types —



                  1, Stateless Widget
                  2, Statefull Widget

       State:

Before knowing the difference between the stateless and stateful widget we should take a look at the definition of State of a widget.

The State is information that can read synchronously when the widget is build and might change during the lifetime of the widget.

In other words, the state of the widget is the data of the objects that its properties (parameters) are sustaining at the time of its creation (when the widget is painted on the screen). The state can also change when it is used for example when a CheckBox widget is clicked a check appears on the box.


Stateless Widget:

Stateless widgets are the widgets that don’t change i.e. they are immutable. Its appearance and properties remain unchanged throughout the lifetime of the widget. In simple words, Stateless widgets cannot change their state during the runtime of the app, which means the widgets cannot be redrawn while the app is in action.

In simple words, Stateless widgets cannot change their state during the runtime of the app, which means the widgets cannot be redrawn while the app is in action.


Examples: Icon, IconButton, and Text are examples of stateless widgets.
To create a Stateless widget, we have to override the build() method as implemented in the code below.

The structure of a Stateless widget looks like this:
import 'package:flutter/material.dart';
void main(){
runApp(const myapp());
}
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(

title: "hello flutter",
home: Material(
color: Colors.lightBlueAccent,
child: Center(child: Text("hello flutter")),

),
);
}
}


So, let’s understand what is there in this small code snippet.

The name of this Stateless Widget is “myapp”, inside which we have to override the “build” method. This build method takes in a “BuildContext” as the parameter and returns a widget. That’s why you can see that the return type of the build method is a widget. And this the place where you can design the UI of this screen, which is Stateless.

In Stateless widget, The “build” method can be called only ONCE while the app is in action, which is responsible for drawing the widgets on to the device screen.


If you want to redraw the Stateless Widget, then you will need to create a new instance of the widget.


Stateful

Stateful widgets have a mutable state, i.e., they are mutable and can be drawn multiple times within its lifetime.

They are the widgets which can change their state multiple times and can be redrawn on to the screen any number of times while the app is in action.

The structure of a Statefull widget looks like this:

import 'package:flutter/material.dart';
void main(){
runApp(StartScreen());
}
class StartScreen extends StatefulWidget {
const StartScreen({Key? key}) : super(key: key);

@override
_StartScreenState createState() => _StartScreenState();
}

class _StartScreenState extends State<StartScreen> {
@override
Widget build(BuildContext context) {
return Container();
}
}


The name of the widget is  “StartScreen”, but now it overrides the “createState” method, instead of the “build” method, which returns the instance of the class “_StartScreenState”.

The class “_StartScreenState” extends from State<> which takes “StartScreen” as a template input.

Now, this “_StartScreenState” overrides the “build” method and returns a widget. This is where you can define the UI of the app, which is Stateful. As it is a Stateful widget you can call the build method any number of times, which will redraw the widgets on the screen.

So, how can you call the build method?

It’s really easy, you can use the “setState” method to call the build method, which will, in turn, redraw the widgets. This is the most important method you will need to use with any Stateful widget, to really use the statefulness of the widget.

examples can be CheckBoxRadioButtonFormTextField.



Differences Between Stateless and Stateful Widget: 

Stateless Widget:

  • Stateless Widgets are static widgets.
  • They do not depend on any data change or any behavior change.
  • Stateless Widgets do not have a state, they will be rendered once and will not update themselves, but will only be updated when external data changes.
  • For Example: Text, Icon, RaisedButton are Stateless Widgets. 
     

Stateful Widget:

  • Stateful Widgets are dynamic widgets.
  • They can be updated during runtime based on user action or data change.
  • Stateful Widgets have an internal state and can re-render if the input data changes or if Widget’s state changes.
  • For Example: Checkbox, Radio Button, Slider are Stateful Widgets






No comments

Powered by Blogger.