drools有状态与无状态_无状态与有状态

drools有状态与无状态

Flutter has two core types of widgets: Stateless and Stateful. These two widgets are the building blocks of every widget that flutter provides.

Flutter有两种核心类型的小部件:无状态和有状态。 这两个小部件是flutter提供的每个小部件的构造块。

But before we get into what Stateless and Stateful is, we need to understand what State is. The simplest way to put it, State is something that can change within a widget. For example, let’s say we have a like button. The button can either be filled in, or not filled in depending on whether it has been clicked. That’s a state right there. The state of that button can either be filled in or not filled in. If a widget is constant and does not change no matter what is done, then it does not have a State.

但是,在我们了解无状态和有状态是什么之前,我们需要了解什么是国家。 表示状态最简单的方法是,状态可以在小部件内更改。 例如,假设我们有一个点赞按钮。 可以填充按钮,也可以不填充按钮,具体取决于是否单击了按钮。 那是一个州。 该按钮的状态可以填充也可以不填充。如果窗口小部件是恒定的,并且无论执行什么操作都不会更改,则它没有状态。

So now that you understand State, you can see where the names Stateless and Stateful come from. Stateless means it has no state. Stateful is full of state.

因此,既然您了解了State,就可以看到无状态和有状态的名称从何而来。 无状态意味着它没有状态。 有状态充满状态。

有状态的 (Stateful)

Stateful widgets are very powerful, as long as you can manage your state well. They can display or do something different depending on the state that it is currently in. If your app is very interactive then you will need to use a lot of Stateful widget. And you need to be attentive when managing all the different states. There’s lots of tool and methods out there that can help with state management (BLoC, Provider, etc).

只要您可以很好地管理状态,状态小部件就非常强大。 它们可以根据当前状态显示或执行不同的操作。如果您的应用程序具有很高的交互性,则您将需要使用很多有状态的小部件。 而且,在管理所有不同的状态时,您需要细心。 有很多工具和方法可以帮助进行状态管理(BLoC,Provider等)。

无状态 (Stateless)

Stateless widgets have no state. They are just widgets that can display or do the same thing all the time. They don’t change the way they behave during run time.

无状态小部件没有状态。 它们只是可以始终显示或执行相同操作的小部件。 它们在运行时不会改变其行为方式。

A good practice to follow when writing widgets is to try to have as little stateful widgets as possible. Write your code using only stateless widgets, and add stateful widgets where you see the need. For example if on your screen you only have a text that won’t change and a button that will change, there’s no need to have the whole screen be stateful. Only the button should need state (if it will change state once pressed like a like button). There are two main reasons for this. First, it will improve performance. In the example 2 sentences before we only rebuild the changing button. If the whole screen was a stateful widget then we would need to rebuild the whole screen even though only one element in the screen is changing. The second reason is because it makes your code more maintainable and easier to debug. Let’s say the button we mentioned isn’t working correctly, you won’t need to go looking at the state of the screen and figure out how it all ties together and makes the button work, you can just check the button itself and make sure it changes state accordingly and go from there.

编写窗口小部件时应遵循的一个很好的做法是,尽量减少有状态的窗口小部件。 仅使用无状态小部件编写代码,并在需要时添加有状态小部件。 例如,如果您的屏幕上只有一个不会更改的文本和一个会更改的按钮,则无需使整个屏幕保持有状态。 只有按钮应该需要状态(如果它像按钮一样按一次就会改变状态)。 这有两个主要原因。 首先,它将提高性能。 在示例2之前的句子中,我们仅重建更改按钮。 如果整个屏幕都是有状态的小部件,那么即使屏幕中只有一个元素发生变化,我们也需要重新构建整个屏幕。 第二个原因是因为它使您的代码更易于维护且更易于调试。 假设我们提到的按钮无法正常工作,您无需查看屏幕状态并弄清它们如何相互关联并使按钮正常工作,您只需检查按钮本身并确保它会相应地更改状态并从那里开始。

As you can see Stateless and Stateful widgets are a core part of flutter, and learning to manage them will help you write better code and have better architecture.

如您所见,无状态和有状态小部件是Flutter的核心部分,学会管理它们将有助于您编写更好的代码并拥有更好的体系结构。

Thanks for Reading

谢谢阅读

Follow me on Twitter: @tadaspetra

在Twitter上关注我:@tadaspetra

翻译自: https://itnext.io/stateless-vs-stateful-cde9d178084f

drools有状态与无状态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值