Skip to content

Commit

Permalink
fix DropdownMenu overflow (#147233)
Browse files Browse the repository at this point in the history
fix `DropdownMenu` overflow issue in  #147076 and #147173
However I believe the the menu placement issue in #147076 is a separate issue. It is not fixed here.

fixes #147173
  • Loading branch information
PurplePolyhedron committed May 1, 2024
1 parent f256f68 commit 68f8b53
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/flutter/lib/src/material/dropdown_menu.dart
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
54 changes: 54 additions & 0 deletions packages/flutter/test/material/dropdown_menu_test.dart
Expand Up @@ -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<int>(
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(
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<int>(
hintText: 'Hint text',
helperText: 'Menu Helper text',
inputDecorationTheme: InputDecorationTheme(
helperMaxLines: 2,
helperStyle: TextStyle(fontSize: 30),
),
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(
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 {
Expand Down

0 comments on commit 68f8b53

Please sign in to comment.