What is the relation between stateful and stateless widgets in Flutter?
Asked Answered
P

12

155

A stateful widget is defined as any widget which changes its state within its lifetime. But it is a very common practice for a StatelessWidget to have a StatefulWidget as one of its children. Doesn't StatelessWidget become stateful if it has StatefulWidget as one of its children?

I tried looking into the documentation as part of the code of StatelessWidget, but couldn't figure out how a StatelessWidget can have Statefulwidget as its children and still remain StatelessWidget.

What is the relation and difference between stateful and stateless widgets in Flutter?

Phlogistic answered 26/11, 2017 at 22:32 Comment(2)
You can compose your layout from different types of widgets, however that does not mean you are inheriting the characteristics of the composition to affect each widget. What I mean is you can have a Container that is stateless that has a child of another Container that is declared as StatefulWidget somewhere else, the state of the container will only affect this one component only. So, it is all about having a composition from different types of widgets, each function as you need them to.Epochal
To mess things even more, there's a 3rd type of widget : InheritedWidget ; Which can make StatelessWidget update.Loudish
A
136

A StatelessWidget will never rebuild by itself (but can from external events). A StatefulWidget can. That is the golden rule.

BUT any kind of widget can be repainted any times.

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. It doesn't e.g. lock the widget tree.

But you shouldn't care about what's the type of your children. It doesn't have any impact on you.

Anklebone answered 26/11, 2017 at 23:38 Comment(8)
(Relatively new to the framework). What is the difference between rebuild and repaintPhlogistic
Also from comments in flutter framework code, apparently StateFulWidgets are immutable too.Phlogistic
The build of a widget is basically a call to "build" method, followed by creating/updating the corresponding renderbox ; which is followed by the painting process. Which will print these renderboxes on screen.Loudish
classes that inherit "StatefulWidget" are immutable. But the state (State<YourWidget>) itself is mutable.Loudish
I note my stateless widget does get rebuilt, but I'm wondering if it's because it references a stateful widget ?Woodcock
@Woodcock It probably depends on an InheritedWidget that got updated.Loudish
@RémiRousselet There is a question I ask myself. In case you have a stateful widget that calls another stateful widget is right? We consider that they are both dynamic widgets. If I put two statefullwidgets one inside the other, the didChangeDependencies () would also be called up several times, so I could lose some controlBooth
@RémiRousselet Stateful and stateless widgets both rebuild every frame, according to flutter.dev/docs/get-started/flutter-for/…Septuor
M
32

From the documentation at flutter.io:

...The important thing to note here is at the core both Stateless and Stateful widgets behave the same. They rebuild every frame, the difference is the StatefulWidget has a State object which stores state data across frames and restores it.

If you are in doubt, then always remember this rule: If a widget changes (the user interacts with it, for example) it’s stateful. However, if a child is reacting to change, the containing parent can still be a Stateless widget if the parent doesn’t react to change.

Milford answered 20/3, 2018 at 5:48 Comment(0)
P
17

As mention in flutter docs

What’s the point?

Some widgets are stateful, and some are stateless. If a widget changes—the user interacts with it, for example—it’s stateful. A widget’s state consists of values that can change, like a slider’s current value or whether a checkbox is checked. A widget’s state is stored in a State object, separating the widget’s state from its appearance. When the widget’s state changes, the state object calls setState(), telling the framework to redraw the widget.

A stateless widget has no internal state to manage. Icon, IconButton, and Text are examples of stateless widgets, which subclass StatelessWidget.

A stateful widget is dynamic. The user can interact with a stateful widget (by typing into a form, or moving a slider, for example), or it changes over time (perhaps a data feed causes the UI to update). Checkbox, Radio, Slider, InkWell, Form, and TextField are examples of stateful widgets, which subclass StatefulWidget.

https://flutter.io/tutorials/interactive/#stateful-stateless

Projector answered 22/9, 2018 at 12:59 Comment(0)
R
16

I can think of a very simple analogy. You have some piece of furniture with books, decorations, and a TV. The furniture is stateless, it does nothing doesn't move. In the TV, on the other side, you can turn it on, off, change channel, play a movie if it has some DVD attached, etc. The TV has a internal state which affects the way it behaves. In the furniture you have no state. The presence of the TV in the furniture is not adding a state to it. Hope this helps.

Rouse answered 20/2, 2020 at 2:20 Comment(2)
This doesn't answer the asker's specific question.Proclamation
This is a great analogy!Hearsay
P
13

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. For Example. Text, Icon, RaisedButton are Stateless Widgets.

Stateful Widgets are dynamic widgets, they can be updated during runtime based on user action or data change. If a Widget can change its state during run time it will be stateful widget.

Edit 15/11/2018

Stateless Widgets can re-render if the input/external data changed (external data being data that is passed through the constructor). Because 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.

Whereas Stateful Widgets have an internal state and can re-render if the input data changes or if Widget's state changes.

Both stateless and stateful widgets have different lifecycle.

