From 2c4138428a27b57967b6f2fd66bde1c89b247530 Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Wed, 24 Mar 2021 22:26:46 +0100 Subject: [PATCH] Feature refresh indicator padding (#72052) Add ability to control RefreshProgressIndicator's distance from edges. --- .../lib/src/material/refresh_indicator.dart | 31 ++++++-- .../test/material/refresh_indicator_test.dart | 77 +++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index 9b4d135aca..8e575fe24e 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -113,6 +113,7 @@ class RefreshIndicator extends StatefulWidget { Key? key, required this.child, this.displacement = 40.0, + this.edgeOffset = 0.0, required this.onRefresh, this.color, this.backgroundColor, @@ -136,11 +137,31 @@ class RefreshIndicator extends StatefulWidget { /// Typically a [ListView] or [CustomScrollView]. final Widget child; - /// 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 displacement may significantly exceed this value. + /// The distance from the child's top or bottom [edgeOffset] where + /// the refresh indicator will settle. During the drag that exposes the refresh + /// indicator, its actual displacement may significantly exceed this value. + /// + /// In most cases, [displacement] distance starts counting from the parent's + /// edges. However, if [edgeOffset] is larger than zero then the [displacement] + /// value is calculated from that offset instead of the parent's edge. final double displacement; + /// The offset where [RefreshProgressIndicator] starts to appear on drag start. + /// + /// Depending whether the indicator is showing on the top or bottom, the value + /// of this variable controls how far from the parent's edge the progress + /// indicator starts to appear. This may come in handy when, for example, the + /// UI contains a top [Widget] which covers the parent's edge where the progress + /// indicator would otherwise appear. + /// + /// By default, the edge offset is set to 0. + /// + /// See also: + /// + /// * [displacement], can be used to change the distance from the edge that + /// the indicator settles. + final double edgeOffset; + /// A function that's called when the user has dragged the refresh indicator /// far enough to demonstrate that they want the app to refresh. The returned /// [Future] must complete when the refresh operation is finished. @@ -500,8 +521,8 @@ class RefreshIndicatorState extends State with TickerProviderS children: [ child, if (_mode != null) Positioned( - top: _isIndicatorAtTop! ? 0.0 : null, - bottom: !_isIndicatorAtTop! ? 0.0 : null, + top: _isIndicatorAtTop! ? widget.edgeOffset : null, + bottom: !_isIndicatorAtTop! ? widget.edgeOffset : null, left: 0.0, right: 0.0, child: SizeTransition( diff --git a/packages/flutter/test/material/refresh_indicator_test.dart b/packages/flutter/test/material/refresh_indicator_test.dart index 21c42699e9..1c961225a4 100644 --- a/packages/flutter/test/material/refresh_indicator_test.dart +++ b/packages/flutter/test/material/refresh_indicator_test.dart @@ -500,6 +500,83 @@ void main() { ); }); + testWidgets('RefreshIndicator responds to edgeOffset', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: RefreshIndicator( + onRefresh: () async {}, + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { + return SizedBox( + height: 200.0, + child: Text(item), + ); + }).toList(), + ), + ), + ) + ); + + //By default the value of edgeOffset is 0.0 + expect( + tester.widget(find.byType(RefreshIndicator)).edgeOffset, + 0.0, + ); + + await tester.pumpWidget( + MaterialApp( + home: RefreshIndicator( + onRefresh: () async {}, + edgeOffset: kToolbarHeight, + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { + return SizedBox( + height: 200.0, + child: Text(item), + ); + }).toList(), + ), + ), + ) + ); + + expect( + tester.widget(find.byType(RefreshIndicator)).edgeOffset, + kToolbarHeight, + ); + }); + + testWidgets('RefreshIndicator appears at edgeOffset', (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + home: RefreshIndicator( + edgeOffset: kToolbarHeight, + displacement: kToolbarHeight, + onRefresh: () async { + await Future.delayed(const Duration(seconds: 1), () { }); + }, + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { + return SizedBox( + height: 200.0, + child: Text(item), + ); + }).toList(), + ), + ), + )); + + await tester.fling(find.byType(ListView), const Offset(0.0, 2.0 * kToolbarHeight), 1000.0); + await tester.pump(const Duration(seconds: 2)); + + expect( + tester.getTopLeft(find.byType(RefreshProgressIndicator)).dy, + greaterThanOrEqualTo(2.0 * kToolbarHeight), + ); + }); + testWidgets('Top RefreshIndicator(anywhere mode) should be shown when dragging from non-zero scroll position', (WidgetTester tester) async { refreshCalled = false; final ScrollController scrollController = ScrollController();