Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix popup menu offset when using useRootNavigator in PopupMenu #144670

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/flutter/lib/src/material/popup_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 1439,10 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
void showButtonMenu() {
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
final RenderBox button = context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(
context,
rootNavigator: widget.useRootNavigator,
).overlay!.context.findRenderObject()! as RenderBox;
final PopupMenuPosition popupMenuPosition = widget.position ?? popupMenuTheme.position ?? PopupMenuPosition.over;
late Offset offset;
switch (popupMenuPosition) {
Expand Down
45 changes: 45 additions & 0 deletions packages/flutter/test/material/popup_menu_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 927,51 @@ void main() {
expect(tester.getTopLeft(popupFinder), buttonTopLeft);
});

testWidgets('PopupMenu positioning inside nested Navigator when useRootNavigator',
(WidgetTester tester) async {
final Key buttonKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Example')),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Navigator(
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute<dynamic>(
builder: (BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: PopupMenuButton<int>(
key: buttonKey,
useRootNavigator: true,
itemBuilder: (_) => <PopupMenuItem<int>>[
const PopupMenuItem<int>(value: 1, child: Text('Item 1')),
const PopupMenuItem<int>(value: 2, child: Text('Item 2')),
],
child: const Text('Show Menu'),
),
),
);
},
);
},
),
),
),
),
);

final Finder buttonFinder = find.byKey(buttonKey);
final Finder popupFinder = find.bySemanticsLabel('Popup menu');
await tester.tap(buttonFinder);
await tester.pumpAndSettle();

final Offset buttonTopLeft = tester.getTopLeft(buttonFinder);
expect(tester.getTopLeft(popupFinder), buttonTopLeft);
});

testWidgets('Popup menu with RouteSettings', (WidgetTester tester) async {
final Key buttonKey = UniqueKey();
const RouteSettings popupRoute = RouteSettings(name: '/popup');
Expand Down