From 9ee3fb813fc1e949c5ea2331ce4b25e50a57d0f0 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Fri, 27 Sep 2024 09:18:35 +0200 Subject: [PATCH] Fix DropdownMenu rendered behind AppBar (#155539) ## Description This PR changes the `MenuAnchor` implementation in order to always show the menu on the root overlay. Doing so menus can't be hidden by other widgets especially when using go_router. See [[go_router] DropdownMenu behind NavigationBar](https://github.com/flutter/flutter/issues/155034) where the DropdownMenu menu was displayed behind the navigation bar. I did not make this configurable for the moment to avoid introducing a new parameter until there is a clear use case for it. ## Related Issue Fixes [[go_router] DropdownMenu behind NavigationBar](https://github.com/flutter/flutter/issues/155034). ## Tests Adds 1 test. --- .../flutter/lib/src/material/menu_anchor.dart | 2 +- .../test/material/menu_anchor_test.dart | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index f5e118cdac..134023ea27 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -403,7 +403,7 @@ class _MenuAnchorState extends State { ); } - Widget child = OverlayPortal( + Widget child = OverlayPortal.targetsRootOverlay( controller: _overlayController, overlayChildBuilder: (BuildContext context) { return _Submenu( diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index 5c1bf9981a..86e7c37cc2 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -4586,6 +4586,77 @@ void main() { await tester.pumpWidget(buildButton(enabled: false)); expect(iconStyle(tester, Icons.add).color, disabledIconColor); }); + + // Regression test for https://github.com/flutter/flutter/issues/155034. + testWidgets('Content is shown in the root overlay', (WidgetTester tester) async { + final MenuController controller = MenuController(); + final UniqueKey overlayKey = UniqueKey(); + final UniqueKey menuItemKey = UniqueKey(); + + List ancestorRenderTheaters(RenderObject child) { + final List results = []; + RenderObject? node = child; + while (node != null) { + if (node.runtimeType.toString() == '_RenderTheater') { + results.add(node); + } + final RenderObject? parent = node.parent; + node = parent is RenderObject? parent : null; + } + return results; + } + + Widget boilerplate() { + return MaterialApp( + home: Overlay( + key: overlayKey, + initialEntries: [ + OverlayEntry( + builder: (BuildContext context) { + return Scaffold( + body: Center( + child: MenuAnchor( + controller: controller, + menuChildren: [ + MenuItemButton( + key: menuItemKey, + onPressed: () {}, + child: const Text('Item 1'), + ), + ], + ), + ), + ); + } + ), + ], + ), + ); + } + + await tester.pumpWidget(boilerplate()); + expect(find.byKey(menuItemKey), findsNothing); + + // Open the menu. + controller.open(); + await tester.pump(); + expect(find.byKey(menuItemKey), findsOne); + + // Expect two overlays: the root overlay created by MaterialApp and the + // overlay created by the boilerplate code. + expect(find.byType(Overlay), findsNWidgets(2)); + + final Iterable overlays = tester.widgetList(find.byType(Overlay)); + final Overlay nonRootOverlay = tester.widget(find.byKey(overlayKey)); + final Overlay rootOverlay = overlays.firstWhere((Overlay overlay) => overlay != nonRootOverlay); + + // Check that the ancestor _RenderTheater for the menu item is the one + // from the root overlay. + expect( + ancestorRenderTheaters(tester.renderObject(find.byKey(menuItemKey))).single, + tester.renderObject(find.byWidget(rootOverlay)), + ); + }); } List createTestMenus({