当前位置:网站首页>你的列表很卡?这4个优化能让你的列表丝般顺滑
你的列表很卡?这4个优化能让你的列表丝般顺滑
2022-07-27 20:42:00 【InfoQ】
优化点1:使用 builder构建列表
childrenchildren//糟糕的用法
ListView(
children: [
item1,
item2,
item3,
...
],
)
//正确的用法
ListView.builder(
itemBuilder: (context, index) => ListItem(),
itemCount: itemCount,
)
优化点2:禁用 addAutomaticKeepAlives 和 addRepaintBoundaries 特性
addAutomaticKeepAlivestruefalseaddRepaintBoundariesaddAutomaticKeepAlives: false,
addRepaintBoundaries: false,
优化点3:尽可能将列表元素中不变的组件使用 const 修饰
constconstreturn Padding(
child: Row(
children: [
const ListImage(),
const SizedBox(
width: 5.0,
),
Text('第$index 个元素'),
],
),
padding: EdgeInsets.all(10.0),
);
优化点4:使用 itemExtent 确定列表元素滚动方向的尺寸
itemExtentitemExtent: 120,
优化实例
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('大列表'),
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('第$index 个元素'),
],
),
),
),
),
);
}
}
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('大列表'),
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('第$index 个元素'),
],
),
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,
);
}
}
总结
ListView边栏推荐
- NDK 系列(6):说一下注册 JNI 函数的方式和时机
- MySQL basic installation and startup
- Vulnhub range double trouble
- 360入选中国安全产业全景图63个领域 ISC2022共话安全服务方向
- "The faster the code is written, the slower the program runs."
- 微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条
- Flink怎么使用Savepoint
- Nature综述:微生物群落形成过程中的优先效应
- Cloudcompare & PCL point cloud equally spaced slices
- 我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律
猜你喜欢

NDK series (6): let's talk about the way and time to register JNI functions

怎么使用C# Winform实现复制文件显示进度

用户画像在科技期刊微信公众号精准推送中的应用

With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million

置信区间之正态

20 character short domain name bypass replication

Implicit indicators for evaluating the advantages and disadvantages of automated testing

LANproxy mapping local development environment
![[number recognition] handwritten number recognition based on knowledge base with matlab code](/img/06/6adab955a339f453249543baab1dc6.png)
[number recognition] handwritten number recognition based on knowledge base with matlab code

网络开发套接字以及UDP、TCP协议
随机推荐
kubevela通过CLI部署应用
Basic lighting of unity
Safety foundation 2
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery
The security dilemma of software supply chain faced by enterprises
面试官:说一下网络数据传输的具体流程
Xu Jinbo: AI protein prediction and design
【信号处理】基于高阶统计量特征的通信系统中微弱信号检测附matlab代码
Lianmai live broadcast system software - voice chat system
图基本知识代码
8000 word explanation of OBSA principle and application practice
Pyqt5 rapid development and practice 4.10 window drawing controls
containerd ctr运行ansible容器执行ansible-playbook任务完整命令
Google executives: 40% of "generation Z" are more willing to use tiktok to cannibalize Google's core products
Analysis of cloud native application security organization structure
Learn more about xxE injection
Read an article to understand artificial neural network
使用灰度滤镜
JVM composition and memory model
[number recognition] handwritten number recognition based on knowledge base with matlab code