diff --git a/packages/flutter/lib/src/widgets/page_view.dart b/packages/flutter/lib/src/widgets/page_view.dart index e67195e182..aa7ad9e005 100644 --- a/packages/flutter/lib/src/widgets/page_view.dart +++ b/packages/flutter/lib/src/widgets/page_view.dart @@ -341,17 +341,9 @@ class _PagePosition extends ScrollPositionWithSingleContext implements PageMetri RenderObject? targetRenderObject, }) { // Since the _PagePosition is intended to cover the available space within - // its viewport, stop trying to move the target render object to the center - // - otherwise, could end up changing which page is visible and moving the - // targetRenderObject out of the viewport. - return super.ensureVisible( - object, - alignment: alignment, - duration: duration, - curve: curve, - alignmentPolicy: alignmentPolicy, - targetRenderObject: null, - ); + // its viewport, stop trying to any scroll, otherwise, could end up changing + // which page is visible and moving the render object out of the viewport. + return Future.value(); } @override diff --git a/packages/flutter/test/widgets/scrollable_test.dart b/packages/flutter/test/widgets/scrollable_test.dart index 6e48059423..f162e8b162 100644 --- a/packages/flutter/test/widgets/scrollable_test.dart +++ b/packages/flutter/test/widgets/scrollable_test.dart @@ -1006,6 +1006,76 @@ void main() { expect(targetMidRightPage1, findsOneWidget); expect(targetMidLeftPage1, findsOneWidget); }); + + testWidgets('ensureVisible does not move PageViews when there are objects between pageView and target object', (WidgetTester tester) async { + final PageController controller = PageController(); + int count = 0; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: PageView( + controller: controller, + children: List.generate(3, (int index) { + return Row( + children: [ + Container( + width: 400, + color: Colors.red, + ), + Expanded( + child: Container( + color: Colors.green, + width: double.infinity, + height: 50, + child: SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: Container( + key: Key(index.toString()), + color: Colors.yellow, + height: 50, + width: 200, + ), + ), + ), + ) + ], + ); + }), + ), + ), + ); + + controller.position.addListener(() { + count++; + }); + + final Finder targetOfPage0 = find.byKey(const Key('0')); + final Finder targetOfPage1 = find.byKey(const Key('1')); + + expect(targetOfPage0, findsOneWidget); + expect(targetOfPage1, findsNothing); + + // `ensureVisible` should not trigger any scrolling or page changing of pageView. + await tester.ensureVisible(targetOfPage0); + await tester.pumpAndSettle(); + expect(count, 0); + expect(targetOfPage0, findsOneWidget); + expect(targetOfPage1, findsNothing); + + controller.jumpToPage(1); + await tester.pumpAndSettle(); + + expect(count, 1); // Trigger by `controller.jumpToPage(1)` + expect(targetOfPage0, findsNothing); + expect(targetOfPage1, findsOneWidget); + + await tester.ensureVisible(targetOfPage1); + await tester.pumpAndSettle(); + expect(count, 1); + expect(targetOfPage0, findsNothing); + expect(targetOfPage1, findsOneWidget); + }); } // ignore: must_be_immutable