美文网首页
Flutter - ListView3 - easy_refre

Flutter - ListView3 - easy_refre

作者: 西半球_ | 来源:发表于2020-02-12 17:59 被阅读0次
    demo 地址: https://github.com/iotjin/jh_flutter_demo

    easy_refresh git地址

    效果图

    下拉刷新.png 上拉加载.png

    引入和本地化处理

    yaml

      flutter_localizations:
        sdk: flutter
      flutter_easyrefresh: ^2.0.9
    
    

    main

    //main导入
     import 'package:flutter_easyrefresh/easy_refresh.dart';
     import 'package:flutter_localizations/flutter_localizations.dart';
    
    //MaterialApp
      localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalEasyRefreshLocalizations.delegate
            ],
            supportedLocales: [ Locale('zh', 'CN')],
    
    

    ListViewTest_SimplePullDown 代码 (for循环造假数据)

    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:flutter_easyrefresh/easy_refresh.dart';
    
    var dataArr;
    var pageIndex =0;//页数
    var count =10;//每页10条
    
    void getNewData(){
      pageIndex =0;
      dataArr = new List();
      for(int i = pageIndex*count; i < count; i++){
        dataArr.add(i);
      }
      print(dataArr);
    }
    
    void getMoreData(){
      pageIndex++;
      for(int i = pageIndex*count; i <pageIndex*count+count; i++){
        dataArr.add(i);
      }
      print(dataArr);
    }
    
    
    class ListViewTest_SimplePullDown extends StatefulWidget {
      @override
      _ListViewTest_SimplePullDownState createState() => _ListViewTest_SimplePullDownState();
    }
    
    class _ListViewTest_SimplePullDownState extends State<ListViewTest_SimplePullDown> {
    
      EasyRefreshController _controller = EasyRefreshController();
    
        @override
        void initState() {
          super.initState();
        }
        int _count =0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar:AppBar(
              title:Text('ListViewTest_SimplePullDown')
          ),
          body:EasyRefresh(
            controller: _controller,
            firstRefresh: true,
            child:
            ListView.builder(
                itemCount: _count,
                itemExtent: 50.0, //强制高度为50.0
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(title: Text("$index"));
                }
            ),
    
            onRefresh: () async{
    
              await Future.delayed(Duration(seconds: 2), () {
                print("下拉刷新-----");
                getNewData();
                setState(() {
                  _count =dataArr.length;
                  print("最新条数"+_count.toString());
                  _controller.resetLoadState();
                });
              });
    
    
            },
            onLoad: () async {
             await Future.delayed(Duration(seconds: 2), () {
              print("上拉加载-----");
              getMoreData();
              setState(() {
                _count =dataArr.length;
                print("加载更多条数"+_count.toString());
              });
              _controller.finishLoad(noMore: _count >= 30);
             });
            },
          )
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Flutter - ListView3 - easy_refre

          本文链接:https://www.haomeiwen.com/subject/lmpcfhtx.html