当前位置:网站首页>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 .
边栏推荐
- kalibr标定realsenseD435i --多相机标定
- Embedded sig | distributed soft bus
- 芯鼎收购紫光控股!万业企业:全面转型集成电路!
- 中兴通讯:5G基站在全球发货已超过5万个!
- Page file system based on C language
- Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
- Openstack virtual machine network card is renamed cirename0
- Making wechat robot with go (I) sending messages
- HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
- HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
猜你喜欢

Docker uses mysql:5.6 and owncloud image to build a personal network disk, install and build a private warehouse harbor

Making wechat robot with go (I) sending messages

正则表达式与绕过案例复现

HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由

Eureka基本使用

Parameter analysis and stone jumping board

【HCIP】OSPF 关系建立

Ribbon负载均衡

实战项目:Boost搜索引擎

Summary of shell related commands
随机推荐
2019 biometric forum successfully ended: these ten highlights should not be missed!
什么是 Base64 ?
【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版
Calendar documents implemented by several qwidgets
Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
正则表达式与绕过案例复现
Incremental secure file system SFS based on C language design
比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
Day07 MySql知识点再总结与多表查询
Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective
Kt6368a Bluetooth chip development precautions and problem collection - long term update
Huawei conspires to acquire Brazilian operators?
推动ARM服务器芯片替代X86,华为、飞腾扛起国产化大旗!
The JSON string is converted into a JSON object, the value of a key is obtained, and whether a key exists is judged
Sequence table implementation
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
基于gRPC编写golang简单C2远控
PostgreSQL and Navicat: the backbone of the database industry
蔚来杯2022牛客暑期多校训练营2
New employees of black maredge takeout