Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DropdownMenu overflow #147233

Merged
merged 4 commits into from May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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