Skip to content

Commit 02f5416

Browse files
committed
优化上下拉刷新
1 parent a8e6b58 commit 02f5416

File tree

5 files changed

+93
-14
lines changed

5 files changed

+93
-14
lines changed

lib/page/push_detail_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class _PushDetailPageState extends State<PushDetailPage> with AutomaticKeepAlive
5959
setState(() {
6060
pushHeaderViewModel = PushHeaderViewModel.forMap(pushCommit);
6161
pullLoadWidgetControl.dataList.addAll(pushCommit.files);
62-
pullLoadWidgetControl.needLoadMore = false;
62+
pullLoadWidgetControl.needLoadMore.value = false;
6363
titleOptionControl.url = pushCommit.htmlUrl;
6464
});
6565
}

lib/widget/gsy_list_state.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,30 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
1616

1717
int page = 1;
1818

19+
bool isRefreshing = false;
20+
21+
bool isLoadMoring = false;
22+
1923
final List dataList = new List();
2024

2125
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
2226

2327
final GlobalKey<RefreshIndicatorState> refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
2428

29+
_lockToAwait() async {
30+
///if loading, lock to await
31+
doDelayed() async {
32+
await Future.delayed(Duration(seconds: 1)).then((_) async {
33+
if (isLoading) {
34+
return await doDelayed();
35+
} else {
36+
return null;
37+
}
38+
});
39+
}
40+
await doDelayed();
41+
}
42+
2543
showRefreshLoading() {
2644
new Future.delayed(const Duration(seconds: 0), () {
2745
refreshIndicatorKey.currentState.show().then((e) {});
@@ -44,9 +62,13 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
4462
@protected
4563
Future<Null> handleRefresh() async {
4664
if (isLoading) {
47-
return null;
65+
if(isRefreshing) {
66+
return null;
67+
}
68+
await _lockToAwait();
4869
}
4970
isLoading = true;
71+
isRefreshing = true;
5072
page = 1;
5173
var res = await requestRefresh();
5274
resolveRefreshResult(res);
@@ -57,15 +79,20 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
5779
resolveDataResult(resNext);
5880
}
5981
isLoading = false;
82+
isRefreshing = false;
6083
return null;
6184
}
6285

6386
@protected
6487
Future<Null> onLoadMore() async {
6588
if (isLoading) {
66-
return null;
89+
if(isLoadMoring) {
90+
return null;
91+
}
92+
await _lockToAwait();
6793
}
6894
isLoading = true;
95+
isLoadMoring = true;
6996
page++;
7097
var res = await requestLoadMore();
7198
if (res != null && res.result) {
@@ -77,14 +104,15 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
77104
}
78105
resolveDataResult(res);
79106
isLoading = false;
107+
isLoadMoring = false;
80108
return null;
81109
}
82110

83111
@protected
84112
resolveDataResult(res) {
85113
if (isShow) {
86114
setState(() {
87-
pullLoadWidgetControl.needLoadMore = (res != null && res.data != null && res.data.length == Config.PAGE_SIZE);
115+
pullLoadWidgetControl.needLoadMore.value = (res != null && res.data != null && res.data.length == Config.PAGE_SIZE);
88116
});
89117
}
90118
}

lib/widget/gsy_pull_load_widget.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
4242

4343
@override
4444
void initState() {
45+
this.control.needLoadMore?.addListener((){
46+
///延迟两秒等待确认
47+
try {
48+
Future.delayed(Duration(seconds: 2), (){
49+
// ignore: invalid_use_of_visible_for_testing_member, invalid_use_of_protected_member
50+
_scrollController.notifyListeners();
51+
});
52+
} catch(e) {
53+
print(e);
54+
}
55+
});
4556
///增加滑动监听
4657
_scrollController.addListener(() {
4758
///判断当前滑动位置是不是到达底部,触发加载更多回调
4859
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
49-
if (this.control.needLoadMore) {
60+
if (this.control.needLoadMore.value) {
5061
this.onLoadMore?.call();
5162
}
5263
}
@@ -139,7 +150,7 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
139150
///上拉加载更多
140151
Widget _buildProgressIndicator() {
141152
///是否需要显示上拉加载更多的loading
142-
Widget bottomWidget = (control.needLoadMore)
153+
Widget bottomWidget = (control.needLoadMore.value)
143154
? new Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
144155
///loading框
145156
new SpinKitRotatingCircle(color: Theme.of(context).primaryColor),
@@ -174,7 +185,7 @@ class GSYPullLoadWidgetControl {
174185
List dataList = new List();
175186

176187
///是否需要加载更多
177-
bool needLoadMore = true;
188+
ValueNotifier<bool> needLoadMore = new ValueNotifier(false);
178189

179190
///是否需要头部
180191
bool needHeader = false;

lib/widget/gsy_pull_new_load_widget.dart

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ class GSYPullLoadWidget extends StatefulWidget {
2727
}
2828

2929
class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
30-
3130
final ScrollController _scrollController = new ScrollController();
3231

32+
bool isRefreshing = false;
33+
34+
bool isLoadMoring = false;
35+
3336
@override
3437
void initState() {
3538
///增加滑动监听
@@ -43,6 +46,14 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
4346
});
4447
widget.control.addListener(() {
4548
setState(() {});
49+
try {
50+
Future.delayed(Duration(seconds: 2), () {
51+
// ignore: invalid_use_of_visible_for_testing_member, invalid_use_of_protected_member
52+
_scrollController.notifyListeners();
53+
});
54+
} catch (e) {
55+
print(e);
56+
}
4657
});
4758
super.initState();
4859
}
@@ -84,24 +95,53 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
8495
}
8596
}
8697

98+
_lockToAwait() async {
99+
///if loading, lock to await
100+
doDelayed() async {
101+
await Future.delayed(Duration(seconds: 1)).then((_) async {
102+
if (widget.control.isLoading) {
103+
return await doDelayed();
104+
} else {
105+
return null;
106+
}
107+
});
108+
}
109+
110+
await doDelayed();
111+
}
112+
87113
@protected
88114
Future<Null> handleRefresh() async {
89115
if (widget.control.isLoading) {
90-
return null;
116+
if (isRefreshing) {
117+
return null;
118+
}
119+
120+
///if loading, lock to await
121+
await _lockToAwait();
91122
}
92123
widget.control.isLoading = true;
124+
isRefreshing = true;
93125
await widget.onRefresh?.call();
126+
isRefreshing = false;
94127
widget.control.isLoading = false;
95128
return null;
96129
}
97130

98131
@protected
99132
Future<Null> handleLoadMore() async {
100133
if (widget.control.isLoading) {
101-
return null;
134+
if (isLoadMoring) {
135+
return null;
136+
}
137+
138+
///if loading, lock to await
139+
await _lockToAwait();
102140
}
141+
isLoadMoring = true;
103142
widget.control.isLoading = true;
104143
await widget.onLoadMore?.call();
144+
isLoadMoring = false;
105145
widget.control.isLoading = false;
106146
return null;
107147
}
@@ -192,14 +232,14 @@ class GSYPullLoadWidgetControl extends ChangeNotifier {
192232

193233
set dataList(List value) {
194234
_dataList.clear();
195-
if(value != null) {
235+
if (value != null) {
196236
_dataList.addAll(value);
197237
notifyListeners();
198238
}
199239
}
200240

201241
addList(List value) {
202-
if(value != null) {
242+
if (value != null) {
203243
_dataList.addAll(value);
204244
notifyListeners();
205245
}

lib/widget/nested/gsy_nested_pull_load_widget.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class _GSYNestedPullLoadWidgetState extends State<GSYNestedPullLoadWidget> {
116116
onNotification: (ScrollNotification p){
117117
if (p.metrics.pixels >=
118118
p.metrics.maxScrollExtent) {
119-
if (this.control.needLoadMore) {
119+
if (this.control.needLoadMore.value) {
120120
this.onLoadMore?.call();
121121
}
122122
}
@@ -163,7 +163,7 @@ class _GSYNestedPullLoadWidgetState extends State<GSYNestedPullLoadWidget> {
163163
///上拉加载更多
164164
Widget _buildProgressIndicator() {
165165
///是否需要显示上拉加载更多的loading
166-
Widget bottomWidget = (control.needLoadMore)
166+
Widget bottomWidget = (control.needLoadMore.value)
167167
? new Row(
168168
mainAxisAlignment: MainAxisAlignment.center,
169169
children: <Widget>[

0 commit comments

Comments
 (0)