Flutter ListView封装,下拉刷新、上拉加载更多…

2019-06-1513:38:26APP与小程序开发Comments3,051 views字数 2227阅读模式

封装了Flutter的ListView,只要传递请求数据的方法和绘制item的方法进去就可以绘制ListView,同时支持下拉刷新、上拉加载更多。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

主要三个文件:refresh_list_view.dart//封装的ListView,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

list_view_item.dart//ListView的子item渲染view文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

refersh_list_view_demo.dart//封装的ListView使用方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

github地址:github.com/damengzai/f…文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

1. refresh_list_view.dart

​ 可以下拉刷新、上拉加载更多的ListView文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

@override
Widget build(BuildContext context) {
return RefreshIndicator(
    child: ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        if (widget.listItemView is Function) {
          return widget.listItemView(index, items[index]);
        }
      },
      physics: const AlwaysScrollableScrollPhysics(),
      controller: _scrollController,
    ),
    onRefresh: _handleRefresh);
  }
复制代码

​ RefreshIndicator:一个Material风格的下拉刷新组件,包含一个可滚动的子组件,当子组件下拉是会有一个圆形刷新图标,当下拉距离足够的时候,会触发onRefresh,而在onRefresh中会去刷新数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

当子组件太短而不能滚动的时候,需要添加physics: const AlwaysScrollableScrollPhysics(),否则下拉刷新和上拉加载更多都无效,无法触发了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

​ ListView:itemBuilder用于渲染子item,controller添加一个ScrollController,用于响应滚动事件,监听滚动到底部,做加载更多处理,同时ScrollController可以跳转到指定位置,记住滚动位置等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

@override
void initState() {
  super.initState();
  _loadMore();
  _scrollController.addListener(() {
    if (_scrollController.position.pixels ==
        _scrollController.position.maxScrollExtent) {
      //滚动到最后请求更多
      _loadMore();
    }
  });
}
复制代码

​ 在初始换state中首先通过_loadMore()获取数据,同时使用_scrollController监听滚动滚动的位置,当滚动到最底部的时候,会触发加载更多数据的方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

//加载更多
Future _loadMore() async {
  if (!isLoading) {
    if(this.mounted) {
      setState(() {
        //通过setState去更新数据
        isLoading = true;
      });
    }
  }
  List moreList = await mockHttpRequest();
  if (this.mounted) {
    setState(() {
      items.addAll(moreList);
      isLoading = false;
    });
  }
}
复制代码

​ 加载更多的方法,模拟了一个网络请求,当有数据返回的时候,通过setState将数据更新回state中,实现UI的刷新。下拉刷新的方法类似,不做过多介绍。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

2. list_view_item.dart
@override
Widget build(BuildContext context) {
  return Card(
    color: Colors.white,
    elevation: 4.0,
    child: Padding(
      padding: const EdgeInsets.fromLTRB(8.0, 30.0, 8.0, 30.0),
      child: Text(
        this.title,
        style: new TextStyle(
          color: Colors.blue,
          fontSize: 20,
        ),),),);}
复制代码

​ 不做过多介绍,可以根据自己的需要定制UI,现在只显示了一个TextView。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

3.refresh_list_view_demo.dart
 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Column(
        children: <Widget>[
          new Expanded(
            child: RefreshListView(getListData, createListItem),
          ),],),);}}
复制代码

getListDatacreateListItem分别是获取数据和渲染子item的方法,传递方法进去(比JAVA灵活多了)自己定制更灵活。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

以上是一个下拉刷新、上拉加载更多的ListView的简单封装,后续持续优化,方便使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

作者:damengzai
链接:https://juejin.im/post/5cfdb6155188251bdc78ceaa
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13663.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/13663.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定