简体   繁体   English

Flutter 中有状态和无状态小部件之间的关系是什么?

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

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.但是对于StatelessWidget ,将StatefulWidget作为其子代之一是一种非常常见的做法。 Doesn't StatelessWidget become stateful if it has StatefulWidget as one of its children?如果StatelessWidget作为其子代之一,那么StatelessWidget不会变成有StatefulWidget吗?

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 .我尝试查看文档作为StatelessWidget代码的一部分,但无法弄清楚StatelessWidget如何将Statefulwidget作为其子项并仍然保持StatelessWidget

What is the relation and difference between stateful and stateless widgets in Flutter? Flutter 中有状态和无状态小部件的关系和区别是什么?

A StatelessWidget will never rebuild by itself (but can from external events). StatelessWidget永远不会自行重建(但可以从外部事件重建)。 A StatefulWidget can. StatefulWidget可以。 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 eg 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.它对你没有任何影响。

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.但是,让StatelessWidget拥有StatefulWidget作为其子级之一是一种非常普遍的做法。 Doesn't StatelessWidget become stateful if it has StatefulWidget as one of its children?如果将StatefulWidget作为其子级之一, StatelessWidget不会变成有StatefulWidget吗?

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 .我尝试将文档作为StatelessWidget代码的一部分进行研究,但无法弄清楚StatelessWidget如何将Statefulwidget作为其子级,并且仍然保持StatelessWidget

What is the relation and difference between stateful and stateless widgets in Flutter? Flutter中有状态和无状态小部件之间的关系和区别是什么?

From the documentation at flutter.io :来自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.他们重建每一帧,不同之处在于 StatefulWidget 有一个 State 对象,它跨帧存储状态数据并恢复它。

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.但是,如果子级对更改做出反应,并且父级对更改没有反应,则包含的父级仍然可以是无状态小部件。

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.小部件的状态存储在 State 对象中,将小部件的状态与其外观分开。 When the widget's state changes, the state object calls setState(), telling the framework to redraw the widget.当小部件的状态改变时,状态对象调用 setState(),告诉框架重绘小部件。

A stateless widget has no internal state to manage.无状态小部件没有要管理的内部状态。 Icon, IconButton, and Text are examples of stateless widgets, which subclass StatelessWidget. Icon、IconButton 和 Text 是无状态小部件的示例,它们是 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).用户可以与有状态的小部件进行交互(例如,通过在表单中​​输入内容或移动滑块),或者它会随时间发生变化(也许数据馈送会导致 UI 更新)。 Checkbox, Radio, Slider, InkWell, Form, and TextField are examples of stateful widgets, which subclass StatefulWidget. Checkbox、Radio、Slider、InkWell、Form 和 TextField 是有状态小部件的示例,它们是 StatefulWidget 的子类。

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

State is information that (1) can be read synchronously when the widget is built and (2) might change during the lifetime of the widget.状态是 (1) 可以在构建小部件时同步读取和 (2) 可能在小部件的生命周期内更改的信息。 It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes, using State.setState.小部件实现者有责任使用 State.setState 确保在此类状态更改时及时通知 State。

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 (eg, consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).构建过程以递归方式继续,直到用户界面的描述完全具体(例如,完全由描述具体 RenderObject 的 RenderObjectWidgets 组成)。

Stateful widget are useful when the part of the user interface you are describing can change dynamically, eg 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.对于仅依赖于对象本身的配置信息和小部件在其中膨胀的 BuildContext 的组合,请考虑使用 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. StatefulWidget 实例本身是不可变的,并且将它们的可变状态存储在由 createState 方法创建的单独 State 对象中,或者存储在该 State 订阅的对象中,例如 Stream 或 ChangeNotifier 对象,这些对象的引用存储在 StatefulWidget 的最终字段中本身。

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 (eg, consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).构建过程以递归方式继续,直到用户界面的描述完全具体(例如,完全由描述具体 RenderObject 的 RenderObjectWidgets 组成)。

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.当您描述的用户界面部分不依赖于对象本身的配置信息和小部件在其中膨胀的 BuildContext 之外的任何其他内容时,无状态小部件很有用。 For compositions that can change dynamically, eg due to having an internal clock-driven state, or depending on some system state, consider using StatefulWidget.对于可以动态更改的组合,例如由于具有内部时钟驱动状态或取决于某些系统状态,请考虑使用 StatefulWidget。

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. Text、Icon、RaisedButton 是无状态小部件。

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.如果 Widget 可以在运行时更改其状态,那么它就是有状态的 Widget。

