1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:keframe/size_cache_widget.dart';
class CacheTableList extends StatefulWidget { List tableList; double height; ScrollController controller; String emptyMessage; String noMoreMessage; Function onRefresh; Function createList; Function getMoreData; int pageCount;
CacheTableList( {Key key, @required this.createList, this.tableList, this.onRefresh, this.controller, this.height, this.emptyMessage, this.noMoreMessage, this.pageCount, this.getMoreData}) : assert(getMoreData != null || pageCount != null), assert(pageCount == null || tableList != null), assert(pageCount != null || tableList == null), super(key: key); @override _CacheTableListState createState() => _CacheTableListState(); }
class _CacheTableListState extends State<CacheTableList> { List orginalTableList = []; List _tableList = []; int _pageCount = 0; bool _hasMore = true; double _height; ScrollController _controller;
@override void initState() { super.initState(); orginalTableList = widget.tableList ?? []; _controller = widget.controller ?? new ScrollController(); if (widget.getMoreData != null || widget.pageCount != 0) { _controller.addListener(() { if (_controller.position.pixels == _controller.position.maxScrollExtent) { _getMoreData(); } }); } _height = widget.height ?? 300; }
@override void dispose() { super.dispose(); _controller.removeListener(() {}); }
@override Widget build(BuildContext context) { return Container( height: _height, decoration: new BoxDecoration(color: Colors.white), child: SizeCacheWidget( estimateCount: _tableList.length, child: widget.onRefresh != null ? RefreshIndicator( onRefresh: _onRefresh, displacement: 10, color: Colors.white, backgroundColor: Colors.blueAccent, notificationPredicate: defaultScrollNotificationPredicate, child: Scrollbar( child: _createList(), ), ) : Scrollbar( child: _createList(), ), ), ); }
Widget _createList() { if (_tableList.isEmpty && _pageCount == 0) { _getMoreData(); return CupertinoActivityIndicator(); } return _tableList.isEmpty ? Container( height: _height - 20, alignment: Alignment.topCenter, margin: EdgeInsets.only(top: 20), child: Text( widget.emptyMessage ?? '暂无数据', style: TextStyle(fontSize: 15, color: Colors.black38, fontWeight: FontWeight.bold), ), ) : ListView.builder( controller: _controller, cacheExtent: 1000, itemCount: _tableList.length, itemBuilder: (_, i) { if (i == _tableList.length - 1 && _hasMore) { return Padding( padding: EdgeInsets.all(10.0), child: Center( child: CupertinoActivityIndicator(), ), ); } return widget.createList(i); }, ); }
void _getMoreData() async { if (!_hasMore) { EasyLoading.showToast(widget.noMoreMessage ?? '已经到底了'); return; } if (widget.getMoreData != null) { _pageCount = -1; var tempData = await widget.getMoreData(); if (tempData == null) { _hasMore = false; } else { _tableList = tempData; } setState(() {}); return; } int nowLength = orginalTableList.length; if (_pageCount <= nowLength) { _pageCount += widget.pageCount; Future.delayed(Duration(microseconds: 500)).then((e) { setState(() { if (_pageCount <= nowLength) { _tableList = orginalTableList.sublist(0, _pageCount); } else { _tableList = orginalTableList; _hasMore = false; } }); }); } }
Future _onRefresh() { return Future.sync(() async { _tableList = await widget.onRefresh(); _pageCount = 0; _hasMore = true; setState(() {}); }); } }
|