国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Flutter中ListView加載圖片數(shù)據(jù)的優(yōu)化

2020-08-02 21:24 作者:早起的年輕人  | 我要投稿

在使用ListView懶加載模式時,當(dāng)ListView的Item中有圖片信息時,在快速滾動過程中會大量的浪費(fèi)流量與內(nèi)存,甚至?xí)斐稍跐L動過程中頁面的卡頓效果。
在這里提出優(yōu)化方案,當(dāng)開始滾動時不加載圖片,滾動結(jié)束后再加載圖片,這個優(yōu)化方案實現(xiàn)的效果如下圖所示,在快速滑動列表數(shù)據(jù)時,圖片未加載,運(yùn)行內(nèi)存無明顯波動。


實現(xiàn)代碼如下:

class ListViewUsePage13 extends StatefulWidget {

? @override

? State<StatefulWidget> createState() {

? ? return ScrollHomePageState();

? }

}


class ScrollHomePageState extends State {

? ///加載圖片的標(biāo)識

? bool isLoadingImage = true;

? ///網(wǎng)絡(luò)圖片地址

? String netImageUrl =

? ? ? "https://images.gitee.com/uploads/images/2020/0602/203000_9fa3ddaa_568055.png";

? @override

? Widget build(BuildContext context) {

? ? return Scaffold(

? ? ? appBar: new AppBar(

? ? ? ? title: Text("詳情"),

? ? ? ),

? ? ? ///列表

? ? ? body: NotificationListener(

? ? ? ? ///子Widget中的滾動組件滑動時就會分發(fā)滾動通知

? ? ? ? child: buildListView(),

? ? ? ? ///每當(dāng)有滑動通知時就會回調(diào)此方法

? ? ? ? onNotification: notificationFunction,

? ? ? ),

? ? );

? }


? bool notificationFunction(Notification notification) {

? ? ///通知類型

? ? switch (notification.runtimeType) {

? ? ? case ScrollStartNotification:

? ? ? ? print("開始滾動");

? ? ? ? ///在這里更新標(biāo)識 刷新頁面 不加載圖片

? ? ? ? isLoadingImage = false;

? ? ? ? break;

? ? ? case ScrollUpdateNotification:

? ? ? ? print("正在滾動");

? ? ? ? break;

? ? ? case ScrollEndNotification:

? ? ? ? print("滾動停止");


? ? ? ? ///在這里更新標(biāo)識 刷新頁面 加載圖片

? ? ? ? setState(() {

? ? ? ? ? isLoadingImage = true;

? ? ? ? });

? ? ? ? break;

? ? ? case OverscrollNotification:

? ? ? ? print("滾動到邊界");

? ? ? ? break;

? ? }

? ? return true;

? }


? ListView buildListView() {

? ? return ListView.separated(

? ? ? itemCount: 10000, //子條目個數(shù)

? ? ? ///構(gòu)建每個條目

? ? ? itemBuilder: (BuildContext context, int index) {

? ? ? ? if (isLoadingImage) {

? ? ? ? ? ///這時將子條目單獨(dú)封裝在了一個StatefulWidget中

? ? ? ? ? return Image.network(

? ? ? ? ? ? netImageUrl,

? ? ? ? ? ? width: 100,

? ? ? ? ? ? height: 100,

? ? ? ? ? ? fit: BoxFit.fitHeight,

? ? ? ? ? );

? ? ? ? } else {

? ? ? ? ? return Container(

? ? ? ? ? ? height: 100,

? ? ? ? ? ? width: 100,

? ? ? ? ? ? child: Text("加載中..."),

? ? ? ? ? ); //占位

? ? ? ? }

? ? ? },

? ? ? ///構(gòu)建每個子Item之間的間隔Widget

? ? ? separatorBuilder: (BuildContext context, int index) {

? ? ? ? return new Divider();

? ? ? },

? ? );

? }

}



Flutter中ListView加載圖片數(shù)據(jù)的優(yōu)化的評論 (共 條)

分享到微博請遵守國家法律
夹江县| 新营市| 鄂州市| 大姚县| 乐至县| 淮阳县| 金山区| 勃利县| 汽车| 昭觉县| 醴陵市| 通化市| 离岛区| 资源县| 土默特右旗| 新竹县| 海丰县| 阳城县| 靖西县| 山阴县| 徐汇区| 双桥区| 河东区| 光山县| 秭归县| 库尔勒市| 水富县| 确山县| 长武县| 和龙市| 玉树县| 南昌市| 理塘县| 中西区| 志丹县| 平定县| 上高县| 谷城县| 青田县| 中牟县| 自贡市|