当前位置:网站首页>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边栏推荐
- 他山之石 | 蚂蚁超大规模知识图谱构建及应用
- 用户画像在科技期刊微信公众号精准推送中的应用
- MySQL之数据查询(WHERE)
- With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
- 用3dmax做折扇的思路方法与步骤
- 回Mixlab三天,“创造力团队”治好了我的精神内耗
- Flink怎么使用Savepoint
- Record the errors about formatc in R language
- Winform怎么使用FTP实现自动更新
- Socket interaction process of four waves
猜你喜欢

Nature review: preferential effects in the formation of microbial communities

回Mixlab三天,“创造力团队”治好了我的精神内耗

消息队列常见的几种使用场景介绍

NDK 系列(6):说一下注册 JNI 函数的方式和时机

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug

Bi Shi - University Logistics Management System Based on SSM

西门子PLC能否实时无线采集多处从站模拟量数据?

iMeta | 国际标准刊号ISSN印刷版正式确认,双ISSN申请完成

【C语言】通讯录(动态版本)

面试官:说一下网络数据传输的具体流程
随机推荐
史上最简明的 Tcpdump 入门指南,看这一篇就够了
用户画像在科技期刊微信公众号精准推送中的应用
数据管理的重点
技术认证 | 图扑软件携手华为云再创合作共赢新局面
Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server
实现按照序号命名的txt文件由后往前递补重命名文件
Zabbix4.0 uses SNMP agent to monitor vcenter6.5
进制转换方法
QT with OpenGL(Shadow Mapping)(平行光篇)
Learn more about xxE injection
15million per day! BYD masks won a US $1billion order in California
JUC工具包学习
常用泰勒展开
详解分布式系统的幂等
【C语言】通讯录(动态版本)
在所有浏览器中禁用带有元 HTML 标记的缓存
使用灰度滤镜
[signal denoising] signal denoising based on Kalman filter with matlab code
Visual display method of machine learning project
Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market