fix DropdownMenu overflow (#147233)

fix `DropdownMenu` overflow issue in  https://github.com/flutter/flutter/issues/147076 and https://github.com/flutter/flutter/issues/147173
However I believe the the menu placement issue in https://github.com/flutter/flutter/issues/147076 is a separate issue. It is not fixed here.

fixes https://github.com/flutter/flutter/issues/147173
This commit is contained in:
PurplePolyhedron 2024-05-02 07:24:58 +10:00 committed by GitHub
parent f256f68831
commit 68f8b536e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 4 deletions

View File

@ -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) {

View File

@ -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 {