From 993f554e4c542bc2108b4f032383589b37aea44d Mon Sep 17 00:00:00 2001 From: sanni prasad Date: Tue, 19 Mar 2024 03:05:37 +0530 Subject: [PATCH] Fix for issue 140372 (#144947) *Continuing work from the PR https://github.com/flutter/flutter/pull/140373* Add the ability to set route settings on PopupMenuButton Fixes https://github.com/flutter/flutter/issues/140372 Added UTs as requested --- .../flutter/lib/src/material/popup_menu.dart | 7 ++++ .../test/material/popup_menu_test.dart | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 5083add60bd43c..9fc7d80d5430eb 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -1150,6 +1150,7 @@ class PopupMenuButton extends StatefulWidget { this.clipBehavior = Clip.none, this.useRootNavigator = false, this.popUpAnimationStyle, + this.routeSettings, this.style, }) : assert( !(child != null && icon != null), @@ -1345,6 +1346,11 @@ class PopupMenuButton extends StatefulWidget { /// If this is null, then the default animation will be used. final AnimationStyle? popUpAnimationStyle; + /// Optional route settings for the menu. + /// + /// See [RouteSettings] for details. + final RouteSettings? routeSettings; + /// Customizes this icon button's appearance. /// /// The [style] is only used for Material 3 [IconButton]s. If [ThemeData.useMaterial3] @@ -1412,6 +1418,7 @@ class PopupMenuButtonState extends State> { clipBehavior: widget.clipBehavior, useRootNavigator: widget.useRootNavigator, popUpAnimationStyle: widget.popUpAnimationStyle, + routeSettings: widget.routeSettings, ) .then((T? newValue) { if (!mounted) { diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index cfce4bc128a577..435344a3eb5459 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -926,6 +926,39 @@ void main() { expect(tester.getTopLeft(popupFinder), buttonTopLeft); }); + testWidgets('Popup menu with RouteSettings', (WidgetTester tester) async { + final Key buttonKey = UniqueKey(); + const RouteSettings popupRoute = RouteSettings(name: '/popup'); + late RouteSettings currentRouteSetting; + + await tester.pumpWidget( + MaterialApp( + navigatorObservers: [ + _ClosureNavigatorObserver(onDidChange: (Route newRoute) { + currentRouteSetting = newRoute.settings; + }), + ], + home: Scaffold( + body: PopupMenuButton( + key: buttonKey, + routeSettings: popupRoute, + itemBuilder: (_) => >[ + const PopupMenuItem(value: 1, child: Text('Item 1')), + const PopupMenuItem(value: 2, child: Text('Item 2')), + ], + child: const Text('Show Menu'), + ), + ), + ), + ); + + final Finder buttonFinder = find.byKey(buttonKey); + await tester.tap(buttonFinder); + await tester.pumpAndSettle(); + + expect(currentRouteSetting, popupRoute); + }); + testWidgets('PopupMenu positioning around display features', (WidgetTester tester) async { final Key buttonKey = UniqueKey();