Flutter 下拉刷新加載的封裝 ,pull_to_refresh 的使用

pull_to_refresh 是flutter常用的列表刷新加載組件,因?yàn)轫?xiàng)目中通常列表是比較多的,所以便封裝了一個(gè)類,把需要復(fù)用到的代碼整合起來,一下是我項(xiàng)目中對(duì)pull_to_refresh 封裝的代碼:




import?'package:flutter/material.dart';

import?'package:flutter_jtcenter/color_style.dart';

import?'package:lottie/lottie.dart';

import?'package:pull_to_refresh/pull_to_refresh.dart';

class?Refresh?{

??static?header({type:?1})?{

????Widget?load?=?Column(children:?[

??????Container(

????????width:?79,

????????height:?15,

????????margin:?EdgeInsets.only(top:?5,?bottom:?8.0),

????????child:?Image.asset(

??????????type?==?0???'images/logo.png'?:?'images/logo-red.png',

??????????fit:?BoxFit.fill,

????????),

??????),

??????Container(

????????alignment:?Alignment.center,

????????width:?150.0,

????????//?height:?30.0,

????????child:?Lottie.asset(

????????????'assets/lottie/Loading-${type?==?0???'white'?:?'red'}.json'),

??????)

????]);

????return?CustomHeader(

??????builder:?(context,?mode)?{

????????Widget?body;

????????if?(mode?==?RefreshStatus.idle)?{

??????????body?=?load;

????????}?else?if?(mode?==?RefreshStatus.refreshing)?{

??????????body?=?load;

????????}?else?if?(mode?==?RefreshStatus.canRefresh)?{

??????????body?=?load;

????????}?else?if?(mode?==?RefreshStatus.completed)?{

??????????body?=?load;

????????}?else?if?(mode?==?RefreshStatus.failed)?{

??????????body?=?Text(

????????????'網(wǎng)絡(luò)異常,請(qǐng)刷新重試',

????????????style:?TextStyle(

??????????????fontSize:?14,

??????????????color:?ColorStyle.tipsColor,

????????????),

??????????);

????????}

????????return?Container(

??????????height:?60.0,

??????????child:?Center(

????????????child:?body,

??????????),

????????);

??????},

????);

??}

??static?footer({int?len:?1})?{

????var?sty?=?TextStyle(color:?ColorStyle.tipsColor);

????return?CustomFooter(

??????builder:?(BuildContext?context,?LoadStatus?mode)?{

????????Widget?body;

????????if?(mode?==?LoadStatus.idle)?{

??????????body?=?Text("上拉加載",?style:?sty);

????????}?else?if?(mode?==?LoadStatus.loading)?{

??????????body?=?Row(

????????????mainAxisAlignment:?MainAxisAlignment.center,

????????????children:?[

??????????????Image.asset(

????????????????'images/loading.gif',

????????????????fit:?BoxFit.fill,

????????????????width:?15,

????????????????height:?15,

??????????????),

??????????????SizedBox(width:?5),

??????????????Text("加載中...",?style:?sty)

????????????],

??????????);

????????}?else?if?(mode?==?LoadStatus.failed)?{

??????????body?=?Text("加載失??!",?style:?sty);

????????}?else?if?(mode?==?LoadStatus.canLoading)?{

??????????body?=?Text("松手,加載更多!",?style:?sty);

????????}?else?{

??????????body?=?Text(len?==?0???"暫無數(shù)據(jù)"?:?"沒有更多數(shù)據(jù)了!",?style:?sty);

????????}

????????return?Container(

??????????height:?55.0,

??????????child:?Center(child:?body),

????????);

??????},

????);

??}

??static?fail()?{

????return?Column(

??????mainAxisAlignment:?MainAxisAlignment.center,

??????children:?[

????????Image.asset(

??????????'images/fail.png',

??????????width:?100.0,

??????????height:?100.0,

??????????fit:?BoxFit.fill,

????????),

????????Text(

??????????'網(wǎng)絡(luò)異常,請(qǐng)刷新重試',

??????????style:?TextStyle(

????????????fontSize:?14,

????????????color:?ColorStyle.tipsColor,

??????????),

????????),

??????],

????);

??}

??static?nodata()?{

????return?Column(

??????mainAxisAlignment:?MainAxisAlignment.center,

??????children:?[

????????Image.asset(

??????????'images/ico_nodata.png',

??????????width:?100.0,

??????????height:?100.0,

??????????fit:?BoxFit.fill,

????????),

????????//?Text(

????????//???'暫無數(shù)據(jù)',

????????//???style:?TextStyle(

????????//?????fontSize:?14,

????????//?????color:?ColorStyle.tipsColor,

????????//???),

????????//?),

??????],

????);

??}

??static?setStatus(res,?_controller,?fn,?err)?{

????if?(res["code"]?==?1)?{

??????if?(res['data']?!=?null)?{

????????int?total?=?res['data']['total'];

????????List?orderData?=?res['data']['records'];

????????fn(orderData,?total);

????????if?(_controller.isRefresh)?{

??????????//?下拉刷新

??????????_controller.refreshCompleted();

??????????if?(orderData.length?==?total)?{

????????????_controller.loadNoData();

??????????}?else?{

????????????_controller.loadComplete();

??????????}

????????}?else?if?(_controller.isLoading)?{

??????????//?上拉加載

??????????if?(orderData.length?==?total)?{

????????????_controller.loadNoData();

??????????}?else?{

????????????_controller.loadComplete();

??????????}

????????}?else?{

??????????if?(orderData.length?==?total)?{

????????????_controller.loadNoData();

??????????}?else?{

????????????_controller.loadComplete();

??????????}

????????}

??????}

????}?else?{

??????if?(_controller.isRefresh)?{

????????//?是下拉刷新

????????_controller.refreshFailed();

??????}?else?{

????????_controller.loadFailed();

??????}

??????err();

????}

??}

}

?lottie 是一個(gè)loading組件,這里就不做介紹了,有興趣的可以單獨(dú)了解一下。

使用如下:

包含了下拉刷新的header樣式以及上拉加載的footer樣式,并對(duì)列表加載成功、失敗、數(shù)據(jù)全部加載完畢做了處理。

setStatus方法是對(duì)接口返回的數(shù)據(jù)做相應(yīng)的處理,接受的參數(shù)中,res是接口返回的數(shù)據(jù),更加res['code']來判斷是否成功或失敗,_controller是下拉組件的控制器,根據(jù)它來判斷當(dāng)前操作是上拉還是下拉,(_controller.isLoading | _controller.isRefresh)。

footer方法傳入一個(gè)len參數(shù),此參數(shù)是列表的長度,用來判斷是顯示‘暫無數(shù)據(jù)’還是列表數(shù)據(jù)以加載完全部。

fail方法返回失敗布局樣式,nodata方法返回暫無數(shù)據(jù)布局樣式。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開了第一次的黨會(huì),身份的轉(zhuǎn)變要...
    余生動(dòng)聽閱讀 10,916評(píng)論 0 11
  • 彩排完,天已黑
    劉凱書法閱讀 4,499評(píng)論 1 3
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,919評(píng)論 2 7

友情鏈接更多精彩內(nèi)容