Skip to content

Commit 12764a0

Browse files
author
Hans Muller
authored
RefreshIndicatorState.show() (flutter#4877)
1 parent 445f250 commit 12764a0

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

examples/flutter_gallery/lib/demo/overscroll_demo.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class OverscrollDemo extends StatefulWidget {
1818
}
1919

2020
class OverscrollDemoState extends State<OverscrollDemo> {
21+
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
22+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
2123
static final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
2224
static final List<String> _items = <String>[
2325
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
@@ -28,10 +30,19 @@ class OverscrollDemoState extends State<OverscrollDemo> {
2830
Future<Null> refresh() {
2931
Completer<Null> completer = new Completer<Null>();
3032
new Timer(new Duration(seconds: 3), () { completer.complete(null); });
31-
return completer.future;
33+
return completer.future.then((_) {
34+
_scaffoldKey.currentState.showSnackBar(new SnackBar(
35+
content: new Text("Refresh complete"),
36+
action: new SnackBarAction(
37+
label: 'RETRY',
38+
onPressed: () {
39+
_refreshIndicatorKey.currentState.show();
40+
}
41+
)
42+
));
43+
});
3244
}
3345

34-
3546
@override
3647
Widget build(BuildContext context) {
3748
String indicatorTypeText;
@@ -68,6 +79,7 @@ class OverscrollDemoState extends State<OverscrollDemo> {
6879
break;
6980
case IndicatorType.refresh:
7081
body = new RefreshIndicator(
82+
key: _refreshIndicatorKey,
7183
child: body,
7284
refresh: refresh,
7385
scrollableKey: _scrollableKey,
@@ -77,6 +89,7 @@ class OverscrollDemoState extends State<OverscrollDemo> {
7789
}
7890

7991
return new Scaffold(
92+
key: _scaffoldKey,
8093
appBar: new AppBar(
8194
title: new Text('$indicatorTypeText'),
8295
actions: <Widget>[

packages/flutter/lib/src/material/refresh_indicator.dart

+46-24
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ class RefreshIndicator extends StatefulWidget {
121121
final RefreshIndicatorLocation location;
122122

123123
@override
124-
_RefreshIndicatorState createState() => new _RefreshIndicatorState();
124+
RefreshIndicatorState createState() => new RefreshIndicatorState();
125125
}
126126

127-
class _RefreshIndicatorState extends State<RefreshIndicator> {
127+
/// Contains the state for a [RefreshIndicator]. This class can be used to
128+
/// programmatically show the refresh indicator, see the [show] method.
129+
class RefreshIndicatorState extends State<RefreshIndicator> {
128130
final AnimationController _sizeController = new AnimationController();
129131
final AnimationController _scaleController = new AnimationController();
130132
Animation<double> _sizeFactor;
@@ -280,36 +282,56 @@ class _RefreshIndicatorState extends State<RefreshIndicator> {
280282
}
281283
}
282284

283-
Future<Null> _doHandlePointerUp(PointerUpEvent event) async {
284-
if (_mode == _RefreshIndicatorMode.armed) {
285-
_mode = _RefreshIndicatorMode.snap;
286-
await _sizeController.animateTo(1.0 / _kDragSizeFactorLimit, duration: _kIndicatorSnapDuration);
287-
if (mounted && _mode == _RefreshIndicatorMode.snap) {
288-
setState(() {
289-
_mode = _RefreshIndicatorMode.refresh; // Show the indeterminate progress indicator.
290-
});
285+
Future<Null> _show() async {
286+
_mode = _RefreshIndicatorMode.snap;
287+
await _sizeController.animateTo(1.0 / _kDragSizeFactorLimit, duration: _kIndicatorSnapDuration);
288+
if (mounted && _mode == _RefreshIndicatorMode.snap) {
289+
assert(config.refresh != null);
290+
setState(() {
291+
_mode = _RefreshIndicatorMode.refresh; // Show the indeterminate progress indicator.
292+
});
291293

292-
// Only one refresh callback is allowed to run at a time. If the user
293-
// attempts to start a refresh while one is still running ("pending") we
294-
// just continue to wait on the pending refresh.
295-
if (_pendingRefreshFuture == null)
296-
_pendingRefreshFuture = config.refresh();
297-
await _pendingRefreshFuture;
298-
bool completed = _pendingRefreshFuture != null;
299-
_pendingRefreshFuture = null;
300-
301-
if (mounted && completed && _mode == _RefreshIndicatorMode.refresh)
302-
_dismiss(_DismissTransition.slide);
303-
}
304-
} else if (_mode == _RefreshIndicatorMode.drag) {
305-
_dismiss(_DismissTransition.shrink);
294+
// Only one refresh callback is allowed to run at a time. If the user
295+
// attempts to start a refresh while one is still running ("pending") we
296+
// just continue to wait on the pending refresh.
297+
if (_pendingRefreshFuture == null)
298+
_pendingRefreshFuture = config.refresh();
299+
await _pendingRefreshFuture;
300+
bool completed = _pendingRefreshFuture != null;
301+
_pendingRefreshFuture = null;
302+
303+
if (mounted && completed && _mode == _RefreshIndicatorMode.refresh)
304+
_dismiss(_DismissTransition.slide);
306305
}
307306
}
308307

308+
Future<Null> _doHandlePointerUp(PointerUpEvent event) async {
309+
if (_mode == _RefreshIndicatorMode.armed)
310+
_show();
311+
else if (_mode == _RefreshIndicatorMode.drag)
312+
_dismiss(_DismissTransition.shrink);
313+
}
314+
309315
void _handlePointerUp(PointerEvent event) {
310316
_doHandlePointerUp(event);
311317
}
312318

319+
/// Show the refresh indicator and run the refresh callback as if it had
320+
/// been started interactively. If this method is called while the refresh
321+
/// callback is running, it quietly does nothing.
322+
///
323+
/// See also:
324+
///
325+
/// * [GlobalKey] (creating the RefreshIndicator with a [GlobalKey<RefreshIndicatorState>]
326+
/// will make it possible to refer to the [RefreshIndicatorState] later)
327+
Future<Null> show() async {
328+
if (_mode != _RefreshIndicatorMode.refresh) {
329+
_sizeController.value = 0.0;
330+
_scaleController.value = 0.0;
331+
await _show();
332+
}
333+
}
334+
313335
@override
314336
Widget build(BuildContext context) {
315337
final bool showIndeterminateIndicator =

0 commit comments

Comments
 (0)