What is the relationship between state and stateless views in Flutter?

A state widget is defined as any widget that changes its state during its life cycle. But for StatelessWidget , StatefulWidget as one of his children is a very common practice. Can't a StatelessWidget be obsolete if it has a StatefulWidget as one of its children?

I tried to study the documentation as part of the StatelessWidget code, but couldn't understand how StatelessWidget can have Statefulwidget as its children and still remain StatelessWidget .

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

+22
source share
7 answers

StatelessWidget will never rebuild on its own (but can from external events). StatefulWidget can. This is the golden rule.

BUT any widget can be repainted at any time.

Only without state preservation means that all its properties are immutable and that the only way to change them is to create a new instance of this widget. For example, it does not block the widget tree.

But you should not care about the type of your children. It has no effect on you.

+21
source

A state is information that (1) can be read synchronously when building a widget and (2) can change during the life of the widget. Responsibility for the implementation of the widget is responsible for ensuring that the state is immediately notified when this state changes using State.setState.

StatefulWidget :

A state widget is a widget that describes part of the user interface, creating a constellation of other widgets that describe the user interface in more detail. The building process continues recursively until the user interface description is completely concrete (for example, it consists entirely of RenderObjectWidgets that describe specific RenderObjects).

A virtualized widget is useful when the part of the user interface that you describe can change dynamically, for example, due to the presence of an internal state controlled by a clock, or depending on the state of any system. For compositions that depend only on configuration information in the object itself and BuildContext, in which the widget is overpriced, consider using StatelessWidget.

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

StatelessWidget :

A stateless widget is a widget that describes part of the user interface, creating a constellation of other widgets that describe the user interface in more detail. The building process continues recursively until the user interface description is completely concrete (for example, it consists entirely of RenderObjectWidgets that describe specific RenderObjects).

Widgets without saving are useful when the part of the user interface that you describe does not depend on anything other than the configuration information of the object itself and the BuildContext object in which the widget is overpriced. For compositions that can dynamically change, for example, due to the presence of an internal state controlled by a clock, or depending on the state of any system, consider using StatefulWidget.

+8
source

From the documentation on flutter.io :

... It is important to note that at the core, both Stateless and Stateful widgets behave identically. They rebuild each frame, the difference in StatefulWidget has a State object that stores state data through frames and restores them.

If in doubt, then always remember this rule: if the widget changes (for example, the user interacts with it), its state. However, if the child responds to the change, the containing parent can still be stateless widgets if the parent does not respond to the change.

+7
source

fooobar.com/questions/18640 / ....

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

A state widget, however, is not necessarily the same as another one built with the same constructor arguments. It may be in a different condition.
In fact, a state widget is immutable (by itself), but Flutter manages a separate state object and associates it with widgets, as described in the StatefulWidget doc . This means that when Flutter restores a widget with a state, it checks whether it should reuse the previous state object and, if desired, attach this object to the widget.

The parent widget has no state because it does not care about its child state. An independent child himself (or technically Flutter) will take care of his own condition.
At a high level, I agree that this makes the parent widget wealthy, because two parents can contain two children with different conditions and, therefore, be technically different. But from the point of view of Flutter, he builds a parent widget without worrying about the state, and only when building a child will he take into account his condition.

+3
source

As mentioned in flutter docs

What's the point?

Some widgets have state status, and some are stateless. If the widget changes, the user interacts with it, for example, its state. The state of widgets consists of values ​​that can change, for example, the current value of the slider or the check box is selected. The state of widgets is stored in the State object, separating the state of widgets from its appearance. When the state of widgets changes, the state object calls setState (), telling the framework to redraw the widget.

A stateless widget does not have an internal state to control. Icon, IconButton, and text are examples of stateless widgets that are subclassed by StatelessWidget.

The state-enabled widget is dynamic. The user can interact with state widgets (for example, by entering a form or moving the slider) or change over time (perhaps the data channel causes the user interface to be updated). Checkbox, Radio, Slider, InkWell, Form, and TextField are examples of stateful widgets in the StatefulWidget subclass.

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

+1
source

Stateless widgets are static widgets. You just need to pass a few properties before initializing widgets without saving state. They are not affected by data changes or behavior. For instance. Text, Icon, RaisedButton are stateless widgets.

Stateful widgets are dynamic widgets that can be updated at run time depending on user actions or data changes. If the widget can change its state at run time, it will be a stateful widget.

Edit 11/15/2018

Stateless widgets can be re-rendered if the input / external data has changed (external data is data that is passed through the constructor). Since stateless widgets have no state, they will be displayed once and will not be updated independently, but will be updated only when external data changes.

Whereas Stateful Widgets have an internal state and can be re-displayed when the input changes or when the state of the Widget changes.

Both widgets without state and with state have a different life cycle.

+1
source

Source: https://habr.com/ru/post/1273673/


All Articles