From ccaf51562fbc7c383a1f8a6061deb8696c2d115d Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 15 Feb 2022 09:45:18 +0200 Subject: [PATCH] Add custom size constraints parameter to `PopupMenu` (#97798) --- .../flutter/lib/src/material/popup_menu.dart | 32 ++++++++++++++++-- .../test/material/popup_menu_test.dart | 33 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 5fe18a5338..b8a9e565ab 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -529,10 +529,12 @@ class _PopupMenu extends StatelessWidget { Key? key, required this.route, required this.semanticLabel, + this.constraints, }) : super(key: key); final _PopupMenuRoute route; final String? semanticLabel; + final BoxConstraints? constraints; @override Widget build(BuildContext context) { @@ -572,7 +574,7 @@ class _PopupMenu extends StatelessWidget { final CurveTween height = CurveTween(curve: Interval(0.0, unit * route.items.length)); final Widget child = ConstrainedBox( - constraints: const BoxConstraints( + constraints: constraints ?? const BoxConstraints( minWidth: _kMenuMinWidth, maxWidth: _kMenuMaxWidth, ), @@ -735,6 +737,7 @@ class _PopupMenuRoute extends PopupRoute { this.shape, this.color, required this.capturedThemes, + this.constraints, }) : itemSizes = List.filled(items.length, null); final RelativeRect position; @@ -746,6 +749,7 @@ class _PopupMenuRoute extends PopupRoute { final ShapeBorder? shape; final Color? color; final CapturedThemes capturedThemes; + final BoxConstraints? constraints; @override Animation createAnimation() { @@ -779,7 +783,11 @@ class _PopupMenuRoute extends PopupRoute { } } - final Widget menu = _PopupMenu(route: this, semanticLabel: semanticLabel); + final Widget menu = _PopupMenu( + route: this, + semanticLabel: semanticLabel, + constraints: constraints, + ); final MediaQueryData mediaQuery = MediaQuery.of(context); return MediaQuery.removePadding( context: context, @@ -870,6 +878,7 @@ Future showMenu({ ShapeBorder? shape, Color? color, bool useRootNavigator = false, + BoxConstraints? constraints, }) { assert(context != null); assert(position != null); @@ -899,6 +908,7 @@ Future showMenu({ shape: shape, color: color, capturedThemes: InheritedTheme.capture(from: context, to: navigator.context), + constraints: constraints, )); } @@ -966,6 +976,7 @@ class PopupMenuButton extends StatefulWidget { this.shape, this.color, this.enableFeedback, + this.constraints, }) : assert(itemBuilder != null), assert(offset != null), assert(enabled != null), @@ -1072,6 +1083,22 @@ class PopupMenuButton extends StatefulWidget { /// If this property is null, the default size is 24.0 pixels. final double? iconSize; + /// Optional size constraints for the menu. + /// + /// When unspecified, defaults to: + /// ```dart + /// const BoxConstraints( + /// minWidth: 2.0 * 56.0, + /// maxWidth: 5.0 * 56.0, + /// ) + /// ``` + /// + /// The default constraints ensure that the menu width matches maximum width + /// recommended by the material design guidelines. + /// Specifying this parameter enables creation of menu wider than + /// the default maximum width. + final BoxConstraints? constraints; + @override PopupMenuButtonState createState() => PopupMenuButtonState(); } @@ -1111,6 +1138,7 @@ class PopupMenuButtonState extends State> { position: position, shape: widget.shape ?? popupMenuTheme.shape, color: widget.color ?? popupMenuTheme.color, + constraints: widget.constraints, ) .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 05ab49f58f..7bcd82f8e5 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -2536,6 +2536,39 @@ void main() { expect(tester.widget(find.byType(InkWell)).radius, testSplashRadius); }); + + testWidgets('Can override menu size constraints', (WidgetTester tester) async { + final Key popupMenuButtonKey = UniqueKey(); + final Type menuItemType = const PopupMenuItem(child: Text('item')).runtimeType; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: PopupMenuButton( + key: popupMenuButtonKey, + constraints: const BoxConstraints( + minWidth: 500, + ), + itemBuilder: (_) => >[ + const PopupMenuItem( + value: 'value', + child: Text('Item 0'), + ), + ], + ), + ), + ), + ), + ); + + // Show the menu + await tester.tap(find.byKey(popupMenuButtonKey)); + await tester.pumpAndSettle(); + + expect(tester.getSize(find.widgetWithText(menuItemType, 'Item 0')).height, 48); + expect(tester.getSize(find.widgetWithText(menuItemType, 'Item 0')).width, 500); + }); } class TestApp extends StatefulWidget {