Fix the depth of scroll notifications (#8156)
Fixes https://github.com/flutter/flutter/issues/8017
This commit is contained in:
@@ -726,7 +726,7 @@ class _TabBarViewState extends State<TabBarView> {
|
||||
if (_warpUnderwayCount > 0)
|
||||
return false;
|
||||
|
||||
if (notification.depth != 1)
|
||||
if (notification.depth != 0)
|
||||
return false;
|
||||
|
||||
if (notification is ScrollStartNotification) {
|
||||
|
||||
@@ -188,7 +188,7 @@ class PageView extends BoxScrollView {
|
||||
final Widget scrollable = super.build(context);
|
||||
return new NotificationListener<ScrollNotification2>(
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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"})');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ScrollStartNotification>());
|
||||
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<ScrollUpdateNotification>());
|
||||
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<ScrollEndNotification>());
|
||||
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 <https://github.com/flutter/flutter/issues/8017>.
|
||||
expect(depth0Values, equals(<int>[1, 1, 1, 1, 1]));
|
||||
expect(depth1Values, equals(<int>[2, 2, 2, 2, 2]));
|
||||
expect(depth0Values, equals(<int>[0, 0, 0, 0, 0]));
|
||||
expect(depth1Values, equals(<int>[1, 1, 1, 1, 1]));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user