当前位置:网站首页>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 .
边栏推荐
- 研究阿尔茨海默病最经典的Nature论文涉嫌造假
- Why am I still writing articles on CSDN? A journey of accompanying learning.
- The secret weapon of apple iphone11 series: U1 chip may usher in the era of ultra wideband
- Calendar documents implemented by several qwidgets
- 【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版
- CSRC: agree to the IPO registration of Chuanyin holding science and Technology Innovation Board
- Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess
- Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
- 2019 biometric forum successfully ended: these ten highlights should not be missed!
- Day07 MySql知识点再总结与多表查询
猜你喜欢

The JSON string is converted into a JSON object, the value of a key is obtained, and whether a key exists is judged

研究阿尔茨海默病最经典的Nature论文涉嫌造假

Shardingsphere JDBC keyword problem

基于C语言的页式文件系统

Regular expressions and bypass case recurrence

STM32 how to use serial port

Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum

利用Go制作微信机器人(一)发送消息

Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information

你知道磁环电感的常见磁芯类型有哪些吗?
随机推荐
Science | 华盛顿大学利用AI和结构预测设计全新蛋白质
Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
Kt6368a Bluetooth chip development precautions and problem collection - long term update
华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
实战项目:Boost搜索引擎
Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information
基于gRPC编写golang简单C2远控
sql多表查询的练习
Openstack virtual machine network card is renamed cirename0
HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
【无标题】
Why did kylin 990 series fail to meet cortex-a77 and Mali G77?
Day07 MySQL knowledge points re summary and multi table query
One fingerprint patent has been declared invalid! Huiding and siliwei both officially responded!
基于C语言的页式文件系统
A13 processor has become the biggest highlight of iphone11 series: its performance is twice that of Kirin 980!
Systemctl command
2022 latest Tibet Construction Safety Officer simulation question bank and answers
【HCIP】OSPF 路由计算
Promote the replacement of X86 with arm server chips, and Huawei and Feiteng carry the banner of localization!