Pablo answered 9/10, 2018 at 4:35 Comment(2)
Even after passing a new data from outside to Stateless widget we can change it too in run time but it is not called Stateful widget (in contrast to your last line).Driscoll
Can you please explain how a Stateless widget can "be updated when external data changes"? (With "external data being data that is passed through the constructor".) Will the constructor not only be called once ever? How data passed through the constructor change?Marucci
S
11

State is information that (1) can be read synchronously when the widget is built and (2) might change during the lifetime of the widget. It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes, using State.setState.

StatefulWidget:

A stateful widget is a widget that describes part of the user interface by building a constellation of other widgets that describe the user interface more concretely. The building process continues recursively until the description of the user interface is fully concrete (e.g., consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).

Stateful widget are useful when the part of the user interface you are describing can change dynamically, e.g. due to having an internal clock-driven state, or depending on some system state. For compositions that depend only on the configuration information in the object itself and the BuildContext in which the widget is inflated, consider using StatelessWidget.

StatefulWidget instances themselves are immutable and store their mutable state either in separate State objects that are created by the createState method, or in objects to which that State subscribes, for example Stream or ChangeNotifier objects, to which references are stored in final fields on the StatefulWidget itself.

StatelessWidget:

A stateless widget is a widget that describes part of the user interface by building a constellation of other widgets that describe the user interface more concretely. The building process continues recursively until the description of the user interface is fully concrete (e.g., consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).

Stateless widget are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the widget is inflated. For compositions that can change dynamically, e.g. due to having an internal clock-driven state, or depending on some system state, consider using StatefulWidget.

Sartain answered 1/3, 2018 at 17:51 Comment(0)
A
9

Answer for the Stack Overflow question - statefulness vs statelessness.

In Flutter, the difference is that stateless widgets can be defined by all the constructor arguments alone. If you create two stateless widgets using the same arguments, then they will be the same.

A stateful widget, however, is not necessarily the same as another built with the same constructor arguments. It might be in a different state.
Actually, a stateful widget is immutable (stateless) itself, but Flutter manages a separate state object and associates that with the widget, as explained in the StatefulWidget doc. This means that when Flutter rebuilds a stateful widget, it will check to see if it should reuse a previous state object and will, if desired, attach that state object to the 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.
On a high level, I agree that this makes the parent widget stateful, because two parents might contain two childs with different states and thus be technically different themselves. But from the viewpoint of Flutter, it builds the parent widget without caring about the state and only when building the child will consider its statefullness.

Andri answered 5/12, 2017 at 12:13 Comment(0)
S
8

Stateless : Widget state creates ONLY ONCE, then it can update values but not state explicitly. This is clear from there structure as well. That's why it has only one class which extends with StatelessWidget. So if I say, they can never re-run build() method again.

Stateful : Widgets can update their STATE (locally) & values multiple times upon event triggered. That's the reason, the implementation is also different. In this, we have 2 classes, one is StatefulWidget & the other is it's State implementation handler i.e. State<YourWidget>. So if I say, they can re-run build() method again & again based on events triggered.

Below diagram will help.

enter image description here

Skirting answered 22/12, 2019 at 1:50 Comment(0)
K
6

What are Stateful and Stateless widgets?

TL;DR: A widget that allows you to refresh the screen is a Stateful widget. A widget that does not is Stateless.

In more detail, a dynamic widget with content that can change should be a Stateful widget. A Stateless widget can only change content when the parameters are changed and hence needs to be done above the point of its location in the widget hierarchy. A screen or widget containing static content should be a stateless widget but to change the content, needs to be stateful.

I found this relative content on an interesting medium story. You're welcome!

Kwangchowan answered 9/5, 2020 at 11:2 Comment(0)
S
3

disclaimer :- started working on flutter from last week :)

stateless and statefull widget has his own lifecycle to create and update the UI. however you can use either stateless or statefull to render UI but practically statefull are more handy when ui is totally or partially dependent with the external data (like - rendering a list using api) whereas using stateless widget to render static ui like any input screen is a good practice.

Selmner answered 24/2, 2020 at 3:54 Comment(1)
I think the author meant the opposite :·DLabiovelar
M
3

In simple words:

As we know every widget is a view in the flutter. Which has its own classes. When we use those classes we create an object of it. We give values to their different variables/properties. Ex. We are creating Text widget so we can give it String, Color, Font Size, Font family. So by giving this, we are defining its properties while creating it. Up to this point, Stateless or Stateful widgets are the same but,

When we want to change/update its properties(let's say String or Color) again and again afterward then it should Stateful widget.

And when we don't want to change its properties after defining the first time it is a stateless widget.

that means we care about data that widget holds/control/show.

So Stateless is data less and Stateful is data full.

Now if you define a class that is stateless that means this class doesn't care/have variables in it or say data in its own class i.e. class level but it could have another widget/class in it which cares about data i.e. it is Stateful. So it does not have any impact on each other.

Please correct me if I am wrong here.

Montano answered 15/4, 2020 at 5:34 Comment(0)
S
1

What are Stateful and Stateless widgets?

Stateless Widget : Stateless widget are builds only when it is of parent changes.

Stateful Widgets : State full widgets holds state of the widget and can be rebuild when the state change.

Sheffield answered 17/7, 2020 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.