-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathloadmore_widget.dart
More file actions
470 lines (411 loc) · 12.6 KB
/
loadmore_widget.dart
File metadata and controls
470 lines (411 loc) · 12.6 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import 'dart:async';
import 'package:flutter/material.dart';
/// return true is refresh success
///
/// return false or null is fail
typedef Future<bool> FutureCallBack();
class LoadMore extends StatefulWidget {
static DelegateBuilder<LoadMoreDelegate> buildDelegate =
() => DefaultLoadMoreDelegate();
static DelegateBuilder<LoadMoreTextBuilder> buildTextBuilder =
() => DefaultLoadMoreTextBuilder.chinese;
/// Only support [ListView],[SliverList]
final Widget child;
/// return true is refresh success
///
/// return false or null is fail
final FutureCallBack onLoadMore;
/// if [isFinish] is true, then loadMoreWidget status is [LoadMoreStatus.nomore].
final bool isFinish;
/// see [LoadMoreDelegate]
final LoadMoreDelegate? delegate;
/// see [LoadMoreTextBuilder]
final LoadMoreTextBuilder? textBuilder;
/// when [whenEmptyLoad] is true, and when listView children length is 0,or the itemCount is 0,not build loadMoreWidget
final bool whenEmptyLoad;
const LoadMore({
Key? key,
required this.child,
required this.onLoadMore,
this.textBuilder,
this.isFinish = false,
this.delegate,
this.whenEmptyLoad = true,
}) : super(key: key);
@override
_LoadMoreState createState() => _LoadMoreState();
}
class _LoadMoreState extends State<LoadMore> {
Widget get child => widget.child;
LoadMoreDelegate get loadMoreDelegate =>
widget.delegate ?? LoadMore.buildDelegate();
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
if (child is ListView) {
return _buildListView(child as ListView) ?? Container();
}
if (child is SliverList) {
return _buildSliverList(child as SliverList);
}
return child;
}
/// if call the method, then the future is not null
/// so, return a listview and item count + 1
Widget? _buildListView(ListView listView) {
var delegate = listView.childrenDelegate;
outer:
if (delegate is SliverChildBuilderDelegate) {
SliverChildBuilderDelegate delegate =
listView.childrenDelegate as SliverChildBuilderDelegate;
if (!widget.whenEmptyLoad && delegate.estimatedChildCount == 0) {
break outer;
}
var viewCount = (delegate.estimatedChildCount ?? 0) + 1;
IndexedWidgetBuilder builder = (context, index) {
if (index == viewCount - 1) {
return _buildLoadMoreView();
}
return delegate.builder(context, index) ?? Container();
};
return ListView.builder(
itemBuilder: builder,
addAutomaticKeepAlives: delegate.addAutomaticKeepAlives,
addRepaintBoundaries: delegate.addRepaintBoundaries,
addSemanticIndexes: delegate.addSemanticIndexes,
dragStartBehavior: listView.dragStartBehavior,
semanticChildCount: listView.semanticChildCount,
itemCount: viewCount,
cacheExtent: listView.cacheExtent,
controller: listView.controller,
itemExtent: listView.itemExtent,
key: listView.key,
padding: listView.padding,
physics: listView.physics,
primary: listView.primary,
reverse: listView.reverse,
scrollDirection: listView.scrollDirection,
shrinkWrap: listView.shrinkWrap,
);
} else if (delegate is SliverChildListDelegate) {
SliverChildListDelegate delegate =
listView.childrenDelegate as SliverChildListDelegate;
if (!widget.whenEmptyLoad && delegate.estimatedChildCount == 0) {
break outer;
}
delegate.children.add(_buildLoadMoreView());
return ListView(
children: delegate.children,
addAutomaticKeepAlives: delegate.addAutomaticKeepAlives,
addRepaintBoundaries: delegate.addRepaintBoundaries,
cacheExtent: listView.cacheExtent,
controller: listView.controller,
itemExtent: listView.itemExtent,
key: listView.key,
padding: listView.padding,
physics: listView.physics,
primary: listView.primary,
reverse: listView.reverse,
scrollDirection: listView.scrollDirection,
shrinkWrap: listView.shrinkWrap,
addSemanticIndexes: delegate.addSemanticIndexes,
dragStartBehavior: listView.dragStartBehavior,
semanticChildCount: listView.semanticChildCount,
);
}
return listView;
}
Widget _buildSliverList(SliverList list) {
final delegate = list.delegate;
if (delegate is SliverChildListDelegate) {
return SliverList(
delegate: delegate,
);
}
outer:
if (delegate is SliverChildBuilderDelegate) {
if (!widget.whenEmptyLoad && delegate.estimatedChildCount == 0) {
break outer;
}
final viewCount = (delegate.estimatedChildCount ?? 0) + 1;
IndexedWidgetBuilder builder = (context, index) {
if (index == viewCount - 1) {
return _buildLoadMoreView();
}
return delegate.builder(context, index) ?? Container();
};
return SliverList(
delegate: SliverChildBuilderDelegate(
builder,
addAutomaticKeepAlives: delegate.addAutomaticKeepAlives,
addRepaintBoundaries: delegate.addRepaintBoundaries,
addSemanticIndexes: delegate.addSemanticIndexes,
childCount: viewCount,
semanticIndexCallback: delegate.semanticIndexCallback,
semanticIndexOffset: delegate.semanticIndexOffset,
),
);
}
outer:
if (delegate is SliverChildListDelegate) {
if (!widget.whenEmptyLoad && delegate.estimatedChildCount == 0) {
break outer;
}
delegate.children.add(_buildLoadMoreView());
return SliverList(
delegate: SliverChildListDelegate(
delegate.children,
addAutomaticKeepAlives: delegate.addAutomaticKeepAlives,
addRepaintBoundaries: delegate.addRepaintBoundaries,
addSemanticIndexes: delegate.addSemanticIndexes,
semanticIndexCallback: delegate.semanticIndexCallback,
semanticIndexOffset: delegate.semanticIndexOffset,
),
);
}
return list;
}
LoadMoreStatus status = LoadMoreStatus.idle;
Widget _buildLoadMoreView() {
if (widget.isFinish == true) {
this.status = LoadMoreStatus.nomore;
} else {
if (this.status == LoadMoreStatus.nomore) {
this.status = LoadMoreStatus.idle;
}
}
return NotificationListener<_RetryNotify>(
child: NotificationListener<_BuildNotify>(
child: DefaultLoadMoreView(
status: status,
delegate: loadMoreDelegate,
textBuilder: widget.textBuilder ?? LoadMore.buildTextBuilder(),
),
onNotification: _onLoadMoreBuild,
),
onNotification: _onRetry,
);
}
bool _onLoadMoreBuild(_BuildNotify notification) {
//判断状态,触发对应的操作
if (status == LoadMoreStatus.loading) {
return false;
}
if (status == LoadMoreStatus.nomore) {
return false;
}
if (status == LoadMoreStatus.fail) {
return false;
}
if (status == LoadMoreStatus.idle) {
// 切换状态为加载中,并且触发回调
loadMore();
}
return false;
}
void _updateStatus(LoadMoreStatus status) {
if (mounted) setState(() => this.status = status);
}
bool _onRetry(_RetryNotify notification) {
loadMore();
return false;
}
void loadMore() {
_updateStatus(LoadMoreStatus.loading);
widget.onLoadMore().then((v) {
if (v == true) {
// 成功,切换状态为空闲
_updateStatus(LoadMoreStatus.idle);
} else {
// 失败,切换状态为失败
_updateStatus(LoadMoreStatus.fail);
}
});
}
}
enum LoadMoreStatus {
/// 空闲中,表示当前等待加载
///
/// wait for loading
idle,
/// 刷新中,不应该继续加载,等待future返回
///
/// the view is loading
loading,
/// 刷新失败,刷新失败,这时需要点击才能刷新
///
/// loading fail, need tap view to loading
fail,
/// 没有更多,没有更多数据了,这个状态不触发任何条件
///
/// not have more data
nomore,
}
class DefaultLoadMoreView extends StatefulWidget {
final LoadMoreStatus status;
final LoadMoreDelegate delegate;
final LoadMoreTextBuilder textBuilder;
const DefaultLoadMoreView({
Key? key,
this.status = LoadMoreStatus.idle,
required this.delegate,
required this.textBuilder,
}) : super(key: key);
@override
DefaultLoadMoreViewState createState() => DefaultLoadMoreViewState();
}
const _defaultLoadMoreHeight = 80.0;
const _loadmoreIndicatorSize = 33.0;
const _loadMoreDelay = 16;
class DefaultLoadMoreViewState extends State<DefaultLoadMoreView> {
LoadMoreDelegate get delegate => widget.delegate;
@override
Widget build(BuildContext context) {
notify();
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
if (widget.status == LoadMoreStatus.fail ||
widget.status == LoadMoreStatus.idle) {
_RetryNotify().dispatch(context);
}
},
child: Container(
height: delegate.widgetHeight(widget.status),
alignment: Alignment.center,
child: delegate.buildChild(
context,
widget.status,
builder: widget.textBuilder,
),
),
);
}
void notify() async {
var delay = max(delegate.loadMoreDelay(), Duration(milliseconds: 16));
await Future.delayed(delay);
if (widget.status == LoadMoreStatus.idle) {
_BuildNotify().dispatch(context);
}
}
Duration max(Duration duration, Duration duration2) {
if (duration > duration2) {
return duration;
}
return duration2;
}
}
class _BuildNotify extends Notification {}
class _RetryNotify extends Notification {}
typedef T DelegateBuilder<T>();
/// loadmore widget properties
abstract class LoadMoreDelegate {
static DelegateBuilder<LoadMoreDelegate> buildWidget =
() => DefaultLoadMoreDelegate();
const LoadMoreDelegate();
/// the loadmore widget height
double widgetHeight(LoadMoreStatus status) => _defaultLoadMoreHeight;
/// build loadmore delay
Duration loadMoreDelay() => Duration(milliseconds: _loadMoreDelay);
Widget buildChild(
BuildContext context,
LoadMoreStatus status, {
LoadMoreTextBuilder builder = DefaultLoadMoreTextBuilder.chinese,
});
}
class DefaultLoadMoreDelegate extends LoadMoreDelegate {
const DefaultLoadMoreDelegate();
@override
Widget buildChild(
BuildContext context,
LoadMoreStatus status, {
LoadMoreTextBuilder builder = DefaultLoadMoreTextBuilder.chinese,
}) {
String text = builder(status);
if (status == LoadMoreStatus.fail) {
return Container(
child: Text(text),
);
}
if (status == LoadMoreStatus.idle) {
return Text(text);
}
if (status == LoadMoreStatus.loading) {
return Container(
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
width: _loadmoreIndicatorSize,
height: _loadmoreIndicatorSize,
child: CircularProgressIndicator(
backgroundColor: Colors.blue,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(text),
),
],
),
);
}
if (status == LoadMoreStatus.nomore) {
return Text(text);
}
return Text(text);
}
}
typedef String LoadMoreTextBuilder(LoadMoreStatus status);
String _buildChineseText(LoadMoreStatus status) {
String text;
switch (status) {
case LoadMoreStatus.fail:
text = "加载失败,请点击重试";
break;
case LoadMoreStatus.idle:
text = "等待加载更多";
break;
case LoadMoreStatus.loading:
text = "加载中,请稍候...";
break;
case LoadMoreStatus.nomore:
text = "到底了,别扯了";
break;
default:
text = "";
}
return text;
}
String _buildEnglishText(LoadMoreStatus status) {
String text;
switch (status) {
case LoadMoreStatus.fail:
text = "load fail, tap to retry";
break;
case LoadMoreStatus.idle:
text = "wait for loading";
break;
case LoadMoreStatus.loading:
text = "loading, wait for moment ...";
break;
case LoadMoreStatus.nomore:
text = "no more data";
break;
default:
text = "";
}
return text;
}
class DefaultLoadMoreTextBuilder {
static const LoadMoreTextBuilder chinese = _buildChineseText;
static const LoadMoreTextBuilder english = _buildEnglishText;
}