当前位置:网站首页>About statefulwidget, you have to know the principle and main points!
About statefulwidget, you have to know the principle and main points!
2022-07-26 23:04:00 【InfoQ】
Preface
StatelessWidgetStatefulWidgetsetStateStatefulWidgetStatefulWidget The classification of
StatefulWidgetsetStateStatefulWidgetinitStateStatefulWidgetsetStatesetStateStatefulWidget The rendering mechanism of
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 Precautions for use
setStateStatefulWidget- Push the state maintenance of the component tree down to the leaf node as much as possible. It's easy to understand , The higher the level of state maintenance , It means that the larger the reconstructed component tree . Of course, leaf nodes that place state maintenance at a low level have higher performance . for instance , Suppose you have a clock component that updates regularly every second in your page , Then the maintenance of this time state should draw out a clock component separately , It maintains the time state by itself .
- To minimize the
StatefulWidgetOfStateinbuildMethod to build the number of components. Ideally , OneStatefulWidgetThere should be only one sub component , And this component corresponds to aRenderObject. This may be difficult to meet in reality , But this is a guiding principle , If yourStatefulWidgetHas built too many components , Then the performance will naturally decline . At this time, you should consider using the state management plug-in for local refresh .
- If the sub component tree does not change in the whole life cycle , Then you should consider caching the sub component tree for reuse. This will perform much better than rebuilding this subcomponent tree every time . The usual approach is to separate this sub component tree into one
Widget, Then it is passed to as a sub componentStatefulWidget.
- Use as much as possible
constDecorate the sub component construction method . We're talking aboutconstwhen (Decrypt Flutter Of const keyword) There was an introduction to . actually , UseconstDecoration is equivalent to a caching method .
- Try to avoid changing the level of the sub component tree or the component type in the sub component tree. For example, when returning subcomponents , It is possible to return sub components or wrap sub components in
IgnorePointerin . This method actually changes the hierarchy of the sub component tree . The sub components should be wrapped inIgnorePointerin , And then throughIgnorePointerOfignoringAttribute to control . This is because , Any operation that changes the depth of the subcomponent tree will need to be rebuilt 、 Rearrange 、 Redraw the entire subcomponent tree . If you only change the attributes of a node , It will narrow the scope of change a lot ( For example, in this example , There is no need to rearrange and redraw ).
- Suppose you have to change the hierarchy of the subcomponent tree , Then you should consider using the invariant parts of the sub component tree GlobalKey Make this part in the whole
StatefulWidgetThe life cycle of is consistent. If it's not convenient to useGlobalKeyWords , Then consider usingKeyedSubtreeComponentGlobalKey.
- If
StatefulWidgetIf some of the attributes are unchanged , that The definition of these attributes should take precedence overWidgetThe definition of , And declare asfinal, instead ofStatein , This can reduceStateData to be maintained .
summary
- If time permits , Read more official documents at the beginning 、 Relevant peripheral applications and documentation , This will reduce the subsequent development cost and time —— Especially when performance bottlenecks require reconfiguration .
- If time does not allow , Then don't toss blindly when you encounter problems , Back to the official documents, first read it several times ,Flutter The official documents of are very detailed , And it is also equipped with many explanation videos , Can solve most of your puzzles .
- Multiple input , Although at home Flutter Not many players , But there are still many foreign words , Stick to visiting foreign blogs , Website shows , It can make you less detours .
边栏推荐
- Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
- Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
- 国产DRAM年底将量产,但前路依旧漫漫!
- 研究阿尔茨海默病最经典的Nature论文涉嫌造假
- 各有一项指纹专利被宣告无效!汇顶与思立微均正式回应!
- Implementation principle of semaphore in golang
- [postgresql]postgresqlg使用enerate_series() 函数补全统计
- After closing the Suzhou plant, Omron Dongguan plant announced its dissolution, and more than 2000 people are facing unemployment!
- 比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
- Summary of shell related commands
猜你喜欢

json格式化小工具--pyqt5实例

Arduino experiment I: two color lamp experiment

PostgreSQL 与 Navicat:数据库行业的中坚力量

Incremental secure file system SFS based on C language design

程序员成长第二十九篇:如何激励员工?
![[hcip] OSPF route calculation](/img/1c/ee9eee2e723b850c401f7cddda1b27.png)
[hcip] OSPF route calculation

2022 latest Tibet Construction Safety Officer simulation question bank and answers

黑马瑞吉外卖之新增员工

sql多表查询的练习

Apifox--比 Postman 还好用的 API 测试工具
随机推荐
由若干QWidget实现日历文档
中兴通讯:5G基站在全球发货已超过5万个!
Calendar documents implemented by several qwidgets
【无标题】
Interview: your most impressive bug, for example
证监会:同意传音控股科创板IPO注册
Mate30系列发布:华为的重构影像之路还能走多远?
基于C语言设计的增量型安全文件系统 SFS
面试官问:JS的this指向
[IO Development Notes] smart cloud intelligent watering device practice (3) - automatic code generation and transplantation
Apifox -- a better API testing tool than postman
[postgresql]postgresqlg使用enerate_series() 函数补全统计
STM32 how to use serial port
【HCIP】OSPF 关系建立
Siliwei's counterattack: huiding's under screen optical fingerprint patent involved in the case was declared invalid
After working for one year, I have some insights (written in 2017)
Eureka基本使用
程序员成长第二十九篇:如何激励员工?
Is test development development development?
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum