Unverified Commit 418b1535 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Update style for CupertinoRefreshControl. (#19025)

All slivers must start with Sliver (or CupertinoSliver).
All widgets must have a key argument.

Also, some minor tweaks here and there to style and docs.
parent cf932490
...@@ -51,7 +51,7 @@ class _CupertinoRefreshControlDemoState extends State<CupertinoRefreshControlDem ...@@ -51,7 +51,7 @@ class _CupertinoRefreshControlDemoState extends State<CupertinoRefreshControlDem
const CupertinoSliverNavigationBar( const CupertinoSliverNavigationBar(
largeTitle: const Text('Cupertino Refresh'), largeTitle: const Text('Cupertino Refresh'),
), ),
new CupertinoRefreshControl( new CupertinoSliverRefreshControl(
onRefresh: () { onRefresh: () {
return new Future<void>.delayed(const Duration(seconds: 2)) return new Future<void>.delayed(const Duration(seconds: 2))
..then((_) => setState(() => repopulateList())); ..then((_) => setState(() => repopulateList()));
......
...@@ -13,34 +13,35 @@ import 'activity_indicator.dart'; ...@@ -13,34 +13,35 @@ import 'activity_indicator.dart';
import 'colors.dart'; import 'colors.dart';
import 'icons.dart'; import 'icons.dart';
class _CupertinoRefreshSliver extends SingleChildRenderObjectWidget { class _CupertinoSliverRefresh extends SingleChildRenderObjectWidget {
const _CupertinoRefreshSliver({ const _CupertinoSliverRefresh({
Key key,
this.refreshIndicatorLayoutExtent = 0.0, this.refreshIndicatorLayoutExtent = 0.0,
this.hasLayoutExtent = false, this.hasLayoutExtent = false,
Widget child, Widget child,
}) : assert(refreshIndicatorLayoutExtent != null), }) : assert(refreshIndicatorLayoutExtent != null),
assert(refreshIndicatorLayoutExtent >= 0.0), assert(refreshIndicatorLayoutExtent >= 0.0),
assert(hasLayoutExtent != null), assert(hasLayoutExtent != null),
super(child: child); super(key: key, child: child);
// The amount of space the indicator should occupy in the sliver in a // The amount of space the indicator should occupy in the sliver in a
// resting state when in the refreshing mode. // resting state when in the refreshing mode.
final double refreshIndicatorLayoutExtent; final double refreshIndicatorLayoutExtent;
// _RenderCupertinoRefreshSliver will paint the child in the available // _RenderCupertinoSliverRefresh will paint the child in the available
// space either way but this instructs the _RenderCupertinoRefreshSliver // space either way but this instructs the _RenderCupertinoSliverRefresh
// on whether to also occupy any layoutExtent space or not. // on whether to also occupy any layoutExtent space or not.
final bool hasLayoutExtent; final bool hasLayoutExtent;
@override @override
_RenderCupertinoRefreshSliver createRenderObject(BuildContext context) { _RenderCupertinoSliverRefresh createRenderObject(BuildContext context) {
return new _RenderCupertinoRefreshSliver( return new _RenderCupertinoSliverRefresh(
refreshIndicatorExtent: refreshIndicatorLayoutExtent, refreshIndicatorExtent: refreshIndicatorLayoutExtent,
hasLayoutExtent: hasLayoutExtent, hasLayoutExtent: hasLayoutExtent,
); );
} }
@override @override
void updateRenderObject(BuildContext context, covariant _RenderCupertinoRefreshSliver renderObject) { void updateRenderObject(BuildContext context, covariant _RenderCupertinoSliverRefresh renderObject) {
renderObject renderObject
..refreshIndicatorLayoutExtent = refreshIndicatorLayoutExtent ..refreshIndicatorLayoutExtent = refreshIndicatorLayoutExtent
..hasLayoutExtent = hasLayoutExtent; ..hasLayoutExtent = hasLayoutExtent;
...@@ -53,10 +54,9 @@ class _CupertinoRefreshSliver extends SingleChildRenderObjectWidget { ...@@ -53,10 +54,9 @@ class _CupertinoRefreshSliver extends SingleChildRenderObjectWidget {
// //
// The [layoutExtentOffsetCompensation] field keeps internal accounting to // The [layoutExtentOffsetCompensation] field keeps internal accounting to
// prevent scroll position jumps as the [layoutExtent] is set and unset. // prevent scroll position jumps as the [layoutExtent] is set and unset.
class _RenderCupertinoRefreshSliver class _RenderCupertinoSliverRefresh extends RenderSliver
extends RenderSliver
with RenderObjectWithChildMixin<RenderBox> { with RenderObjectWithChildMixin<RenderBox> {
_RenderCupertinoRefreshSliver({ _RenderCupertinoSliverRefresh({
@required double refreshIndicatorExtent, @required double refreshIndicatorExtent,
@required bool hasLayoutExtent, @required bool hasLayoutExtent,
RenderBox child, RenderBox child,
...@@ -185,13 +185,17 @@ enum RefreshIndicatorMode { ...@@ -185,13 +185,17 @@ enum RefreshIndicatorMode {
/// Initial state, when not being overscrolled into, or after the overscroll /// Initial state, when not being overscrolled into, or after the overscroll
/// is canceled or after done and the sliver retracted away. /// is canceled or after done and the sliver retracted away.
inactive, inactive,
/// While being overscrolled but not far enough yet to trigger the refresh. /// While being overscrolled but not far enough yet to trigger the refresh.
drag, drag,
/// Dragged far enough that the onRefresh callback will run and the dragged /// Dragged far enough that the onRefresh callback will run and the dragged
/// displacement is not yet at the final refresh resting state. /// displacement is not yet at the final refresh resting state.
armed, armed,
/// While the onRefresh task is running. /// While the onRefresh task is running.
refresh, refresh,
/// While the indicator is animating away after refreshing. /// While the indicator is animating away after refreshing.
done, done,
} }
...@@ -201,7 +205,7 @@ enum RefreshIndicatorMode { ...@@ -201,7 +205,7 @@ enum RefreshIndicatorMode {
/// control and the space available. /// control and the space available.
/// ///
/// The `refreshTriggerPullDistance` and `refreshIndicatorExtent` parameters are /// The `refreshTriggerPullDistance` and `refreshIndicatorExtent` parameters are
/// the same values passed into the [CupertinoRefreshControl]. /// the same values passed into the [CupertinoSliverRefreshControl].
/// ///
/// The `pulledExtent` parameter is the currently available space either from /// The `pulledExtent` parameter is the currently available space either from
/// overscrolling or as held by the sliver during refresh. /// overscrolling or as held by the sliver during refresh.
...@@ -213,9 +217,9 @@ typedef Widget RefreshControlIndicatorBuilder( ...@@ -213,9 +217,9 @@ typedef Widget RefreshControlIndicatorBuilder(
double refreshIndicatorExtent, double refreshIndicatorExtent,
); );
/// A callback function that's invoked when the [CupertinoRefreshControl] is /// A callback function that's invoked when the [CupertinoSliverRefreshControl] is
/// pulled a `refreshTriggerPullDistance`. Must return a [Future]. Upon /// pulled a `refreshTriggerPullDistance`. Must return a [Future]. Upon
/// completion of the [Future], the [CupertinoRefreshControl] enters the /// completion of the [Future], the [CupertinoSliverRefreshControl] enters the
/// [RefreshIndicatorMode.done] state and will start to go away. /// [RefreshIndicatorMode.done] state and will start to go away.
typedef Future<void> RefreshCallback(); typedef Future<void> RefreshCallback();
...@@ -254,19 +258,22 @@ typedef Future<void> RefreshCallback(); ...@@ -254,19 +258,22 @@ typedef Future<void> RefreshCallback();
/// * [RefreshIndicator], a Material Design version of the pull-to-refresh /// * [RefreshIndicator], a Material Design version of the pull-to-refresh
/// paradigm. This widget works differently than [RefreshIndicator] because /// paradigm. This widget works differently than [RefreshIndicator] because
/// instead of being an overlay on top of the scrollable, the /// instead of being an overlay on top of the scrollable, the
/// [CupertinoRefreshControl] is part of the scrollable and actively occupies /// [CupertinoSliverRefreshControl] is part of the scrollable and actively occupies
/// scrollable space. /// scrollable space.
class CupertinoRefreshControl extends StatefulWidget { class CupertinoSliverRefreshControl extends StatefulWidget {
/// Create a new [CupertinoRefreshControl] for inserting into a list of slivers. /// Create a new refresh control for inserting into a list of slivers.
/// ///
/// [refreshTriggerPullDistance], [refreshIndicatorExtent] both have reasonable /// The [refreshTriggerPullDistance] and [refreshIndicatorExtent] arguments
/// defaults and cannot be null. /// must not be null.
/// ///
/// [builder] has a default indicator builder but can be null, in which case /// The [builder] argument may be null, in which case no indicator UI will be
/// no indicator UI will be shown but the [onRefresh] will still be invoked. /// shown but the [onRefresh] will still be invoked. By default, [builder]
/// shows a [CupertinoActivityIndicator].
/// ///
/// [onRefresh] will be called when pulled far enough to trigger a refresh. /// The [onRefresh] argument will be called when pulled far enough to trigger
const CupertinoRefreshControl({ /// a refresh.
const CupertinoSliverRefreshControl({
Key key,
this.refreshTriggerPullDistance = _defaultRefreshTriggerPullDistance, this.refreshTriggerPullDistance = _defaultRefreshTriggerPullDistance,
this.refreshIndicatorExtent = _defaultRefreshIndicatorExtent, this.refreshIndicatorExtent = _defaultRefreshIndicatorExtent,
this.builder = buildSimpleRefreshIndicator, this.builder = buildSimpleRefreshIndicator,
...@@ -279,7 +286,8 @@ class CupertinoRefreshControl extends StatefulWidget { ...@@ -279,7 +286,8 @@ class CupertinoRefreshControl extends StatefulWidget {
refreshTriggerPullDistance >= refreshIndicatorExtent, refreshTriggerPullDistance >= refreshIndicatorExtent,
'The refresh indicator cannot take more space in its final state ' 'The refresh indicator cannot take more space in its final state '
'than the amount initially created by overscrolling.' 'than the amount initially created by overscrolling.'
); ),
super(key: key);
/// The amount of overscroll the scrollable must be dragged to trigger a reload. /// The amount of overscroll the scrollable must be dragged to trigger a reload.
/// ///
...@@ -327,12 +335,12 @@ class CupertinoRefreshControl extends StatefulWidget { ...@@ -327,12 +335,12 @@ class CupertinoRefreshControl extends StatefulWidget {
static const double _defaultRefreshTriggerPullDistance = 100.0; static const double _defaultRefreshTriggerPullDistance = 100.0;
static const double _defaultRefreshIndicatorExtent = 60.0; static const double _defaultRefreshIndicatorExtent = 60.0;
/// Retrieve the current state of the CupertinoRefreshControl. The same as the /// Retrieve the current state of the CupertinoSliverRefreshControl. The same as the
/// state that gets passed into the [builder] function. Used for testing. /// state that gets passed into the [builder] function. Used for testing.
@visibleForTesting @visibleForTesting
static RefreshIndicatorMode state(BuildContext context) { static RefreshIndicatorMode state(BuildContext context) {
final _CupertinoRefreshControlState state final _CupertinoSliverRefreshControlState state
= context.ancestorStateOfType(const TypeMatcher<_CupertinoRefreshControlState>()); = context.ancestorStateOfType(const TypeMatcher<_CupertinoSliverRefreshControlState>());
return state.refreshState; return state.refreshState;
} }
...@@ -373,10 +381,10 @@ class CupertinoRefreshControl extends StatefulWidget { ...@@ -373,10 +381,10 @@ class CupertinoRefreshControl extends StatefulWidget {
} }
@override @override
_CupertinoRefreshControlState createState() => new _CupertinoRefreshControlState(); _CupertinoSliverRefreshControlState createState() => new _CupertinoSliverRefreshControlState();
} }
class _CupertinoRefreshControlState extends State<CupertinoRefreshControl> { class _CupertinoSliverRefreshControlState extends State<CupertinoSliverRefreshControl> {
/// Reset the state from done to inactive when only this fraction of the /// Reset the state from done to inactive when only this fraction of the
/// original `refreshTriggerPullDistance` is left. /// original `refreshTriggerPullDistance` is left.
static const double _inactiveResetOverscrollFraction = 0.1; static const double _inactiveResetOverscrollFraction = 0.1;
...@@ -498,7 +506,7 @@ class _CupertinoRefreshControlState extends State<CupertinoRefreshControl> { ...@@ -498,7 +506,7 @@ class _CupertinoRefreshControlState extends State<CupertinoRefreshControl> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new _CupertinoRefreshSliver( return new _CupertinoSliverRefresh(
refreshIndicatorLayoutExtent: widget.refreshIndicatorExtent, refreshIndicatorLayoutExtent: widget.refreshIndicatorExtent,
hasLayoutExtent: hasSliverLayoutExtent, hasLayoutExtent: hasSliverLayoutExtent,
// A LayoutBuilder lets the sliver's layout changes be fed back out to // A LayoutBuilder lets the sliver's layout changes be fed back out to
...@@ -515,9 +523,8 @@ class _CupertinoRefreshControlState extends State<CupertinoRefreshControl> { ...@@ -515,9 +523,8 @@ class _CupertinoRefreshControlState extends State<CupertinoRefreshControl> {
widget.refreshTriggerPullDistance, widget.refreshTriggerPullDistance,
widget.refreshIndicatorExtent, widget.refreshIndicatorExtent,
); );
} else {
return new Container();
} }
return new Container();
}, },
) )
); );
......
...@@ -75,7 +75,7 @@ enum _RefreshIndicatorMode { ...@@ -75,7 +75,7 @@ enum _RefreshIndicatorMode {
/// * [RefreshIndicatorState], can be used to programmatically show the refresh indicator. /// * [RefreshIndicatorState], can be used to programmatically show the refresh indicator.
/// * [RefreshProgressIndicator], widget used by [RefreshIndicator] to show /// * [RefreshProgressIndicator], widget used by [RefreshIndicator] to show
/// the inner circular progress spinner during refreshes. /// the inner circular progress spinner during refreshes.
/// * [CupertinoRefreshControl], an iOS equivalent of the pull-to-refresh pattern. /// * [CupertinoSliverRefreshControl], an iOS equivalent of the pull-to-refresh pattern.
/// Must be used as a sliver inside a [CustomScrollView] instead of wrapping /// Must be used as a sliver inside a [CustomScrollView] instead of wrapping
/// around a [ScrollView] because it's a part of the scrollable instead of /// around a [ScrollView] because it's a part of the scrollable instead of
/// being overlaid on top of it. /// being overlaid on top of it.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment