Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of pull-up and loading more drop-down refresh in FlutterListView

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容主要讲解"FlutterListView怎么实现上拉加载更多下拉刷新的功能",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"FlutterListView怎么实现上拉加载更多下拉刷新的功能"吧!

下拉刷新

跟原生开发一样,下拉刷新在flutter里提供的有组件实现 RefreshIndicator

一直不明白为啥组件中都提供下拉刷新,但就是没有上拉加载!!

我这请求接口数据用的是 http 库,是个第三方的是需要安装的 https://pub.dev/packages/http

用法如下

class MyHomePage extends StatefulWidget { MyHomePage({Key key}) : super(key: key); @override MyHomeWidget2 createState() => MyHomeWidget2();}class MyHomeWidget2 extends State { int page = 1; List data = new List(); var baseUrl = "https://cnodejs.org/api/v1"; @override void initState() { super.initState(); this._onRefresh(); } _fetchData() async { var response = await http.get( '${this.baseUrl}/topics?mdrender=false&limit=10&page=${this.page}'); var json = await convert.jsonDecode(response.body); return json['data']; } Future _onRefresh() { data.clear(); this.page = 1; return _fetchData().then((data) { setState(() => this.data.addAll(data)); }); } @override Widget build(BuildContext context) { return Scaffold( body: RefreshIndicator( // 在ListView外包一层 RefreshIndicator 组件 onRefresh: _onRefresh, // 添加onRefresh方法 child: ListView.separated( itemCount: this.data.length, itemBuilder: (context, index) { var _data = this.data[index]; return ListTile( leading: Image.network(_data["author"]["avatar_url"]), title: Text(_data["title"]), subtitle: Text(_data["author"]["loginname"] + " created at " + new DateTime.now().toString()), // 为了看每次数据变动,这里直接取当前时间 trailing: Icon(Icons.chevron_right)); }, separatorBuilder: (context, index) { return Divider(); }, ) )); }}

上拉加载

上拉加载原理还是一样的,给ListView加一个 ScrollController 组件,然后通过事件监听滚动条的高度来显示和隐藏加载更多的组件

先将加载更多的组件写好

Widget _loadMoreWidget() { return new Padding( padding: const EdgeInsets.all(15.0), // 外边距 child: new Center( child: new CircularProgressIndicator() ), );}

初始化一个 ScrollController 组件,将其设置给 ListView 组件的 controller 属性上

ScrollController _scrollController = new ScrollController();child: ListView.separated( controller: _scrollController, //...)

然后通过重写 dispost() 方法来处理加载更多组件的释放

@overridevoid dispose() { _scrollController.dispose(); super.dispose();}

最后通过数据源来控制界面渲染哪个组件,当数据源循环渲染的 index 跟数据源一样长时(其实少1,下标从0开始的)就渲染加载更多组件,让其显示出来,同时调用加载更多方法,获取数据,再通过state实现组件ui的更新

完整代码如下

class MyHomePage extends StatefulWidget { MyHomePage({Key key}) : super(key: key); @override MyHomeWidget2 createState() => MyHomeWidget2();}class MyHomeWidget2 extends State { int page = 1; bool isLoadmore = false; List data = new List(); var baseUrl = "https://cnodejs.org/api/v1"; ScrollController _scrollController = new ScrollController(); @override void initState() { super.initState(); this._onRefresh(); _scrollController.addListener(() { if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) { _onLoadmore(); } }); } _fetchData() async { var response = await http.get( '${this.baseUrl}/topics?mdrender=false&limit=10&page=${this.page}'); var json = await convert.jsonDecode(response.body); return json['data']; } Future _onRefresh() { data.clear(); this.page = 1; return _fetchData().then((data) { setState(() => this.data.addAll(data)); }); } Future _onLoadmore() { this.page++; return _fetchData().then((data) { setState((){ this.data.addAll(data); isLoadmore = false; }); }); } @override void dispose() { _scrollController.dispose(); super.dispose(); } Widget _loadMoreWidget() { return new Padding( padding: const EdgeInsets.all(15.0), child: new Center( child: new CircularProgressIndicator() ), ); } @override Widget build(BuildContext context) { return Scaffold( body: RefreshIndicator( onRefresh: _onRefresh, child: ListView.separated( controller: _scrollController, itemCount: this.data.length, itemBuilder: (context, index) { if (index == data.length - 1) { return _loadMoreWidget(); } else { var _data = this.data[index]; return ListTile( leading: Image.network(_data["author"]["avatar_url"]), title: Text(_data["title"]), subtitle: Text(_data["author"]["loginname"] + " created at " + new DateTime.now().toString()), trailing: Icon(Icons.chevron_right)); } }, separatorBuilder: (context, index) { return Divider(); }, ) )); }}到此,相信大家对"FlutterListView怎么实现上拉加载更多下拉刷新的功能"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report