Flutter开发之——下拉刷新

一 概述

本文介绍Flutter中的下拉刷新组件:

  • RefreshIndicator:Material风格的下拉刷新组件
  • CupertinoSliverRefreshControl: ios风格的下拉刷新控件
  • flutter_easyrefresh:第三方上拉刷新,下拉加载组件

二 RefreshIndicator

2.1 构造方法

class RefreshIndicator extends StatefulWidget {
  const RefreshIndicator({
    Key? key,
    required this.child,
    this.displacement = 40.0,
    required this.onRefresh,
    this.color,
    this.backgroundColor,
    this.notificationPredicate = defaultScrollNotificationPredicate,
    this.semanticsLabel,
    this.semanticsValue,
    this.strokeWidth = 2.0,
    this.triggerMode = RefreshIndicatorTriggerMode.onEdge,
  })
}  

2.2 常用属性说明

属性说明取值
child显示的数据试图通常是[ListView] or [CustomScrollView]
displacement指示器到顶部或者底部到距离double
color指示器的前置颜色Color
backgroundColor指示器的背景颜色Color

2.3 示例

代码
List<int> _list =[1, 2, 3,];
body: RefreshIndicator(
          color: Colors.red,
          backgroundColor: Colors.lightBlue,
          onRefresh: () {
            setState(() {
              _list.add(_list.length+1);
            });
            return Future.delayed(Duration(seconds:1));
          },
          child: ListView.separated(itemCount: _list.length, 
            separatorBuilder: (context,index){return Divider(height: 10,color: Colors.red,);} ,
            itemBuilder: (BuildContext context, int index){
            return Center(child: Text("数据${_list[index]}"),heightFactor: 1.5,);
          }, ),
        ) 
效果图

三 CupertinoSliverRefreshControl

3.1 用法说明

  • CupertinoSliverRefreshControl的用法和RefreshIndicator不同,CupertinoSliverRefreshControl需要放在CustomScrollView中
  • CustomScrollView中需要包含两个属性:sliver(滚动试图内容)和physics(滚动对象)
  • sliver:包含CupertinoSliverRefreshControl和SliverList两个视图组件,CupertinoSliverRefreshControl控制刷新,设置刷新组件的属性;SliverList设置显示的内容
  • physics:控制滚动类,默认为const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),不设置无法下拉滚动

3.2 示例

代码
List<int> _list =[1, 2, 3,];
CustomScrollView(
                 physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
                 slivers: <Widget>[
                    //const CupertinoSliverNavigationBar(largeTitle: Text('Scroll down')),
                    CupertinoSliverRefreshControl(
                      refreshTriggerPullDistance: 100.0,
                      refreshIndicatorExtent: 60.0,
                      onRefresh: () async {
                        await Future<void>.delayed(const Duration(milliseconds: 1000));
                        setState(() {
                          _list.add(_list.length+1);
                        });
                      },
                    ),
                    SliverList(delegate: SliverChildBuilderDelegate((BuildContext context, int index) =>
                        Center(child: Column(children: [Text("数据${_list[index]}"), Divider(height: 10,color: Colors.red,)],),),
                        childCount: _list.length,),),
                  ],
                )
效果图

四 flutter_easyrefresh

4.1 仓库地址

Github-flutter_easyrefresh:https://github.com/xuelongqy/flutter_easyrefresh

4.2 插件地址

flutter_easyrefresh 2.2.1:https://pub.flutter-io.cn/packages/flutter_easyrefresh

4.3 插件的安装及卸载

插件安装

打开CMD终端,执行如下指令(自动添加pubspec.yaml依赖)

flutter pub add flutter_easyrefresh
插件卸载

打开CMD终端,执行如下指令(pubspec.yaml依赖被删除)

flutter pub remove flutter_easyrefresh

4.4 示例(基本用法,更多用法请看文档)

代码
List<int> _list =[1, 2, 3,];
EasyRefresh(
          child: ListView.separated(itemCount: _list.length,
          separatorBuilder: (context,index){return Divider(height: 10,color: Colors.red,);} ,
          itemBuilder: (BuildContext context, int index){return Center(child: Text("数据${_list[index]}"),heightFactor: 1.5,);}, ),
          onRefresh: () async{
          await Future<void>.delayed(const Duration(milliseconds: 1000));
          setState(() {_list.add(_list.length+1);});
        },
        onLoad: () async{
          await Future<void>.delayed(const Duration(milliseconds: 1000));
          setState(() {_list.remove(_list.length);});
        },
        
效果图

五 参考

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值