Skip to content

Commit b3de00a

Browse files
authored
Fix DropdownMenu with expandedInsets always aligned on top (#156214)
Fixes [DropdownMenu can not be center aligned when using expandedInsets ](flutter/flutter#155581) ### Code sample <details> <summary>expand to view the code sample</summary> ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @OverRide Widget build(BuildContext context) { final List<DropdownMenuEntry<ShortMenu>> shortMenuItems = <DropdownMenuEntry<ShortMenu>>[]; for (final ShortMenu value in ShortMenu.values) { final DropdownMenuEntry<ShortMenu> entry = DropdownMenuEntry<ShortMenu>(value: value, label: value.label); shortMenuItems.add(entry); } return MaterialApp( home: Scaffold( body: Row( children: <Widget>[ Expanded( child: Center( child: DropdownMenu<ShortMenu>( expandedInsets: const EdgeInsets.all(16), initialSelection: ShortMenu.item0, dropdownMenuEntries: shortMenuItems, label: const Text('With expandedInsets'), ), ), ), Expanded( child: Center( child: DropdownMenu<ShortMenu>( initialSelection: ShortMenu.item0, dropdownMenuEntries: shortMenuItems, label: const Text('Without expandedInsets'), ), ), ), ], ), ), ); } } enum ShortMenu { item0('Menu 0'), item1('Menu 1'), item2('Menu 2'); const ShortMenu(this.label); final String label; } ``` </details> ### Before (`DropdownMenu` without `expandedInsets` cannot be centered) <img width="770" alt="Screenshot 2024-10-04 at 14 13 58" src="https://github.com/user-attachments/assets/c7520c12-d16a-4867-8fae-38b75dbc4225"> ### After (`DropdownMenu` with `expandedInsets` be centered) <img width="770" alt="Screenshot 2024-10-04 at 14 13 49" src="https://github.com/user-attachments/assets/82e0b81e-5c85-4e59-99b8-df329459773b">
1 parent ef4807b commit b3de00a

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
972972
),
973973
),
974974
),
975-
child: Align(
976-
alignment: AlignmentDirectional.topStart,
977-
child: menuAnchor,
978-
),
975+
child: menuAnchor,
979976
);
980977
}
981978

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,43 @@ void main() {
34153415
tester.getRect(find.byType(TextField).first).bottom,
34163416
);
34173417
});
3418+
3419+
testWidgets('DropdownMenu with expandedInsets can be aligned', (WidgetTester tester) async {
3420+
Widget buildMenuAnchor({ AlignmentGeometry alignment = Alignment.topCenter }) {
3421+
return MaterialApp(
3422+
home: Scaffold(
3423+
body: Row(
3424+
children: <Widget>[
3425+
Expanded(
3426+
child: Align(
3427+
alignment: alignment,
3428+
child: DropdownMenu<TestMenu>(
3429+
expandedInsets: const EdgeInsets.all(16),
3430+
dropdownMenuEntries: menuChildren,
3431+
),
3432+
),
3433+
),
3434+
],
3435+
),
3436+
),
3437+
);
3438+
}
3439+
3440+
await tester.pumpWidget(buildMenuAnchor());
3441+
3442+
Offset textFieldPosition = tester.getTopLeft(find.byType(TextField));
3443+
expect(textFieldPosition, equals(const Offset(16.0, 0.0)));
3444+
3445+
await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.center));
3446+
3447+
textFieldPosition = tester.getTopLeft(find.byType(TextField));
3448+
expect(textFieldPosition, equals(const Offset(16.0, 272.0)));
3449+
3450+
await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.bottomCenter));
3451+
3452+
textFieldPosition = tester.getTopLeft(find.byType(TextField));
3453+
expect(textFieldPosition, equals(const Offset(16.0, 544.0)));
3454+
});
34183455
}
34193456

34203457
enum TestMenu {

0 commit comments

Comments
 (0)