From d9b9983e1ab92b9be8dffc107853b2ccc716b7d9 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 30 Jun 2016 10:07:28 -0700 Subject: [PATCH] Refresh indicator (#4788) --- .../lib/demo/overscroll_demo.dart | 8 +- .../flutter/lib/src/animation/curves.dart | 2 + packages/flutter/lib/src/material/list.dart | 2 +- .../lib/src/material/progress_indicator.dart | 45 +++++-- .../lib/src/material/refresh_indicator.dart | 125 +++++++++++------- .../test/material/refresh_indicator_test.dart | 7 +- 6 files changed, 129 insertions(+), 60 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/overscroll_demo.dart b/examples/flutter_gallery/lib/demo/overscroll_demo.dart index 7e0898493c..582bed053c 100644 --- a/examples/flutter_gallery/lib/demo/overscroll_demo.dart +++ b/examples/flutter_gallery/lib/demo/overscroll_demo.dart @@ -18,6 +18,7 @@ class OverscrollDemo extends StatefulWidget { } class OverscrollDemoState extends State { + static final GlobalKey _scrollableKey = new GlobalKey(); static final List _items = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' ]; @@ -50,6 +51,7 @@ class OverscrollDemoState extends State { child: new MaterialList( type: MaterialListType.threeLine, padding: const EdgeInsets.all(8.0), + scrollableKey: _scrollableKey, children: _items.map((String item) { return new ListItem( isThreeLine: true, @@ -65,7 +67,11 @@ class OverscrollDemoState extends State { body = new OverscrollIndicator(child: body); break; case IndicatorType.refresh: - body = new RefreshIndicator(child: body, refresh: refresh); + body = new RefreshIndicator( + child: body, + refresh: refresh, + scrollableKey: _scrollableKey + ); break; } diff --git a/packages/flutter/lib/src/animation/curves.dart b/packages/flutter/lib/src/animation/curves.dart index 3c2da02ad7..0a8f5172d8 100644 --- a/packages/flutter/lib/src/animation/curves.dart +++ b/packages/flutter/lib/src/animation/curves.dart @@ -60,6 +60,8 @@ class SawTooth extends Curve { @override double transform(double t) { + if (t == 1.0) + return 1.0; t *= count; return t - t.truncateToDouble(); } diff --git a/packages/flutter/lib/src/material/list.dart b/packages/flutter/lib/src/material/list.dart index 5f8faafcbe..542676d2f5 100644 --- a/packages/flutter/lib/src/material/list.dart +++ b/packages/flutter/lib/src/material/list.dart @@ -96,7 +96,7 @@ class MaterialList extends StatelessWidget { @override Widget build(BuildContext context) { return new ScrollableList( - key: scrollableKey, + scrollableKey: scrollableKey, initialScrollOffset: initialScrollOffset, scrollDirection: Axis.vertical, onScrollStart: onScrollStart, diff --git a/packages/flutter/lib/src/material/progress_indicator.dart b/packages/flutter/lib/src/material/progress_indicator.dart index a29f308d8e..ee931f2893 100644 --- a/packages/flutter/lib/src/material/progress_indicator.dart +++ b/packages/flutter/lib/src/material/progress_indicator.dart @@ -317,9 +317,7 @@ class _CircularProgressIndicatorState extends State { @override void initState() { super.initState(); - _controller = new AnimationController( - duration: const Duration(milliseconds: 6666) - )..repeat(); + _controller = _buildController(); } @override @@ -328,6 +326,10 @@ class _CircularProgressIndicatorState extends State { super.dispose(); } + AnimationController _buildController() { + return new AnimationController(duration: const Duration(milliseconds: 6666))..repeat(); + } + Widget _buildIndicator(BuildContext context, double headValue, double tailValue, int stepValue, double rotationValue) { return new Container( constraints: new BoxConstraints( @@ -348,11 +350,7 @@ class _CircularProgressIndicatorState extends State { ); } - @override - Widget build(BuildContext context) { - if (config.value != null) - return _buildIndicator(context, 0.0, 0.0, 0, 0.0); - + Widget _buildAnimation() { return new AnimatedBuilder( animation: _controller, builder: (BuildContext context, Widget child) { @@ -366,6 +364,13 @@ class _CircularProgressIndicatorState extends State { } ); } + + @override + Widget build(BuildContext context) { + if (config.value != null) + return _buildIndicator(context, 0.0, 0.0, 0, 0.0); + return _buildAnimation(); + } } class _RefreshProgressIndicatorPainter extends _CircularProgressIndicatorPainter { @@ -438,7 +443,12 @@ class RefreshProgressIndicator extends CircularProgressIndicator { double value, Color backgroundColor, Animation valueColor - }) : super(key: key, value: value, backgroundColor: backgroundColor, valueColor: valueColor); + }) : super( + key: key, + value: value, + backgroundColor: backgroundColor, + valueColor: valueColor + ); @override _RefreshProgressIndicatorState createState() => new _RefreshProgressIndicatorState(); @@ -447,6 +457,19 @@ class RefreshProgressIndicator extends CircularProgressIndicator { class _RefreshProgressIndicatorState extends _CircularProgressIndicatorState { static double _kIndicatorSize = 40.0; + // Always show the indeterminate version of the circular progress indicator. + // When value is non-null the sweep of the progress indicator arrow's arc + // varies from 0 to about 270 degrees. When value is null the arrow animates + // starting from wherever we left it. + @override + Widget build(BuildContext context) { + if (config.value != null) + _controller.value = config.value / 10.0; + else + _controller.forward(); + return _buildAnimation(); + } + @override Widget _buildIndicator(BuildContext context, double headValue, double tailValue, int stepValue, double rotationValue) { return new Container( @@ -462,8 +485,8 @@ class _RefreshProgressIndicatorState extends _CircularProgressIndicatorState { child: new CustomPaint( painter: new _RefreshProgressIndicatorPainter( valueColor: config._getValueColor(context), - value: config.value, // may be null - headValue: headValue, // remaining arguments are ignored if config.value is not null + value: null, // Draw the indeterminate progress indicator. + headValue: headValue, tailValue: tailValue, stepValue: stepValue, rotationValue: rotationValue, diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index 17e85466b2..a24f5daad3 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -44,6 +44,14 @@ enum RefreshIndicatorLocation { bottom, } +enum _RefreshIndicatorMode { + drag, + armed, + snap, + refresh, + dimiss +} + /// A widget that supports the Material "swipe to refresh" idiom. /// /// When the child's vertical Scrollable descendant overscrolls, an @@ -76,7 +84,7 @@ class RefreshIndicator extends StatefulWidget { /// Identifies the [Scrollable] descendant of child that will cause the /// refresh indicator to appear. Can be null if there's only one /// [Scrollable] descendant. - final Key scrollableKey; + final GlobalKey scrollableKey; /// The distance from the child's top or bottom edge to where the refresh indicator /// will settle. During the drag that exposes the refresh indicator, its actual @@ -101,6 +109,7 @@ class _RefreshIndicatorState extends State { final AnimationController _scaleController = new AnimationController(); Animation _sizeFactor; Animation _scaleFactor; + Animation _value; Animation _valueColor; double _scrollOffset; @@ -108,6 +117,8 @@ class _RefreshIndicatorState extends State { double _minScrollOffset; double _maxScrollOffset; RefreshIndicatorLocation _location = RefreshIndicatorLocation.top; + _RefreshIndicatorMode _mode; + Future _pendingRefreshFuture; @override void initState() { @@ -117,6 +128,13 @@ class _RefreshIndicatorState extends State { final ThemeData theme = Theme.of(context); + // The "value" of the circular progress indicator during a drag. + _value = new Tween( + begin: 0.0, + end: 0.75 + ) + .animate(_sizeController); + // Fully opaque when we've reached config.displacement. _valueColor = new ColorTween( begin: theme.primaryColor.withOpacity(0.0), @@ -126,6 +144,7 @@ class _RefreshIndicatorState extends State { parent: _sizeController, curve: new Interval(0.0, 1.0 / _kDragSizeFactorLimit) )); + } @override @@ -146,73 +165,84 @@ class _RefreshIndicatorState extends State { _maxScrollOffset = scrollBehavior.maxScrollOffset; } - void _onScrollStarted(ScrollableState scrollable) { - _updateState(scrollable); - _scaleController.value = 0.0; - _sizeController.value = 0.0; - } - RefreshIndicatorLocation get _locationForScrollOffset { return _scrollOffset < _minScrollOffset ? RefreshIndicatorLocation.top : RefreshIndicatorLocation.bottom; } - void _onScrollUpdated(ScrollableState scrollable) { + void _handlePointerDown(PointerDownEvent event) { + final ScrollableState scrollable = config.scrollableKey?.currentState; + if (scrollable == null) + return; + + _updateState(scrollable); + _scaleController.value = 0.0; + _sizeController.value = 0.0; + _mode = _RefreshIndicatorMode.drag; + } + + void _handlePointerMove(PointerMoveEvent event) { + final ScrollableState scrollable = config.scrollableKey?.currentState; + if (scrollable == null) + return; final double value = scrollable.scrollOffset; if ((value < _minScrollOffset || value > _maxScrollOffset) && ((value - _scrollOffset).abs() > kPixelScrollTolerance.distance)) { final double overScroll = value < _minScrollOffset ? _minScrollOffset - value : value - _maxScrollOffset; final double newValue = overScroll / (_containerExtent * _kDragContainerExtentPercentage); - if (newValue > _sizeController.value) { - _sizeController.value = newValue; - if (_location != _locationForScrollOffset) { - setState(() { - _location = _locationForScrollOffset; - }); - } + _sizeController.value = newValue.clamp(0.0, 1.0); + if (_location != _locationForScrollOffset) { + setState(() { + _location = _locationForScrollOffset; + }); } } + _mode = _valueColor.value.alpha == 0xFF ? _RefreshIndicatorMode.armed : _RefreshIndicatorMode.drag; _updateState(scrollable); } - Future _doOnScrollEnded(ScrollableState scrollable) async { - if (_valueColor.value.alpha == 0xFF) { + Future _doHandlePointerUp(PointerUpEvent event) async { + if (_mode == _RefreshIndicatorMode.armed) { + _mode = _RefreshIndicatorMode.snap; await _sizeController.animateTo(1.0 / _kDragSizeFactorLimit, duration: _kIndicatorSnapDuration); - await config.refresh(); - } - return _scaleController.animateTo(1.0, duration: _kIndicatorScaleDuration); - } + if (mounted && _mode == _RefreshIndicatorMode.snap) { + setState(() { + _mode = _RefreshIndicatorMode.refresh; // Show the indeterminate progress indicator. + }); - void _onScrollEnded(ScrollableState scrollable) { - _doOnScrollEnded(scrollable); - } + // Only one refresh callback is allowed to run at a time. If the user + // attempts to start a refresh while one is still running ("pending") we + // just continue to wait on the pending refresh. + if (_pendingRefreshFuture == null) + _pendingRefreshFuture = config.refresh(); + await _pendingRefreshFuture; + bool completed = _pendingRefreshFuture != null; + _pendingRefreshFuture = null; - bool _handleScrollNotification(ScrollNotification notification) { - if (config.scrollableKey == null || config.scrollableKey == notification.scrollable.config.key) { - final ScrollableState scrollable = notification.scrollable; - if (scrollable.config.scrollDirection != Axis.vertical) - return false; - switch(notification.kind) { - case ScrollNotificationKind.started: - _onScrollStarted(scrollable); - break; - case ScrollNotificationKind.updated: - _onScrollUpdated(scrollable); - break; - case ScrollNotificationKind.ended: - _onScrollEnded(scrollable); - break; + if (mounted && completed && _mode == _RefreshIndicatorMode.refresh) { + setState(() { + _mode = null; // Stop showing the indeterminate progress indicator. + }); + _scaleController.animateTo(1.0, duration: _kIndicatorScaleDuration); + } } + } else { + _scaleController.animateTo(1.0, duration: _kIndicatorScaleDuration); } - return false; + } + + void _handlePointerUp(PointerEvent event) { + _doHandlePointerUp(event); } @override Widget build(BuildContext context) { final bool isAtTop = _location == RefreshIndicatorLocation.top; - return new NotificationListener( - onNotification: _handleScrollNotification, + return new Listener( + onPointerDown: _handlePointerDown, + onPointerMove: _handlePointerMove, + onPointerUp: _handlePointerUp, child: new Stack( children: [ new ClampOverscrolls( @@ -235,9 +265,14 @@ class _RefreshIndicatorState extends State { alignment: isAtTop ? FractionalOffset.bottomCenter : FractionalOffset.topCenter, child: new ScaleTransition( scale: _scaleFactor, - child: new RefreshProgressIndicator( - value: null, - valueColor: _valueColor + child: new AnimatedBuilder( + animation: _sizeController, + builder: (BuildContext context, Widget child) { + return new RefreshProgressIndicator( + value: _mode == _RefreshIndicatorMode.refresh ? null : _value.value, + valueColor: _valueColor + ); + } ) ) ) diff --git a/packages/flutter/test/material/refresh_indicator_test.dart b/packages/flutter/test/material/refresh_indicator_test.dart index 7ba299cdf0..378fa0ffbe 100644 --- a/packages/flutter/test/material/refresh_indicator_test.dart +++ b/packages/flutter/test/material/refresh_indicator_test.dart @@ -7,8 +7,9 @@ import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; -void main() { +final GlobalKey scrollableKey = new GlobalKey(); +void main() { bool refreshCalled = false; Future refresh() { @@ -19,8 +20,10 @@ void main() { testWidgets('RefreshIndicator', (WidgetTester tester) async { await tester.pumpWidget( new RefreshIndicator( + scrollableKey: scrollableKey, refresh: refresh, child: new Block( + scrollableKey: scrollableKey, children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { return new SizedBox( height: 200.0, @@ -31,7 +34,7 @@ void main() { ) ); - await tester.fling(find.text('A'), const Offset(0.0, 200.0), -1000.0); + await tester.fling(find.text('A'), const Offset(0.0, 300.0), -1000.0); await tester.pump(); await tester.pump(const Duration(seconds: 1)); // finish the scroll animation await tester.pump(const Duration(seconds: 1)); // finish the indicator settle animation