From 80e159d46950ff40f3c0d55df452ee71a688014a Mon Sep 17 00:00:00 2001 From: Roberto Scaramuzzi Date: Wed, 1 Nov 2017 10:29:37 -0700 Subject: [PATCH] =?UTF-8?q?Add=20acceptNotification=20parameter=20to=20Ref?= =?UTF-8?q?reshIndicator=20and=20OverscrollIn=E2=80=A6=20(#12716)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add acceptNotification parameter to RefreshIndicator and OverscrollIndicator * Various fixes suggested by reviewer * Fixed lint errors --- .../lib/src/material/refresh_indicator.dart | 16 +++++-- .../lib/src/widgets/overscroll_indicator.dart | 15 +++++-- .../lib/src/widgets/scroll_notification.dart | 11 +++++ .../test/material/refresh_indicator_test.dart | 42 +++++++++++++++++++ .../widgets/overscroll_indicator_test.dart | 33 ++++++++++++++- 5 files changed, 110 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index 3d763d797f..6d46ca749c 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -78,7 +78,8 @@ enum _RefreshIndicatorMode { class RefreshIndicator extends StatefulWidget { /// Creates a refresh indicator. /// - /// The [onRefresh] and [child] arguments must be non-null. The default + /// The [onRefresh], [child], and [notificationPredicate] arguments must be + /// non-null. The default /// [displacement] is 40.0 logical pixels. const RefreshIndicator({ Key key, @@ -86,9 +87,11 @@ class RefreshIndicator extends StatefulWidget { this.displacement: 40.0, @required this.onRefresh, this.color, - this.backgroundColor + this.backgroundColor, + this.notificationPredicate: defaultScrollNotificationPredicate, }) : assert(child != null), assert(onRefresh != null), + assert(notificationPredicate != null), super(key: key); /// The refresh indicator will be stacked on top of this child. The indicator @@ -112,6 +115,13 @@ class RefreshIndicator extends StatefulWidget { /// The progress indicator's background color. The current theme's /// [ThemeData.canvasColor] by default. final Color backgroundColor; + + /// A check that specifies whether a [ScrollNotification] should be + /// handled by this widget. + /// + /// By default, checks whether `notification.depth == 0`. Set it to something + /// else for more complicated layouts. + final ScrollNotificationPredicate notificationPredicate; @override RefreshIndicatorState createState() => new RefreshIndicatorState(); @@ -174,7 +184,7 @@ class RefreshIndicatorState extends State with TickerProviderS } bool _handleScrollNotification(ScrollNotification notification) { - if (notification.depth != 0) + if (!widget.notificationPredicate(notification)) return false; if (notification is ScrollStartNotification && notification.metrics.extentBefore == 0.0 && _mode == null && _start(notification.metrics.axisDirection)) { diff --git a/packages/flutter/lib/src/widgets/overscroll_indicator.dart b/packages/flutter/lib/src/widgets/overscroll_indicator.dart index 225277fc09..cda512f29a 100644 --- a/packages/flutter/lib/src/widgets/overscroll_indicator.dart +++ b/packages/flutter/lib/src/widgets/overscroll_indicator.dart @@ -37,19 +37,21 @@ class GlowingOverscrollIndicator extends StatefulWidget { /// widget must contain a widget that generates a [ScrollNotification], such /// as a [ListView] or a [GridView]. /// - /// The [showLeading], [showTrailing], [axisDirection], and [color] arguments - /// must not be null. + /// The [showLeading], [showTrailing], [axisDirection], [color], and + /// [notificationPredicate] arguments must not be null. const GlowingOverscrollIndicator({ Key key, this.showLeading: true, this.showTrailing: true, @required this.axisDirection, @required this.color, + this.notificationPredicate: defaultScrollNotificationPredicate, this.child, }) : assert(showLeading != null), assert(showTrailing != null), assert(axisDirection != null), assert(color != null), + assert(notificationPredicate != null), super(key: key); /// Whether to show the overscroll glow on the side with negative scroll @@ -84,6 +86,13 @@ class GlowingOverscrollIndicator extends StatefulWidget { /// The color of the glow. The alpha channel is ignored. final Color color; + + /// A check that specifies whether a [ScrollNotification] should be + /// handled by this widget. + /// + /// By default, checks whether `notification.depth == 0`. Set it to something + /// else for more complicated layouts. + final ScrollNotificationPredicate notificationPredicate; /// The subtree to place inside the overscroll indicator. This should include /// a source of [ScrollNotification] notifications, typically a [Scrollable] @@ -144,7 +153,7 @@ class _GlowingOverscrollIndicatorState extends State final Map _accepted = {false: true, true: true}; bool _handleScrollNotification(ScrollNotification notification) { - if (notification.depth != 0) + if (!widget.notificationPredicate(notification)) return false; if (notification is OverscrollNotification) { _GlowController controller; diff --git a/packages/flutter/lib/src/widgets/scroll_notification.dart b/packages/flutter/lib/src/widgets/scroll_notification.dart index 96322e7800..1181f9fbab 100644 --- a/packages/flutter/lib/src/widgets/scroll_notification.dart +++ b/packages/flutter/lib/src/widgets/scroll_notification.dart @@ -282,3 +282,14 @@ class UserScrollNotification extends ScrollNotification { description.add('direction: $direction'); } } + +/// A predicate for [ScrollNotification], used to customize widgets that +/// listen to notifications from their children. +typedef bool ScrollNotificationPredicate(ScrollNotification notification); + +/// A [ScrollNotificationPredicate] that checks whether +/// `notification.depth == 0`, which means that the notification diid not bubble +/// through any intervening scrolling widgets. +bool defaultScrollNotificationPredicate(ScrollNotification notification) { + return notification.depth == 0; +} diff --git a/packages/flutter/test/material/refresh_indicator_test.dart b/packages/flutter/test/material/refresh_indicator_test.dart index e5b784f8ee..f73cd753e4 100644 --- a/packages/flutter/test/material/refresh_indicator_test.dart +++ b/packages/flutter/test/material/refresh_indicator_test.dart @@ -46,6 +46,48 @@ void main() { await tester.pump(const Duration(seconds: 1)); // finish the indicator hide animation expect(refreshCalled, true); }); + + testWidgets('Refresh Indicator - nested', (WidgetTester tester) async { + refreshCalled = false; + await tester.pumpWidget( + new MaterialApp( + home: new RefreshIndicator( + notificationPredicate: (ScrollNotification notification) => notification.depth == 1, + onRefresh: refresh, + child: new SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: new Container( + width: 600.0, + child:new ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { + return new SizedBox( + height: 200.0, + child: new Text(item), + ); + }).toList(), + ), + ), + ), + ), + ), + ); + + await tester.fling(find.text('A'), const Offset(300.0, 0.0), 1000.0); // horizontal fling + 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 + await tester.pump(const Duration(seconds: 1)); // finish the indicator hide animation + expect(refreshCalled, false); + + + await tester.fling(find.text('A'), const Offset(0.0, 300.0), 1000.0); // vertical fling + 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 + await tester.pump(const Duration(seconds: 1)); // finish the indicator hide animation + expect(refreshCalled, true); + }); testWidgets('RefreshIndicator - bottom', (WidgetTester tester) async { refreshCalled = false; diff --git a/packages/flutter/test/widgets/overscroll_indicator_test.dart b/packages/flutter/test/widgets/overscroll_indicator_test.dart index b81900a9bf..77e50aec33 100644 --- a/packages/flutter/test/widgets/overscroll_indicator_test.dart +++ b/packages/flutter/test/widgets/overscroll_indicator_test.dart @@ -57,7 +57,38 @@ void main() { await tester.pumpAndSettle(const Duration(seconds: 1)); expect(painter, doesNotOverscroll); }); - + + testWidgets('Nested scrollable', (WidgetTester tester) async { + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new GlowingOverscrollIndicator( + axisDirection: AxisDirection.down, + color: const Color(0x0DFFFFFF), + notificationPredicate: (ScrollNotification notification) => notification.depth == 1, + child: new SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: new Container( + width: 600.0, + child: new CustomScrollView( + slivers: [ + const SliverToBoxAdapter(child: const SizedBox(height: 2000.0)), + ], + ), + ), + ), + ), + ), + ); + + final RenderObject outerPainter = tester.renderObject(find.byType(CustomPaint).first); + final RenderObject innerPainter = tester.renderObject(find.byType(CustomPaint).last); + + await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0)); + expect(outerPainter, paints..circle()); + expect(innerPainter, paints..circle()); + }); + testWidgets('Overscroll indicator changes side when you drag on the other side', (WidgetTester tester) async { await tester.pumpWidget( new Directionality(