Skip to content

Commit adba0ff

Browse files
QuncCcccccexaby73
authored andcommitted
Fix menu width issue for DropdownMenu (flutter#123823)
1 parent 1d8decb commit adba0ff

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'menu_anchor.dart';
1919
import 'menu_style.dart';
2020
import 'text_field.dart';
2121
import 'theme.dart';
22+
import 'theme_data.dart';
2223

2324

2425
// Navigation shortcuts to move the selected menu items up or down.
@@ -535,10 +536,10 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
535536
);
536537

537538
return _DropdownMenuBody(
538-
key: _anchorKey,
539539
width: widget.width,
540540
children: <Widget>[
541541
TextField(
542+
key: _anchorKey,
542543
mouseCursor: effectiveMouseCursor,
543544
canRequestFocus: canRequestFocus(),
544545
enableInteractiveSelection: canRequestFocus(),
@@ -607,7 +608,6 @@ class _ArrowDownIntent extends Intent {
607608

608609
class _DropdownMenuBody extends MultiChildRenderObjectWidget {
609610
const _DropdownMenuBody({
610-
super.key,
611611
super.children,
612612
this.width,
613613
});
@@ -826,6 +826,7 @@ class _DropdownMenuDefaultsM3 extends DropdownMenuThemeData {
826826
return const MenuStyle(
827827
minimumSize: MaterialStatePropertyAll<Size>(Size(_kMinimumWidth, 0.0)),
828828
maximumSize: MaterialStatePropertyAll<Size>(Size.infinite),
829+
visualDensity: VisualDensity.standard,
829830
);
830831
}
831832

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,58 @@ void main() {
10581058
await gesture.moveTo(tester.getCenter(textFieldFinder));
10591059
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
10601060
});
1061+
1062+
testWidgets('The menu has the same width as the input field in ListView', (WidgetTester tester) async {
1063+
// Regression test for https://github.com/flutter/flutter/issues/123631
1064+
await tester.pumpWidget(MaterialApp(
1065+
home: Scaffold(
1066+
body: ListView(
1067+
children: <Widget>[
1068+
DropdownMenu<TestMenu>(
1069+
dropdownMenuEntries: menuChildren,
1070+
),
1071+
],
1072+
),
1073+
),
1074+
));
1075+
1076+
final Rect textInput = tester.getRect(find.byType(TextField));
1077+
1078+
await tester.tap(find.byType(TextField));
1079+
await tester.pumpAndSettle();
1080+
1081+
final Finder findMenu = find.byWidgetPredicate((Widget widget) {
1082+
return widget.runtimeType.toString() == '_MenuPanel';
1083+
});
1084+
final Rect menu = tester.getRect(findMenu);
1085+
expect(textInput.width, menu.width);
1086+
1087+
await tester.pumpWidget(Container());
1088+
await tester.pumpWidget(MaterialApp(
1089+
home: Scaffold(
1090+
body: ListView(
1091+
children: <Widget>[
1092+
DropdownMenu<TestMenu>(
1093+
width: 200,
1094+
dropdownMenuEntries: menuChildren,
1095+
),
1096+
],
1097+
),
1098+
),
1099+
));
1100+
1101+
final Rect textInput1 = tester.getRect(find.byType(TextField));
1102+
1103+
await tester.tap(find.byType(TextField));
1104+
await tester.pumpAndSettle();
1105+
1106+
final Finder findMenu1 = find.byWidgetPredicate((Widget widget) {
1107+
return widget.runtimeType.toString() == '_MenuPanel';
1108+
});
1109+
final Rect menu1 = tester.getRect(findMenu1);
1110+
expect(textInput1.width, 200);
1111+
expect(menu1.width, 200);
1112+
});
10611113
}
10621114

10631115
enum TestMenu {

0 commit comments

Comments
 (0)