当前位置:网站首页>Your list is too laggy? These four optimizations can make your list silky smooth
Your list is too laggy? These four optimizations can make your list silky smooth
2022-07-27 23:38:00 【InfoQ】
Optimization point 1: Use builder Build list
childrenchildren// Bad usage
ListView(
children: [
item1,
item2,
item3,
...
],
)
// The right way to use
ListView.builder(
itemBuilder: (context, index) => ListItem(),
itemCount: itemCount,
)
Optimization point 2: Ban addAutomaticKeepAlives and addRepaintBoundaries characteristic
addAutomaticKeepAlivestruefalseaddRepaintBoundariesaddAutomaticKeepAlives: false,
addRepaintBoundaries: false,
Optimization point 3: Use the same components in the list elements as much as possible const modification
constconstreturn Padding(
child: Row(
children: [
const ListImage(),
const SizedBox(
width: 5.0,
),
Text(' The first $index Elements '),
],
),
padding: EdgeInsets.all(10.0),
);
Optimization point 4: Use itemExtent Determines the size of the scroll direction of the list element
itemExtentitemExtent: 120,
Optimization examples
class LargeListView extends StatefulWidget {
const LargeListView({Key? key}) : super(key: key);
@override
_LargeListViewState createState() => _LargeListViewState();
}
class _LargeListViewState extends State<LargeListView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(' Big list '),
brightness: Brightness.dark,
),
body: ListView(
children: List.generate(
1000,
(index) => Padding(
padding: EdgeInsets.all(10.0),
child: Row(
children: [
Image.network(
'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7869eac08a7d4177b600dc7d64998204~tplv-k3u1fbpfcp-watermark.jpeg',
width: 200,
),
const SizedBox(
width: 5.0,
),
Text(' The first $index Elements '),
],
),
),
),
),
);
}
}
List.generateList<Widget>ListViewchildrenimport 'package:flutter/material.dart';
class LargeListView extends StatefulWidget {
const LargeListView({Key? key}) : super(key: key);
@override
_LargeListViewState createState() => _LargeListViewState();
}
class _LargeListViewState extends State<LargeListView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(' Big list '),
brightness: Brightness.dark,
),
body: ListView.builder(
itemBuilder: (context, index) => ListItem(
index: index,
),
itemCount: 1000,
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
itemExtent: 120.0,
),
);
}
}
class ListItem extends StatelessWidget {
final int index;
ListItem({Key? key, required this.index}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
child: Row(
children: [
const ListImage(),
const SizedBox(
width: 5.0,
),
Text(' The first $index Elements '),
],
),
padding: EdgeInsets.all(10.0),
);
}
}
class ListImage extends StatelessWidget {
const ListImage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Image.network(
'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7869eac08a7d4177b600dc7d64998204~tplv-k3u1fbpfcp-watermark.jpeg',
width: 200,
);
}
}
summary
ListView边栏推荐
- Zabbix4.0 uses SNMP agent to monitor vcenter6.5
- Interviewer: let's talk about the specific process of network data transmission
- 华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
- Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
- My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline
- 实现按照序号命名的txt文件由后往前递补重命名文件
- Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
- Security-001
- 用3dmax做折扇的思路方法与步骤
- NB-IoT产业的现状与未来:跨过1亿出货门槛,奔向5G大连接!
猜你喜欢

华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点

See how Gan controls the image generation style step by step? Explain the evolution process of stylegan in detail

【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码

The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn

【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析

After returning to mixlab for three days, "creative team" cured my spiritual internal friction

深入了解 XXE 注射

记录一下使用R语言中关于formatC的错误

My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline
![[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery](/img/ef/aa490aeff5a0690257cd6eca7d5e28.png)
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery
随机推荐
小程序容器技术超有料,可以让移动研发效率大幅提升
VIM editor tutorial
用户画像在科技期刊微信公众号精准推送中的应用
iMeta | 国际标准刊号ISSN印刷版正式确认,双ISSN申请完成
MapReduce(三)
加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
突发,微信重要通知
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
实现按照序号命名的txt文件由后往前递补重命名文件
After returning to mixlab for three days, "creative team" cured my spiritual internal friction
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
【数字识别】基于Hopfield神经网络识别0-9数字附Matlab代码
他山之石 | 蚂蚁超大规模知识图谱构建及应用
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
Interviewer: let's talk about the specific process of network data transmission
The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
Tita 的OKR系统与其他同类型产品,或者是共享文档等相比,有什么优势?