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.
This commit is contained in:
Bruno Leroux
2024-09-27 09:18:35 +02:00
committed by GitHub
parent 7bb9352911
commit 9ee3fb813f
2 changed files with 72 additions and 1 deletions

View File

@@ -403,7 +403,7 @@ class _MenuAnchorState extends State<MenuAnchor> {
);
}
Widget child = OverlayPortal(
Widget child = OverlayPortal.targetsRootOverlay(
controller: _overlayController,
overlayChildBuilder: (BuildContext context) {
return _Submenu(

View File

@@ -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<RenderObject> ancestorRenderTheaters(RenderObject child) {
final List<RenderObject> results = <RenderObject>[];
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>[
OverlayEntry(
builder: (BuildContext context) {
return Scaffold(
body: Center(
child: MenuAnchor(
controller: controller,
menuChildren: <Widget>[
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<Overlay> overlays = tester.widgetList<Overlay>(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<Widget> createTestMenus({