当前位置:网站首页>关于 StatefulWidget,你不得不知道的原理和要点!
关于 StatefulWidget,你不得不知道的原理和要点!
2022-07-26 22:14:00 【InfoQ】
前言
StatelessWidgetStatefulWidgetsetStateStatefulWidgetStatefulWidget 的分类
StatefulWidgetsetStateStatefulWidgetinitStateStatefulWidgetsetStatesetStateStatefulWidget 的渲染机制
RenderObjectStatefulWidgetsetStateclass ItemCounter extends StatefulWidget {
ItemCounter({Key? key, required this.name}) : super(key: key);
final String name;
@override
_ItemCounterState createState() => _ItemCounterState();
}
class _ItemCounterState extends State<ItemCounter> {
int count = 0;
@override
Widget build(BuildContext context) {
return GestureDetector(
child: Text('${widget.name}: $count'),
onTap: () {
setState(() {
count++;
});
},
);
}
}
StatefulElementStatefulElement(StatefulWidget widget)
: state = widget.createState(),
//...
StatefulElementStatelessElementText(Tom:0)GestureDetectorStatecount
countcountbuildText(Tom: 1)WidgetText(Tom:0)WidgetTreeText(Tom: 1)WidgetWidgetStatefulElementWidgetStatefulElement

WidgetStatefulElementStatefulElementWidgetItemCounter(name: 'Tom')ItemCounter (name: 'Dan')



didUpdateWidget@override
void didUpdateWidget(covariant ItemCounter oldWidget) {
super.didUpdateWidget(oldWidget);
}
StatefulWidget 使用注意事项
setStateStatefulWidget- 尽可能将组件树的状态维护往下推到叶子节点。这个很好理解,状态维护层级越高,意味着重建的组件树越大。当然是将状态维护放在低层级的叶子节点性能更高。举个例子,假设你页面中有一个每秒定时更新的时钟组件,那么这个时间状态的维护应该单独抽出一个时钟组件,由它自己维护时间状态。
- 最小化
StatefulWidget的State中build方法构建的组件的数量。理想情况下,一个StatefulWidget应该只有一个子组件,且这个组件对应一个RenderObject。这个在现实中可能很难满足,但是这是一个指导原则,如果你的StatefulWidget的构建了太多组件,那么性能自然而然会下降。这个时候应该要考虑使用状态管理插件进行局部刷新了。
- 如果子组件树在整个生命周期都不改变的话,那么应该考虑将该子组件树缓存起来重复利用。这会比每次重新构建这个子组件树性能好很多。通常的做法是将这个子组件树单独抽离为一个
Widget,然后作为子组件传给StatefulWidget。
- 尽可能地使用
const修饰子组件构造方法。这个我们在讲const时(解密 Flutter 的 const 关键字)有介绍过。实际上,使用const修饰相当于是一种缓存方式。
- 尽可能避免更改子组件树的层级或子组件树中的组件类型。例如在返回子组件时,有可能会根据条件返回子组件或将子组件包裹在
IgnorePointer中。这种方式其实就是改变了子组件树的层级。应该将子组件统一包裹在IgnorePointer中,然后通过IgnorePointer的ignoring属性来控制。这是因为,任何更改子组件树深度的操作都会需要重新构建、重新布局、重新绘制整个子组件树。而只更改某个节点的属性的话,将会将改变的范围缩小很多(例如这个例子中,就不需要重新布局和重绘)。
- 假设不得不更改子组件树的层级,那么应该考虑将子组件树中不变的部分使用 GlobalKey 使得这部分在整个
StatefulWidget的生命周期都保持一致。如果不方便使用GlobalKey的话,那么可以考虑使用KeyedSubtree组件来应用GlobalKey。
- 如果
StatefulWidget中有些属性是不变的话,那么 这些属性的定义应该优先放在Widget的定义中,并声明为final,而不是State中,这样可以减少State需要维护的数据。
总结
- 如果时间允许,一开始就多看看官方文档、相关周边的应用及说明文档,这样会减少后续很多的开发成本和时间——尤其是遇到性能瓶颈需要重构的时候。
- 如果时间不允许,那么遇到问题的时候不要盲目折腾,回到官方文档先看几遍,Flutter 的官方文档非常详尽,而且还配套了很多讲解视频,能够解决你大部分困惑。
- 多输入,虽然国内 Flutter 的玩家不太多,但是国外的话还是很多的,坚持逛逛国外的博客,官网说明,能够让你少走很多弯路。
边栏推荐
- Summary of shell related commands
- 让程序在一秒或者多秒中做一件事情
- 菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
- Introduction to MySQL database
- Plato farm is expected to further expand its ecosystem through elephant swap
- Sequence table implementation
- [MySQL] - index principle and use
- MySQL summarizes common scenarios of MySQL index failure
- 【HCIP】OSPF 路由计算
- 电脑开机后内存占用过高(50%以上)
猜你喜欢

面试:你印象最深的BUG,举个例子

Why am I still writing articles on CSDN? A journey of accompanying learning.

Calendar documents implemented by several qwidgets

Ribbon负载均衡
![[hcip] OSPF route calculation](/img/1c/ee9eee2e723b850c401f7cddda1b27.png)
[hcip] OSPF route calculation
电脑开机后内存占用过高(50%以上)

golang中的信号量的实现原理

gateway基本使用
![[IO Development Notes] smart cloud intelligent watering device practice (3) - automatic code generation and transplantation](/img/f6/e23089c473cc72a7d763ff975d8e62.png)
[IO Development Notes] smart cloud intelligent watering device practice (3) - automatic code generation and transplantation

Cached database for memcached
随机推荐
商汤科技CEO徐立:公司估值已超70亿美元,不急于上市
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
沟通中经常用到的几个库存术语
各有一项指纹专利被宣告无效!汇顶与思立微均正式回应!
Openstack virtual machine network card is renamed cirename0
利用Go制作微信机器人(一)发送消息
With a total investment of 10billion US dollars, Huahong Wuxi 12 inch wafer factory was officially put into operation
Docker uses mysql:5.6 and owncloud image to build a personal network disk, install and build a private warehouse harbor
Sequence table implementation
json格式化小工具--pyqt5实例
纷享销客罗旭对话西门子王海滨:先进制造,要向数字化要效益
MySQL summarizes common scenarios of MySQL index failure
『Mysql』汇总Mysql索引失效的常见场景
Domestic DRAM will be mass produced by the end of the year, but the road ahead is still long!
Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
华为Atlas900揭秘:集成数千颗昇腾910芯片,算力堪比50万台PC!
[postgresql]postgresqlg使用enerate_series() 函数补全统计
Plato farm is expected to further expand its ecosystem through elephant swap
证监会:同意传音控股科创板IPO注册
Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!