Edit 15/11/2018编辑 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.因为Stateless Widgets没有状态,它们会被渲染一次,不会自己更新,只会在外部数据发生变化时更新。

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.无状态和有状态小部件都有不同的生命周期。

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.在电视的另一侧,您可以打开、关闭、更改频道、播放电影(如果连接了 DVD)等。电视具有影响其行为方式的内部状态。 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.希望这可以帮助。

When writing an app, you'll commonly author new widgets that are subclasses of either StatelessWidget or StatefulWidget在编写应用程序时,您通常会创作作为StatelessWidgetStatefulWidget子类的新小部件

Here is some Differences Between StatelessWidget and StatefulWidget Widgets:以下是StatelessWidgetStatefulWidget之间的一些差异:

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. Stateless Widgets 没有状态,它们会被渲染一次并且不会自我更新,只会在外部数据发生变化时更新。
  5. For Example: Text , Icon , RaisedButton are Stateless Widgets.例如: TextIconRaisedButton是无状态小部件。

Stateful 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例如: CheckboxRadio ButtonSlider是有状态的小部件

Answer for the Stack Overflow question - statefulness vs statelessness . Stack Overflow 问题的答案 - statefulness vs stateless

In Flutter, the difference is that stateless widgets can be defined by all the constructor arguments alone.在 Flutter 中,不同之处在于无状态小部件可以由所有构造函数参数单独定义。 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 .实际上,有状态小部件本身是不可变的(无状态),但 Flutter 管理一个单独的状态对象并将其与小部件相关联,如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.这意味着当 Flutter 重建一个有状态的小部件时,它会检查它是否应该重用以前的状态对象,并在需要时将该状态对象附加到小部件。

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.有状态的孩子本身(或技术上的 Flutter)会处理自己的状态。
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.但是从Flutter的角度来看,它构建父widget并不关心状态,只有在构建子widget时才会考虑其状态。

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 .这就是为什么它只有一个类扩展了StatelessWidget So if I say, they can never re-run build() method again.因此,如果我说,他们永远无法再次运行build()方法。

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 ie State<YourWidget> .在这里,我们有 2 个类,一个是StatefulWidget ,另一个是 State 实现处理程序,即State<YourWidget> So if I say, they can re-run build() method again & again based on events triggered.因此,如果我说,他们可以根据触发的事件一次又一次地重新运行build()方法。

Below diagram will help.下图会有所帮助。

在此处输入图片说明

What are Stateful and Stateless widgets?什么是有状态和无状态小部件?

TL;DR: A widget that allows you to refresh the screen is a Stateful widget. TL;DR:允许您刷新屏幕的小部件是有状态小部件。 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!别客气!

disclaimer :- started working on flutter from last week :)免责声明:- 从上周开始研究颤振:)

stateless and statefull widget has his own lifecycle to create and update the UI.无状态有状态小部件有自己的生命周期来创建和更新 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.但是,您可以使用无状态或有状态来呈现 UI,但当 ui 完全或部分依赖于外部数据(例如 - 使用 api 呈现列表)而使用无状态小部件呈现静态 ui 时,实际上有状态会更方便,就像任何输入屏幕一样一个很好的做法。

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.我们正在创建 Text 小部件,因此我们可以为其提供字符串、颜色、字体大小、字体系列。 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 ie class level but it could have another widget/class in it which cares about data ie it is Stateful.现在,如果你定义了一个无状态的类,这意味着这个类不关心/有变量,或者说它自己的类中的数据,即类级别,但它可能有另一个关心数据的小部件/类,即它是有状态的. So it does not have any impact on each other.所以对彼此没有任何影响。

Please correct me if I am wrong here.如果我在这里错了,请纠正我。

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.有状态的小部件:状态完整的小部件保存小部件的状态,并且可以在状态改变时重建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM