Flutter局部刷新組件

當(dāng)界面復(fù)雜,嵌套多個(gè)子widget時(shí),經(jīng)常有刷新單個(gè)或者局部widget的需求,每次寫業(yè)務(wù)時(shí)創(chuàng)建GlobalKey太繁瑣不夠優(yōu)雅,使用provider狀態(tài)控制類刷新可能功能又過(guò)重。習(xí)慣命令式刷新的同學(xué),可以參考此文

RefreshableStatefulWidget

abstract class RefreshableStatefulWidget extends StatefulWidget {
  RefreshableStatefulWidget({Key key})
      : super(key: key is GlobalKey ? key : GlobalKey());

  void reload() {
    if (key is! GlobalKey) {
      return;
    }
    final aKey = key as GlobalKey;
    aKey.currentState.setState(() {});
  }
}

RefreshableView

class RefreshableView extends RefreshableStatefulWidget {
  final Widget Function(BuildContext cntext) builder;

  RefreshableView({Key key, @required this.builder})
      : assert(builder != null),
        super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _RefreshableViewState(builder);
  }
}

class _RefreshableViewState extends State<RefreshableView> {
  final Widget Function(BuildContext cntext) builder;
  _RefreshableViewState(this.builder);

  @override
  Widget build(BuildContext context) {
    return builder(context);
  }
}

使用

  • 構(gòu)建
 _aRefreshableView = RefreshableView(builder: (BuildContext context) {
  return ListView.builder(
           itemCount: 10,
           itemBuilder: (BuildContext context, int index) {
           return Text("data$index");
         });
 });
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
        navigationBar: _navigationBar,
        child: Column(
          children: <Widget>[
            Text("data"),
            Image.asset("image"),
            _aRefreshableView
          ],
        ));
  }
  • 刷新ListView
await request(); 
_aRefreshableView.reload(); // 網(wǎng)絡(luò)請(qǐng)求回調(diào)后reload

推薦自維護(hù)的一個(gè)Flutter下載器及相關(guān)文章:
al_downloader
Flutter 下載器 al_downloader

最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 轉(zhuǎn)自 Q吹個(gè)大氣球Q 本文主要介紹了Flutter布局相關(guān)的內(nèi)容,對(duì)相關(guān)知識(shí)點(diǎn)進(jìn)行了梳理,并從實(shí)際例子觸發(fā),進(jìn)一步...
    chilim閱讀 2,004評(píng)論 0 17
  • 本文主要介紹了Flutter布局相關(guān)的內(nèi)容,對(duì)相關(guān)知識(shí)點(diǎn)進(jìn)行了梳理,并從實(shí)際例子觸發(fā),進(jìn)一步講解該如何去進(jìn)行布局。...
    Q吹個(gè)大氣球Q閱讀 10,205評(píng)論 6 51
  • 1. 簡(jiǎn)介 在介紹Flutter布局之前,我們得先了解Flutter中的一些布局相關(guān)的特性。 1.1 邊界約束(b...
    24K純城閱讀 557評(píng)論 0 0
  • 床上的老太太雙眼緊閉,呼吸微弱。搶救了幾日得知回天無(wú)力時(shí),已停止救治。想來(lái),在彌留之際,也是希望讓老太太少受一點(diǎn)罪...
    北漁說(shuō)閱讀 409評(píng)論 4 4
  • 當(dāng)冷漠為常態(tài),一切親情就會(huì)淡化。生活總是許多無(wú)奈,尋求根本改變的方法才是出路。一個(gè)家庭沒(méi)了生氣,也就沒(méi)了熱鬧。 初...
    牛一y閱讀 299評(píng)論 5 5

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