Commit ae9bd5ea authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Fix the depth of scroll notifications (#8156)

Fixes https://github.com/flutter/flutter/issues/8017
parent cdc0f15f
...@@ -726,7 +726,7 @@ class _TabBarViewState extends State<TabBarView> { ...@@ -726,7 +726,7 @@ class _TabBarViewState extends State<TabBarView> {
if (_warpUnderwayCount > 0) if (_warpUnderwayCount > 0)
return false; return false;
if (notification.depth != 1) if (notification.depth != 0)
return false; return false;
if (notification is ScrollStartNotification) { if (notification is ScrollStartNotification) {
......
...@@ -188,7 +188,7 @@ class PageView extends BoxScrollView { ...@@ -188,7 +188,7 @@ class PageView extends BoxScrollView {
final Widget scrollable = super.build(context); final Widget scrollable = super.build(context);
return new NotificationListener<ScrollNotification2>( return new NotificationListener<ScrollNotification2>(
onNotification: (ScrollNotification2 notification) { 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; final ScrollMetrics metrics = notification.metrics;
onPageChanged(metrics.extentBefore ~/ metrics.viewportDimension); onPageChanged(metrics.extentBefore ~/ metrics.viewportDimension);
} }
......
...@@ -75,15 +75,19 @@ abstract class ScrollNotification2 extends LayoutChangedNotification { ...@@ -75,15 +75,19 @@ abstract class ScrollNotification2 extends LayoutChangedNotification {
/// size of the viewport, for instance. /// size of the viewport, for instance.
final BuildContext context; final BuildContext context;
/// The number of [Scrollable2] widgets that this notification has bubbled /// The number of viewports that this notification has bubbled through.
/// through. Typically listeners only respond to notifications with a [depth] ///
/// of zero. /// 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 get depth => _depth;
int _depth = 0; int _depth = 0;
@override @override
bool visitAncestor(Element element) { bool visitAncestor(Element element) {
if (element.widget is Scrollable2) if (element is RenderObjectElement && element.renderObject is RenderAbstractViewport)
_depth += 1; _depth += 1;
return super.visitAncestor(element); return super.visitAncestor(element);
} }
...@@ -93,7 +97,7 @@ abstract class ScrollNotification2 extends LayoutChangedNotification { ...@@ -93,7 +97,7 @@ abstract class ScrollNotification2 extends LayoutChangedNotification {
super.debugFillDescription(description); super.debugFillDescription(description);
description.add('$axisDirection'); description.add('$axisDirection');
description.add('metrics: $metrics'); description.add('metrics: $metrics');
description.add('depth: $depth'); description.add('depth: $depth (${ depth == 0 ? "local" : "remote"})');
} }
} }
......
...@@ -23,7 +23,7 @@ void main() { ...@@ -23,7 +23,7 @@ void main() {
TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0)); TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification, const isInstanceOf<ScrollStartNotification>()); expect(notification, const isInstanceOf<ScrollStartNotification>());
expect(notification.depth, equals(1)); expect(notification.depth, equals(0));
ScrollStartNotification start = notification; ScrollStartNotification start = notification;
expect(start.dragDetails, isNotNull); expect(start.dragDetails, isNotNull);
expect(start.dragDetails.globalPosition, equals(const Point(100.0, 100.0))); expect(start.dragDetails.globalPosition, equals(const Point(100.0, 100.0)));
...@@ -31,7 +31,7 @@ void main() { ...@@ -31,7 +31,7 @@ void main() {
await gesture.moveBy(const Offset(-10.0, -10.0)); await gesture.moveBy(const Offset(-10.0, -10.0));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification, const isInstanceOf<ScrollUpdateNotification>()); expect(notification, const isInstanceOf<ScrollUpdateNotification>());
expect(notification.depth, equals(1)); expect(notification.depth, equals(0));
ScrollUpdateNotification update = notification; ScrollUpdateNotification update = notification;
expect(update.dragDetails, isNotNull); expect(update.dragDetails, isNotNull);
expect(update.dragDetails.globalPosition, equals(const Point(90.0, 90.0))); expect(update.dragDetails.globalPosition, equals(const Point(90.0, 90.0)));
...@@ -40,7 +40,7 @@ void main() { ...@@ -40,7 +40,7 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
expect(notification, const isInstanceOf<ScrollEndNotification>()); expect(notification, const isInstanceOf<ScrollEndNotification>());
expect(notification.depth, equals(1)); expect(notification.depth, equals(0));
ScrollEndNotification end = notification; ScrollEndNotification end = notification;
expect(end.dragDetails, isNotNull); expect(end.dragDetails, isNotNull);
expect(end.dragDetails.velocity, equals(Velocity.zero)); expect(end.dragDetails.velocity, equals(Velocity.zero));
...@@ -93,9 +93,7 @@ void main() { ...@@ -93,9 +93,7 @@ void main() {
expect(depth0Types, equals(types)); expect(depth0Types, equals(types));
expect(depth1Types, equals(types)); expect(depth1Types, equals(types));
// These values might not be what we want in the end. expect(depth0Values, equals(<int>[0, 0, 0, 0, 0]));
// See <https://github.com/flutter/flutter/issues/8017>. expect(depth1Values, equals(<int>[1, 1, 1, 1, 1]));
expect(depth0Values, equals(<int>[1, 1, 1, 1, 1]));
expect(depth1Values, equals(<int>[2, 2, 2, 2, 2]));
}); });
} }
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