From 00bbf5434c0d9fe417906927117a5fc785661172 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Fri, 28 Apr 2023 23:21:54 +0800 Subject: [PATCH] Allow users to provide route settings to `showMenu` (#124935) Background: I am adding logging to things like dialogs, bottom sheets and menus. Then I realized that, showMenu does not allow users to provide RouteSettings, while showDialog and showModalBottomSheet both allow. Therefore, IMHO a consistent API design may need to add this to showMenu. I will add tests if this proposal looks OK :) --- .../flutter/lib/src/material/popup_menu.dart | 3 + .../test/material/popup_menu_test.dart | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index c2e12c355b..4f7c95d1af 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -789,6 +789,7 @@ class _PopupMenuRoute extends PopupRoute { required this.capturedThemes, this.constraints, required this.clipBehavior, + super.settings, }) : itemSizes = List.filled(items.length, null), // Menus always cycle focus through their items irrespective of the // focus traversal edge behavior set in the Navigator. @@ -949,6 +950,7 @@ Future showMenu({ bool useRootNavigator = false, BoxConstraints? constraints, Clip clipBehavior = Clip.none, + RouteSettings? routeSettings, }) { assert(items.isNotEmpty); assert(debugCheckHasMaterialLocalizations(context)); @@ -979,6 +981,7 @@ Future showMenu({ capturedThemes: InheritedTheme.capture(from: context, to: navigator.context), constraints: constraints, clipBehavior: clipBehavior, + settings: routeSettings, )); } diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 1423117e5f..0772a953d7 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -3180,6 +3180,48 @@ void main() { paints..rrect(color: const Color(0xff0000ff)), ); }, variant: TargetPlatformVariant.desktop()); + + testWidgets('Popup menu with RouteSettings', (WidgetTester tester) async { + late RouteSettings currentRouteSetting; + + await tester.pumpWidget( + MaterialApp( + navigatorObservers: [ + _ClosureNavigatorObserver(onDidChange: (Route newRoute) { + currentRouteSetting = newRoute.settings; + }), + ], + home: const Material( + child: Center( + child: ElevatedButton( + onPressed: null, + child: Text('Go'), + ), + ), + ), + ), + ); + + final BuildContext context = tester.element(find.text('Go')); + const RouteSettings exampleSetting = RouteSettings(name: 'simple'); + + showMenu( + context: context, + position: RelativeRect.fill, + items: const >[ + PopupMenuItem(child: Text('foo')), + ], + routeSettings: exampleSetting, + ); + + await tester.pumpAndSettle(); + expect(find.text('foo'), findsOneWidget); + expect(currentRouteSetting, exampleSetting); + + await tester.tap(find.text('foo')); + await tester.pumpAndSettle(); + expect(currentRouteSetting.name, '/'); + }); } class TestApp extends StatelessWidget { @@ -3232,3 +3274,21 @@ class MenuObserver extends NavigatorObserver { super.didPush(route, previousRoute); } } + +class _ClosureNavigatorObserver extends NavigatorObserver { + _ClosureNavigatorObserver({required this.onDidChange}); + + final void Function(Route newRoute) onDidChange; + + @override + void didPush(Route route, Route? previousRoute) => onDidChange(route); + + @override + void didPop(Route route, Route? previousRoute) => onDidChange(previousRoute!); + + @override + void didRemove(Route route, Route? previousRoute) => onDidChange(previousRoute!); + + @override + void didReplace({Route? newRoute, Route? oldRoute}) => onDidChange(newRoute!); +}