当前位置:网站首页>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边栏推荐
- 真的很难理解?RecyclerView 缓存机制到底是几级缓存?
- Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
- 进程同步的方式有哪些?
- 消息队列常见的几种使用场景介绍
- Exercise --- BFS
- Simple and practical data visualization cases
- 你的列表很卡?这4个优化能让你的列表丝般顺滑
- 2022/7/26
- ZCMU--1720: 死亡如风,我要装逼
- Node red series (30): use persistent UI table to refresh the page without emptying the last table data
猜你喜欢

面试官:说一下网络数据传输的具体流程

寻找和利用 XXE – XML 外部实体注入

The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed

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

Sudden, wechat important notice

置信区间之正态

The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)

QT with OpenGL(Shadow Mapping)(平行光篇)
随机推荐
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
Xu Jinbo: AI protein prediction and design
What are the methods of process synchronization?
ELK日志分析系统安装和部署
在所有浏览器中禁用带有元 HTML 标记的缓存
VIM editor tutorial
sort排序
回Mixlab三天,“创造力团队”治好了我的精神内耗
携手长江存储,江波龙推出全球最小扩展卡
苹果发布新款iPhone SE:搭载A13仿生处理器,售价3299元起
真的很难理解?RecyclerView 缓存机制到底是几级缓存?
[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code
2022/7/24-7/25
Socket interaction process of three handshakes
你的列表很卡?这4个优化能让你的列表丝般顺滑
钉钉报警工具
怎么使用xshell免费版
How to quickly view the API properties and usage of the h.265 video player easyplayer?