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

查看:30
本文介绍了Flutter 中有状态和无状态小部件之间的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有状态小部件被定义为在其生命周期内改变其状态的任何小部件.但是对于 StatelessWidget 来说,将 StatefulWidget 作为其子代之一是一种非常常见的做法.如果 StatelessWidgetStatefulWidget 作为其子代之一,它不会变成有状态的吗?

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. Doesn't StatelessWidget become stateful if it has StatefulWidget as one of its children?

我尝试查看文档作为 StatelessWidget 代码的一部分,但无法弄清楚 StatelessWidget 如何拥有 Statefulwidget作为它的孩子并且仍然保持 StatelessWidget.

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.

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

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

推荐答案

A StatelessWidget 永远不会自己重建(但可以从外部事件中重建).StatefulWidget 可以.这是黄金法则.

A StatelessWidget will never rebuild by itself (but can from external events). A StatefulWidget can. 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 e.g. 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.

这篇关于Flutter 中有状态和无状态小部件之间的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