博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
flutter-实现一个下拉刷新上拉加载的列表
阅读量:4056 次
发布时间:2019-05-25

本文共 2591 字,大约阅读时间需要 8 分钟。

1.下拉刷新:s使用自带的控件实现

new RefreshIndicator(        onRefresh: onRefresh,        child: ListView.builder(          physics: new AlwaysScrollableScrollPhysics(),          itemBuilder: getItemView,          itemCount: isShowLoadmore ? datas.length + 1 : datas.length,          controller: scrollController,        ),      ),

 

2.上拉加载:监听listview滚动到底部的时间,如果滚动到底部,设置listview的长度是length+1,在获取子view的方法中,处理加载中显示的view。

给listview添加滚动到底部的监听器

ScrollController scrollController = ScrollController();  @override  void initState() {    // TODO: implement initState    super.initState();    //对上拉加载更多的处理    scrollController.addListener(() {      if (scrollController.position.pixels >=          scrollController.position.maxScrollExtent - 10) {        if (!isShowLoadmore) {          //滚动到底部          setState(() {            isShowLoadmore = true;            getDataByIndex(index + 1);          });        }      }    });    getDataByIndex(0);  }

2.对listview的getItemview进行处理

//渲染子view  Widget getItemView(BuildContext context, int positon) {    if (positon >= datas.length) {      if (isShowLoadmore) {        return new Container(          height: 50,          child: new Center(            child: new Text("下拉加载更多..."),          ),        );      }      return null;    }    return new GestureDetector(      child: new Container(        decoration: new BoxDecoration(            border: Border.all(color: Colors.greenAccent, width: 2)),        padding: EdgeInsets.only(left: 10, top: 20, bottom: 20),        child: new Text(datas[positon]),      ),    );  }

3.加载数据和onfresh的处理

Future
getDataByIndex(needLoadIndex) async { if (isLoading) { setState(() { isLoading = false; isShowLoadmore = false; }); return; } setState(() { isLoading = true; }); await Future.delayed(Duration(seconds: 4), () { setState(() { index = needLoadIndex; for (int i = 0; i < 10; i++) { datas.add("第" + index.toString() + "页第" + i.toString() + "条数据"); } isLoading = false; isShowLoadmore = false; datas = datas; }); }); }

 

Future
onRefresh() async { if (isLoading) { setState(() { isLoading = false; isShowLoadmore = false; }); return; } await Future.delayed(Duration(seconds: 3), () { setState(() { index = 0; isLoading = false; if (index == 0) { datas.clear(); } for (int i = 0; i < 10; i++) { datas.add("第" + index.toString() + "页第" + i.toString() + "条数据"); } datas = datas; }); }); }

 

转载地址:http://bxeci.baihongyu.com/

你可能感兴趣的文章
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>
mongodb 命令
查看>>
MongoDB基本使用
查看>>
mongodb管理与安全认证
查看>>
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>
MongoDB 数据文件备份与恢复
查看>>
数据库索引介绍及使用
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>