diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index aad2210faa..1de959f1a5 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -967,11 +967,19 @@ class _DropdownMenuState extends State> { final double? anchorWidth = getWidth(_anchorKey); if (widget.width != null) { effectiveMenuStyle = effectiveMenuStyle.copyWith( - minimumSize: MaterialStatePropertyAll(Size(widget.width!, 0.0)), + minimumSize: MaterialStateProperty.resolveWith((Set states) { + final double? effectiveMaximumWidth = + effectiveMenuStyle!.maximumSize?.resolve(states)?.width; + return Size(math.min(widget.width!, effectiveMaximumWidth ?? 0.0), 0.0); + }), ); } else if (anchorWidth != null) { effectiveMenuStyle = effectiveMenuStyle.copyWith( - minimumSize: MaterialStatePropertyAll(Size(anchorWidth, 0.0)), + minimumSize: MaterialStateProperty.resolveWith((Set states) { + final double? effectiveMaximumWidth = + effectiveMenuStyle!.maximumSize?.resolve(states)?.width; + return Size(math.min(anchorWidth, effectiveMaximumWidth ?? 0.0), 0.0); + }), ); } diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 39e06805c8..529557a7ab 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -3952,6 +3952,52 @@ void main() { textField = tester.widget(find.byType(TextField)); expect(textField.textInputAction, TextInputAction.next); }); + + testWidgets('items can be constrainted to be smaller than the text field with menuStyle', ( + WidgetTester tester, + ) async { + const String longLabel = 'This is a long text that it can overflow.'; + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: DropdownMenu( + dropdownMenuEntries: >[ + DropdownMenuEntry(value: 0, label: longLabel), + ], + menuStyle: MenuStyle(maximumSize: WidgetStatePropertyAll(Size(150.0, 50.0))), + ), + ), + ), + ); + + await tester.tap(find.byType(TextField)); + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + expect(tester.getSize(findMenuItemButton(longLabel)).width, 150.0); + + // The overwrite of menuStyle is different when a width is provided, + // So it needs to be tested separately. + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownMenu( + width: 200.0, + dropdownMenuEntries: menuChildren, + menuStyle: const MenuStyle( + maximumSize: WidgetStatePropertyAll(Size(150.0, 50.0)), + ), + ), + ), + ), + ); + + await tester.tap(find.byType(TextField)); + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + expect(tester.getSize(findMenuItemButton(menuChildren.first.label)).width, 150.0); + }); } enum TestMenu {