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

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

问题描述

有状态窗口小部件定义为在其生存期内更改其状态的任何窗口小部件.但是StatelessWidgetStatefulWidget作为其子级之一是非常普遍的做法.如果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?

推荐答案

一个 StatelessWidget 绝不会单独进行 rebuild (但可以通过外部事件进行).一个 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天全站免登陆