@@ -17,26 +17,16 @@ class GSYPullLoadWidget extends StatefulWidget {
1717 ///控制器,比如数据和一些配置
1818 final GSYPullLoadWidgetControl control;
1919
20+ ///刷新key
2021 final Key refreshKey;
2122
2223 GSYPullLoadWidget (this .control, this .itemBuilder, this .onRefresh, this .onLoadMore, {this .refreshKey});
2324
2425 @override
25- _GSYPullLoadWidgetState createState () => _GSYPullLoadWidgetState (this .control, this .itemBuilder, this .onRefresh, this .onLoadMore, this .refreshKey );
26+ _GSYPullLoadWidgetState createState () => _GSYPullLoadWidgetState ();
2627}
2728
2829class _GSYPullLoadWidgetState extends State <GSYPullLoadWidget > {
29- final IndexedWidgetBuilder itemBuilder;
30-
31- final RefreshCallback onLoadMore;
32-
33- final RefreshCallback onRefresh;
34-
35- final Key refreshKey;
36-
37- GSYPullLoadWidgetControl control;
38-
39- _GSYPullLoadWidgetState (this .control, this .itemBuilder, this .onRefresh, this .onLoadMore, this .refreshKey);
4030
4131 final ScrollController _scrollController = new ScrollController ();
4232
@@ -46,12 +36,12 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
4636 _scrollController.addListener (() {
4737 ///判断当前滑动位置是不是到达底部,触发加载更多回调
4838 if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
49- if (this .control.needLoadMore) {
50- this .onLoadMore ? . call ();
39+ if (widget .control.needLoadMore) {
40+ handleLoadMore ();
5141 }
5242 }
5343 });
54- control.addListener (() {
44+ widget. control.addListener (() {
5545 setState (() {});
5646 });
5747 super .initState ();
@@ -62,46 +52,68 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
6252 ///比如多个头部,是否需要空页面,是否需要显示加载更多。
6353 _getListCount () {
6454 ///是否需要头部
65- if (control.needHeader) {
55+ if (widget. control.needHeader) {
6656 ///如果需要头部,用Item 0 的 Widget 作为ListView的头部
6757 ///列表数量大于0时,因为头部和底部加载更多选项,需要对列表数据总数+2
68- return (control.dataList.length > 0 ) ? control.dataList.length + 2 : control.dataList.length + 1 ;
58+ return (widget. control.dataList.length > 0 ) ? widget. control.dataList.length + 2 : widget. control.dataList.length + 1 ;
6959 } else {
7060 ///如果不需要头部,在没有数据时,固定返回数量1用于空页面呈现
71- if (control.dataList.length == 0 ) {
61+ if (widget. control.dataList.length == 0 ) {
7262 return 1 ;
7363 }
7464
7565 ///如果有数据,因为部加载更多选项,需要对列表数据总数+1
76- return (control.dataList.length > 0 ) ? control.dataList.length + 1 : control.dataList.length;
66+ return (widget. control.dataList.length > 0 ) ? widget. control.dataList.length + 1 : widget. control.dataList.length;
7767 }
7868 }
7969
8070 ///根据配置状态返回实际列表渲染Item
8171 _getItem (int index) {
82- if (! control.needHeader && index == control.dataList.length && control.dataList.length != 0 ) {
72+ if (! widget. control.needHeader && index == widget. control.dataList.length && widget. control.dataList.length != 0 ) {
8373 ///如果不需要头部,并且数据不为0,当index等于数据长度时,渲染加载更多Item(因为index是从0开始)
8474 return _buildProgressIndicator ();
85- } else if (control.needHeader && index == _getListCount () - 1 && control.dataList.length != 0 ) {
75+ } else if (widget. control.needHeader && index == _getListCount () - 1 && widget. control.dataList.length != 0 ) {
8676 ///如果需要头部,并且数据不为0,当index等于实际渲染长度 - 1时,渲染加载更多Item(因为index是从0开始)
8777 return _buildProgressIndicator ();
88- } else if (! control.needHeader && control.dataList.length == 0 ) {
78+ } else if (! widget. control.needHeader && widget. control.dataList.length == 0 ) {
8979 ///如果不需要头部,并且数据为0,渲染空页面
9080 return _buildEmpty ();
9181 } else {
9282 ///回调外部正常渲染Item,如果这里有需要,可以直接返回相对位置的index
93- return itemBuilder (context, index);
83+ return widget.itemBuilder (context, index);
84+ }
85+ }
86+
87+ @protected
88+ Future <Null > handleRefresh () async {
89+ if (widget.control.isLoading) {
90+ return null ;
9491 }
92+ widget.control.isLoading = true ;
93+ await widget.onRefresh? .call ();
94+ widget.control.isLoading = false ;
95+ return null ;
96+ }
97+
98+ @protected
99+ Future <Null > handleLoadMore () async {
100+ if (widget.control.isLoading) {
101+ return null ;
102+ }
103+ widget.control.isLoading = true ;
104+ await widget.onLoadMore? .call ();
105+ widget.control.isLoading = false ;
106+ return null ;
95107 }
96108
97109 @override
98110 Widget build (BuildContext context) {
99111 return new RefreshIndicator (
100112 ///GlobalKey,用户外部获取RefreshIndicator的State,做显示刷新
101- key: refreshKey,
113+ key: widget. refreshKey,
102114
103115 ///下拉刷新触发,返回的是一个Future
104- onRefresh: onRefresh ,
116+ onRefresh: handleRefresh ,
105117 child: new ListView .builder (
106118 ///保持ListView任何情况都能滚动,解决在RefreshIndicator的兼容问题。
107119 physics: const AlwaysScrollableScrollPhysics (),
@@ -142,7 +154,7 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
142154 ///上拉加载更多
143155 Widget _buildProgressIndicator () {
144156 ///是否需要显示上拉加载更多的loading
145- Widget bottomWidget = (control.needLoadMore)
157+ Widget bottomWidget = (widget. control.needLoadMore)
146158 ? new Row (mainAxisAlignment: MainAxisAlignment .center, children: < Widget > [
147159 ///loading框
148160 new SpinKitRotatingCircle (color: Theme .of (context).primaryColor),
@@ -208,4 +220,7 @@ class GSYPullLoadWidgetControl extends ChangeNotifier {
208220 }
209221
210222 get needHeader => _needHeader;
223+
224+ ///是否加载中
225+ bool isLoading = false ;
211226}
0 commit comments