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

前端 未结 14 1816
悲&欢浪女
悲&欢浪女 2020-12-02 07:06

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 S

相关标签:
14条回答
  • 2020-12-02 07:44

    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.

    0 讨论(0)
  • 2020-12-02 07:46

    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.

    0 讨论(0)
  • 2020-12-02 07:46

    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.

    0 讨论(0)
  • 2020-12-02 07:48

    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.

    0 讨论(0)
  • 2020-12-02 07:49

    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.

    0 讨论(0)
  • 2020-12-02 07:49

    When writing an app, you’ll commonly author new widgets that are subclasses of either StatelessWidget or StatefulWidget

    Here is some Differences Between StatelessWidget and StatefulWidget Widgets:

    Stateless Widget:

    1. A widget that has an immutable state.
    2. Stateless Widgets are static widgets.
    3. They do not depend on any data change or any behavior change.
    4. 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.
    5. For Example: Text, Icon, RaisedButton are Stateless Widgets.

    Stateless Widget:

    1. A widget that has a mutable state.
    2. Stateful Widgets are dynamic widgets.
    3. They can be updated during runtime based on user action or data change.
    4. Stateful Widgets have an internal state and can re-render if the input data changes or if the Widget’s state changes.
    5. For Example: Checkbox, Radio Button, Slider are Stateful Widgets
    0 讨论(0)
提交回复
热议问题