当前位置:网站首页>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边栏推荐
猜你喜欢

深入了解 XXE 注射

Nature综述:微生物群落形成过程中的优先效应

常用泰勒展开

用3dmax做折扇的思路方法与步骤

Application of user portrait in precise push of wechat official account of scientific journals

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

Preliminary understanding of Panda3D audio and advanced interactive components

QT with OpenGL(Shadow Mapping)(平行光篇)

突发,微信重要通知

【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
随机推荐
小程序容器技术超有料,可以让移动研发效率大幅提升
With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
实现按照序号命名的txt文件由后往前递补重命名文件
股价暴涨180.46%!国产大硅片龙头沪硅产业上市:近4年净利累计不足6000万
Under the epidemic, TSMC's growth in the first quarter exceeded expectations, with 7Nm accounting for 35%! Second quarter or record high
Test article
What is the MySQL data storage method?
采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
用户画像在科技期刊微信公众号精准推送中的应用
TSMC 3nm detail exposure: transistor density as high as 250million /mm ², Greatly improved performance and energy efficiency
Date的使用
2022/7/26
怎么使用xshell免费版
NDK series (6): let's talk about the way and time to register JNI functions
Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
记录一下使用R语言中关于formatC的错误
回Mixlab三天,“创造力团队”治好了我的精神内耗
js数组复制速度测试220320
台积电3nm细节曝光:晶体管密度高达2.5亿个/mm²,性能及能效大幅提升
强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)