Repository navigation
Expand file tree
/
Copy pathGSYListState.dart
More file actions
141 lines (120 loc) · 3.07 KB
/
Copy pathGSYListState.dart
File metadata and controls
141 lines (120 loc) · 3.07 KB
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:gsy_github_app_flutter/common/config/Config.dart';
import 'package:gsy_github_app_flutter/widget/GSYPullLoadWidget.dart';
/**
* 上下拉刷新列表的通用State
* Created by guoshuyu
* Date: 2018-07-20
*/
// ignore: mixin_inherits_from_not_object
abstract class GSYListState<T extends StatefulWidget> extends State<T> with AutomaticKeepAliveClientMixin<T> {
bool isShow = false;
bool isLoading = false;
int page = 1;
final List dataList = new List();
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
final GlobalKey<RefreshIndicatorState> refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
showRefreshLoading() {
new Future.delayed(const Duration(seconds: 0), () {
refreshIndicatorKey.currentState.show().then((e) {});
return true;
});
}
@protected
resolveRefreshResult(res) {
if (res != null && res.result) {
pullLoadWidgetControl.dataList.clear();
if (isShow) {
setState(() {
pullLoadWidgetControl.dataList.addAll(res.data);
});
}
}
}
@protected
Future<Null> handleRefresh() async {
if (isLoading) {
return null;
}
isLoading = true;
page = 1;
var res = await requestRefresh();
resolveRefreshResult(res);
resolveDataResult(res);
if (res.next != null) {
var resNext = await res.next;
resolveRefreshResult(resNext);
resolveDataResult(resNext);
}
isLoading = false;
return null;
}
@protected
Future<Null> onLoadMore() async {
if (isLoading) {
return null;
}
isLoading = true;
page++;
var res = await requestLoadMore();
if (res != null && res.result) {
if (isShow) {
setState(() {
pullLoadWidgetControl.dataList.addAll(res.data);
});
}
}
resolveDataResult(res);
isLoading = false;
return null;
}
@protected
resolveDataResult(res) {
if (isShow) {
setState(() {
pullLoadWidgetControl.needLoadMore = (res != null && res.data != null && res.data.length == Config.PAGE_SIZE);
});
}
}
@protected
clearData() {
if (isShow) {
setState(() {
pullLoadWidgetControl.dataList.clear();
});
}
}
///下拉刷新数据
@protected
requestRefresh() async {}
///上拉更多请求数据
@protected
requestLoadMore() async {}
///是否需要第一次进入自动刷新
@protected
bool get isRefreshFirst;
///是否需要头部
@protected
bool get needHeader => false;
///是否需要保持
@override
bool get wantKeepAlive => true;
List get getDataList => dataList;
@override
void initState() {
isShow = true;
super.initState();
pullLoadWidgetControl.needHeader = needHeader;
pullLoadWidgetControl.dataList = getDataList;
if (pullLoadWidgetControl.dataList.length == 0 && isRefreshFirst) {
showRefreshLoading();
}
}
@override
void dispose() {
isShow = false;
isLoading = false;
super.dispose();
}
}