diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index fafe76e0304..8258aceb854 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -905,9 +905,11 @@ class _RenderDropdownMenuBody extends RenderBox double? maxHeight; RenderBox? child = firstChild; + final double intrinsicWidth = width ?? getMaxIntrinsicWidth(constraints.maxHeight); + final double widthConstraint = math.min(intrinsicWidth, constraints.maxWidth); final BoxConstraints innerConstraints = BoxConstraints( - maxWidth: width ?? getMaxIntrinsicWidth(constraints.maxWidth), - maxHeight: getMaxIntrinsicHeight(constraints.maxHeight), + maxWidth: widthConstraint, + maxHeight: getMaxIntrinsicHeight(widthConstraint), ); while (child != null) { if (child == firstChild) { @@ -947,9 +949,11 @@ class _RenderDropdownMenuBody extends RenderBox double maxWidth = 0.0; double? maxHeight; RenderBox? child = firstChild; + final double intrinsicWidth = width ?? getMaxIntrinsicWidth(constraints.maxHeight); + final double widthConstraint = math.min(intrinsicWidth, constraints.maxWidth); final BoxConstraints innerConstraints = BoxConstraints( - maxWidth: width ?? getMaxIntrinsicWidth(constraints.maxWidth), - maxHeight: getMaxIntrinsicHeight(constraints.maxHeight), + maxWidth: widthConstraint, + maxHeight: getMaxIntrinsicHeight(widthConstraint), ); while (child != null) { diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index fe2ef9537c8..1f99e087e25 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2129,6 +2129,60 @@ void main() { // No exception should be thrown. expect(tester.takeException(), isNull); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/147076. + testWidgets('Text field does not overflow parent', (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: SizedBox( + width: 300, + child: DropdownMenu( + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: 0, + label: 'This is a long text that is multiplied by 4 so it can overflow. ' * 4, + ), + ], + ), + ), + ), + )); + + await tester.pump(); + final RenderBox box = tester.firstRenderObject(find.byType(TextField)); + expect(box.size.width, 300.0); + }); + + // This is a regression test for https://github.com/flutter/flutter/issues/147173. + testWidgets('Text field with large helper text can be selected', (WidgetTester tester) async { + const String labelText = 'MenuEntry 1'; + await tester.pumpWidget(const MaterialApp( + home: Scaffold( + body: Center( + child: DropdownMenu( + hintText: 'Hint text', + helperText: 'Menu Helper text', + inputDecorationTheme: InputDecorationTheme( + helperMaxLines: 2, + helperStyle: TextStyle(fontSize: 30), + ), + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: 0, + label: labelText, + ), + ], + ), + ), + ), + )); + + await tester.pump(); + await tester.tapAt(tester.getCenter(find.text('Hint text'))); + await tester.pumpAndSettle(); + // One is layout for the _DropdownMenuBody, the other one is the real button item in the menu. + expect(find.widgetWithText(MenuItemButton, labelText), findsNWidgets(2)); + }); } enum TestMenu {