From ae9bd5ea920d6555db20385eddb1043de3db865f Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 14 Feb 2017 19:01:17 -0800 Subject: [PATCH] Fix the depth of scroll notifications (#8156) Fixes https://github.com/flutter/flutter/issues/8017 --- packages/flutter/lib/src/material/tabs.dart | 2 +- packages/flutter/lib/src/widgets/page_view.dart | 2 +- .../lib/src/widgets/scroll_notification.dart | 14 +++++++++----- .../test/widgets/scroll_notification_test.dart | 12 +++++------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index e9678d6dde..47ebdb5613 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -726,7 +726,7 @@ class _TabBarViewState extends State { if (_warpUnderwayCount > 0) return false; - if (notification.depth != 1) + if (notification.depth != 0) return false; if (notification is ScrollStartNotification) { diff --git a/packages/flutter/lib/src/widgets/page_view.dart b/packages/flutter/lib/src/widgets/page_view.dart index e40fea92ee..f235b72cf2 100644 --- a/packages/flutter/lib/src/widgets/page_view.dart +++ b/packages/flutter/lib/src/widgets/page_view.dart @@ -188,7 +188,7 @@ class PageView extends BoxScrollView { final Widget scrollable = super.build(context); return new NotificationListener( onNotification: (ScrollNotification2 notification) { - if (notification.depth == 1 && onPageChanged != null && notification is ScrollEndNotification) { + if (notification.depth == 0 && onPageChanged != null && notification is ScrollEndNotification) { final ScrollMetrics metrics = notification.metrics; onPageChanged(metrics.extentBefore ~/ metrics.viewportDimension); } diff --git a/packages/flutter/lib/src/widgets/scroll_notification.dart b/packages/flutter/lib/src/widgets/scroll_notification.dart index 2a0fe6a26e..d894794eec 100644 --- a/packages/flutter/lib/src/widgets/scroll_notification.dart +++ b/packages/flutter/lib/src/widgets/scroll_notification.dart @@ -75,15 +75,19 @@ abstract class ScrollNotification2 extends LayoutChangedNotification { /// size of the viewport, for instance. final BuildContext context; - /// The number of [Scrollable2] widgets that this notification has bubbled - /// through. Typically listeners only respond to notifications with a [depth] - /// of zero. + /// The number of viewports that this notification has bubbled through. + /// + /// Typically listeners only respond to notifications with a [depth] of zero. + /// + /// Specifically, this is the number of [Widget]s representing + /// [RenderAbstractViewport] render objects through which this notification + /// has bubbled. int get depth => _depth; int _depth = 0; @override bool visitAncestor(Element element) { - if (element.widget is Scrollable2) + if (element is RenderObjectElement && element.renderObject is RenderAbstractViewport) _depth += 1; return super.visitAncestor(element); } @@ -93,7 +97,7 @@ abstract class ScrollNotification2 extends LayoutChangedNotification { super.debugFillDescription(description); description.add('$axisDirection'); description.add('metrics: $metrics'); - description.add('depth: $depth'); + description.add('depth: $depth (${ depth == 0 ? "local" : "remote"})'); } } diff --git a/packages/flutter/test/widgets/scroll_notification_test.dart b/packages/flutter/test/widgets/scroll_notification_test.dart index 49c2761937..45557e5073 100644 --- a/packages/flutter/test/widgets/scroll_notification_test.dart +++ b/packages/flutter/test/widgets/scroll_notification_test.dart @@ -23,7 +23,7 @@ void main() { TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0)); await tester.pump(const Duration(seconds: 1)); expect(notification, const isInstanceOf()); - expect(notification.depth, equals(1)); + expect(notification.depth, equals(0)); ScrollStartNotification start = notification; expect(start.dragDetails, isNotNull); expect(start.dragDetails.globalPosition, equals(const Point(100.0, 100.0))); @@ -31,7 +31,7 @@ void main() { await gesture.moveBy(const Offset(-10.0, -10.0)); await tester.pump(const Duration(seconds: 1)); expect(notification, const isInstanceOf()); - expect(notification.depth, equals(1)); + expect(notification.depth, equals(0)); ScrollUpdateNotification update = notification; expect(update.dragDetails, isNotNull); expect(update.dragDetails.globalPosition, equals(const Point(90.0, 90.0))); @@ -40,7 +40,7 @@ void main() { await gesture.up(); await tester.pump(const Duration(seconds: 1)); expect(notification, const isInstanceOf()); - expect(notification.depth, equals(1)); + expect(notification.depth, equals(0)); ScrollEndNotification end = notification; expect(end.dragDetails, isNotNull); expect(end.dragDetails.velocity, equals(Velocity.zero)); @@ -93,9 +93,7 @@ void main() { expect(depth0Types, equals(types)); expect(depth1Types, equals(types)); - // These values might not be what we want in the end. - // See . - expect(depth0Values, equals([1, 1, 1, 1, 1])); - expect(depth1Values, equals([2, 2, 2, 2, 2])); + expect(depth0Values, equals([0, 0, 0, 0, 0])); + expect(depth1Values, equals([1, 1, 1, 1, 1])); }); }