Skip to content

Commit 68f8b53

Browse files
fix DropdownMenu overflow (#147233)
fix `DropdownMenu` overflow issue in flutter/flutter#147076 and flutter/flutter#147173 However I believe the the menu placement issue in flutter/flutter#147076 is a separate issue. It is not fixed here. fixes flutter/flutter#147173
1 parent f256f68 commit 68f8b53

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

packages/flutter/lib/src/material/dropdown_menu.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,11 @@ class _RenderDropdownMenuBody extends RenderBox
905905
double? maxHeight;
906906
RenderBox? child = firstChild;
907907

908+
final double intrinsicWidth = width ?? getMaxIntrinsicWidth(constraints.maxHeight);
909+
final double widthConstraint = math.min(intrinsicWidth, constraints.maxWidth);
908910
final BoxConstraints innerConstraints = BoxConstraints(
909-
maxWidth: width ?? getMaxIntrinsicWidth(constraints.maxWidth),
910-
maxHeight: getMaxIntrinsicHeight(constraints.maxHeight),
911+
maxWidth: widthConstraint,
912+
maxHeight: getMaxIntrinsicHeight(widthConstraint),
911913
);
912914
while (child != null) {
913915
if (child == firstChild) {
@@ -947,9 +949,11 @@ class _RenderDropdownMenuBody extends RenderBox
947949
double maxWidth = 0.0;
948950
double? maxHeight;
949951
RenderBox? child = firstChild;
952+
final double intrinsicWidth = width ?? getMaxIntrinsicWidth(constraints.maxHeight);
953+
final double widthConstraint = math.min(intrinsicWidth, constraints.maxWidth);
950954
final BoxConstraints innerConstraints = BoxConstraints(
951-
maxWidth: width ?? getMaxIntrinsicWidth(constraints.maxWidth),
952-
maxHeight: getMaxIntrinsicHeight(constraints.maxHeight),
955+
maxWidth: widthConstraint,
956+
maxHeight: getMaxIntrinsicHeight(widthConstraint),
953957
);
954958

955959
while (child != null) {

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,60 @@ void main() {
21292129
// No exception should be thrown.
21302130
expect(tester.takeException(), isNull);
21312131
});
2132+
2133+
// This is a regression test for https://github.com/flutter/flutter/issues/147076.
2134+
testWidgets('Text field does not overflow parent', (WidgetTester tester) async {
2135+
await tester.pumpWidget(MaterialApp(
2136+
home: Scaffold(
2137+
body: SizedBox(
2138+
width: 300,
2139+
child: DropdownMenu<int>(
2140+
dropdownMenuEntries: <DropdownMenuEntry<int>>[
2141+
DropdownMenuEntry<int>(
2142+
value: 0,
2143+
label: 'This is a long text that is multiplied by 4 so it can overflow. ' * 4,
2144+
),
2145+
],
2146+
),
2147+
),
2148+
),
2149+
));
2150+
2151+
await tester.pump();
2152+
final RenderBox box = tester.firstRenderObject(find.byType(TextField));
2153+
expect(box.size.width, 300.0);
2154+
});
2155+
2156+
// This is a regression test for https://github.com/flutter/flutter/issues/147173.
2157+
testWidgets('Text field with large helper text can be selected', (WidgetTester tester) async {
2158+
const String labelText = 'MenuEntry 1';
2159+
await tester.pumpWidget(const MaterialApp(
2160+
home: Scaffold(
2161+
body: Center(
2162+
child: DropdownMenu<int>(
2163+
hintText: 'Hint text',
2164+
helperText: 'Menu Helper text',
2165+
inputDecorationTheme: InputDecorationTheme(
2166+
helperMaxLines: 2,
2167+
helperStyle: TextStyle(fontSize: 30),
2168+
),
2169+
dropdownMenuEntries: <DropdownMenuEntry<int>>[
2170+
DropdownMenuEntry<int>(
2171+
value: 0,
2172+
label: labelText,
2173+
),
2174+
],
2175+
),
2176+
),
2177+
),
2178+
));
2179+
2180+
await tester.pump();
2181+
await tester.tapAt(tester.getCenter(find.text('Hint text')));
2182+
await tester.pumpAndSettle();
2183+
// One is layout for the _DropdownMenuBody, the other one is the real button item in the menu.
2184+
expect(find.widgetWithText(MenuItemButton, labelText), findsNWidgets(2));
2185+
});
21322186
}
21332187

21342188
enum TestMenu {

0 commit comments

Comments
 (0)