From 8e50a17d944470f263ca3799e21a7ba2b5cfe2a4 Mon Sep 17 00:00:00 2001 From: PurplePolyhedron <120297255+PurplePolyhedron@users.noreply.github.com> Date: Fri, 23 Aug 2024 05:14:22 +0800 Subject: [PATCH] Stop `DropdownMenu` internal scrolling from moving parent `Scrollable` (#153360) fixes https://github.com/flutter/flutter/issues/151854 fixes #139113 The regression test uses nested `ListView` because I could not reproduce the issue without using nested `Scrollable`. The issue is masked when there is only one `Scrollable` outside `DropdownMenu`. While `Scrollable.ensureVisible` can find all the `Scrollable`, the actual scrolling is performed by `ScrollPosition.ensureVisible` , which uses the `RenderObject` tree to find nearest Viewport but it could not find one due to `OverlayPortal` putting the target `RenderObject` at different point. So no scrolling will occur. However when there are nested `Scrollable`, `Scrollable.ensureVisible` can scroll the outside `Scrollable` normally since no `RenderObject` tree gap exist between the two `Scrollable` --- .../lib/src/material/dropdown_menu.dart | 2 +- .../test/material/dropdown_menu_test.dart | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 97e89fd40b..a6cb62cff4 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -592,7 +592,7 @@ class _DropdownMenuState extends State> { WidgetsBinding.instance.addPostFrameCallback((_) { final BuildContext? highlightContext = buttonItemKeys[currentHighlight!].currentContext; if (highlightContext != null) { - Scrollable.ensureVisible(highlightContext); + Scrollable.of(highlightContext).position.ensureVisible(highlightContext.findRenderObject()!); } }, debugLabel: 'DropdownMenu.scrollToHighlight'); } diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index c285513e3a..b1527427a2 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2723,6 +2723,36 @@ void main() { ), ); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/151854. + testWidgets('scrollToHighlight does not scroll parent', (WidgetTester tester) async { + final ScrollController controller = ScrollController(); + addTearDown(controller.dispose); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ListView( + controller: controller, + children: [ + ListView( + shrinkWrap: true, + children: [DropdownMenu( + initialSelection: menuChildren.last.value, + dropdownMenuEntries: menuChildren, + )], + ), + const SizedBox(height: 1000.0), + ], + ), + ), + ), + ); + + await tester.tap(find.byType(TextField).first); + await tester.pumpAndSettle(); + expect(controller.offset, 0.0); + }); } enum TestMenu {