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

前端 未结 14 1878
悲&欢浪女
悲&欢浪女 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:32

    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.

提交回复
热议问题