-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PullToRefreshList.dart
308 lines (273 loc) 路 9.29 KB
/
PullToRefreshList.dart
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
import 'dart:io';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:fyx/model/MainRepository.dart';
import 'package:fyx/theme/L.dart';
import 'package:fyx/theme/T.dart';
// ignore: must_be_immutable
class PullToRefreshList extends StatefulWidget {
final TDataProvider dataProvider;
Function sliverListBuilder;
bool _disabled;
bool _isInfinite;
int _rebuild;
final Widget pinnedWidget;
PullToRefreshList({@required this.dataProvider, isInfinite = false, int rebuild = 0, this.sliverListBuilder, bool disabled = false, this.pinnedWidget})
: _isInfinite = isInfinite,
_rebuild = rebuild,
_disabled = disabled,
assert(dataProvider != null);
@override
_PullToRefreshListState createState() => _PullToRefreshListState();
}
class _PullToRefreshListState extends State<PullToRefreshList> {
ScrollController _controller = ScrollController();
bool _isLoading = true;
bool _hasPulledDown = false;
bool _hasError = false;
DataProviderResult _result;
int _lastId;
int _prevLastId; // ID of last item loaded previously.
var _slivers = <Widget>[];
int _lastRebuild = 0;
@override
void setState(fn) {
if (mounted) {
super.setState(fn);
}
}
@override
void initState() {
super.initState();
if (widget._isInfinite) {
_controller.addListener(() {
// TODO: Refactor, use ScrollNotification ?
// Display loading and load next page if we are at the end of the list
if (_controller.position.userScrollDirection == ScrollDirection.reverse && _controller.position.outOfRange) {
if (_slivers.last is! SliverPadding) {
setState(() => _slivers.add(SliverPadding(padding: EdgeInsets.symmetric(vertical: 16), sliver: SliverToBoxAdapter(child: CupertinoActivityIndicator()))));
this.loadData(append: true);
}
}
});
}
// Add the refresh control on first position
_slivers.add(CupertinoSliverRefreshControl(
builder: Platform.isIOS ? CupertinoSliverRefreshControl.buildRefreshIndicator : buildAndroidRefreshIndicator,
onRefresh: () {
setState(() => _hasPulledDown = true);
if (!widget._disabled) {
return this.loadData();
}
return Future.wait([]);
},
));
this.loadData();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget._rebuild > _lastRebuild && !_isLoading) {
setState(() => _lastRebuild = widget._rebuild);
this.loadData();
}
if (_hasError) {
return T.feedbackScreen(isLoading: _isLoading, onPress: loadData, label: L.GENERAL_REFRESH);
}
if (_slivers.length == 1 && !_isLoading) {
return Container(
height: double.infinity,
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(),
Text(
L.GENERAL_EMPTY,
textAlign: TextAlign.center,
),
Image.asset('travolta.gif')
],
),
);
}
return CupertinoScrollbar(
child: Stack(
children: [
Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: CustomScrollView(
physics: Platform.isIOS ? const AlwaysScrollableScrollPhysics() : const RefreshScrollPhysics(),
slivers: _slivers,
controller: _controller,
),
),
],
),
Visibility(
visible: _isLoading && !_hasPulledDown, // Show only when not pulling down the list
child: Positioned(
top: 0,
left: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: 1,
child: LinearProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
backgroundColor: T.COLOR_PRIMARY,
),
),
),
)
],
),
);
}
List<Widget> buildTheList(List _data) {
// If the list contains widgets
if (_data.first is Widget) {
if (widget.sliverListBuilder is Function) {
return <Widget>[widget.sliverListBuilder(_data)];
} else {
return [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => _data[i],
childCount: _data.length,
),
)
];
}
}
// If the list contains category headers
if (_data.first is Map && (_data.first as Map).containsKey('header')) {
List<Widget> _list = [];
_data.cast<Map>().forEach((block) {
_list.add(SliverStickyHeaderBuilder(
builder: (context, state) => block['header'],
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => block['items'][i],
childCount: block['items'].length,
),
),
));
});
return _list;
}
throw Exception('Data in the PullToRefresh must be instance of Widget or Map{header, items}');
}
loadData({bool append = false}) async {
setState(() => _isLoading = true);
if (!append) {
// Not using setState on purpose -> we don't want to rebuild the widget tree, it's not needed.
// TODO: See if this affects performance and if we can do it on other places too.
_lastId = null;
}
// If we try to append the data but the last ID
// from where we try to load the posts is same as the ID of
// last item ID loaded previously.
// Hide the loading indicator and stop.
if (append && _prevLastId == _lastId) {
_slivers.removeLast(); // Remove the loading indicator
setState(() => _isLoading = false);
return;
} else {
// Otherwise save the current last ID for the next loadData() to check
_prevLastId = _lastId;
}
try {
_result = await widget.dataProvider(append ? _lastId : null);
bool makeInactive = false;
// If the ID of the last ID is same as the ID of currently loaded last ID
// Make the list inactive (makeInactive = true)
if (_lastId != null && _result.lastId == _lastId) {
makeInactive = true;
if (append) {
// ... and if also appending, remove the loading indicator
_slivers.removeLast(); // Remove the loading indicator
}
}
// Load the data only if there are any data AND should not be inactive.
if (_result.data.length > 0 && !makeInactive) {
if (append) {
_slivers.removeLast(); // Remove the loading indicator
} else {
_slivers.removeRange(1, _slivers.length);
}
_slivers.addAll(this.buildTheList(_result.data));
setState(() => _hasError = false);
setState(() => _lastId = _result.lastId);
}
// Add the pinned widget only if the list is active
if (widget.pinnedWidget is Widget && !makeInactive) {
_slivers.insert(0, SliverToBoxAdapter(child: widget.pinnedWidget));
}
} catch (error) {
setState(() => _hasError = true);
print('[PullToRefresh error]: $error');
print(StackTrace.current);
MainRepository().sentry.captureException(exception: error);
} finally {
setState(() {
_hasPulledDown = false;
_isLoading = false;
});
}
}
Widget buildAndroidRefreshIndicator(
BuildContext context,
RefreshIndicatorMode refreshState,
double pulledExtent,
double refreshTriggerPullDistance,
double refreshIndicatorExtent,
) {
const Curve opacityCurve = const Interval(0.4, 0.8, curve: Curves.easeInOut);
return Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: refreshState == RefreshIndicatorMode.drag
? Opacity(
opacity: opacityCurve.transform(min(pulledExtent / refreshTriggerPullDistance, 1.0)),
child: const Icon(
Icons.arrow_downward,
color: CupertinoColors.inactiveGray,
size: 24.0,
),
)
: Opacity(
opacity: opacityCurve.transform(min(pulledExtent / refreshIndicatorExtent, 1.0)),
child: CircularProgressIndicator(strokeWidth: 2.0, valueColor: AlwaysStoppedAnimation<Color>(T.COLOR_PRIMARY)),
),
),
);
}
}
class RefreshScrollPhysics extends BouncingScrollPhysics {
const RefreshScrollPhysics({ScrollPhysics parent}) : super(parent: parent);
@override
RefreshScrollPhysics applyTo(ScrollPhysics ancestor) {
return RefreshScrollPhysics(parent: buildParent(ancestor));
}
@override
bool shouldAcceptUserOffset(ScrollMetrics position) {
return true;
}
}
class DataProviderResult {
final List data;
final dynamic lastId;
DataProviderResult(this.data, {this.lastId});
}
typedef Future<DataProviderResult> TDataProvider(int id);