From b3de00ad540aed73b5e07e1741df29ae38068944 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 7 Oct 2024 16:43:22 +0300 Subject: [PATCH] Fix `DropdownMenu` with `expandedInsets` always aligned on top (#156214) Fixes [DropdownMenu can not be center aligned when using expandedInsets ](https://github.com/flutter/flutter/issues/155581) ### Code sample
expand to view the code sample ```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> shortMenuItems = >[]; for (final ShortMenu value in ShortMenu.values) { final DropdownMenuEntry entry = DropdownMenuEntry(value: value, label: value.label); shortMenuItems.add(entry); } return MaterialApp( home: Scaffold( body: Row( children: [ Expanded( child: Center( child: DropdownMenu( expandedInsets: const EdgeInsets.all(16), initialSelection: ShortMenu.item0, dropdownMenuEntries: shortMenuItems, label: const Text('With expandedInsets'), ), ), ), Expanded( child: Center( child: DropdownMenu( 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; } ```
### Before (`DropdownMenu` without `expandedInsets` cannot be centered) Screenshot 2024-10-04 at 14 13 58 ### After (`DropdownMenu` with `expandedInsets` be centered) Screenshot 2024-10-04 at 14 13 49 --- .../lib/src/material/dropdown_menu.dart | 5 +-- .../test/material/dropdown_menu_test.dart | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 4b19b9b432..d118e620bd 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -972,10 +972,7 @@ class _DropdownMenuState extends State> { ), ), ), - child: Align( - alignment: AlignmentDirectional.topStart, - child: menuAnchor, - ), + child: menuAnchor, ); } diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 5ed70d1bd7..ab0992a833 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -3415,6 +3415,43 @@ void main() { tester.getRect(find.byType(TextField).first).bottom, ); }); + + testWidgets('DropdownMenu with expandedInsets can be aligned', (WidgetTester tester) async { + Widget buildMenuAnchor({ AlignmentGeometry alignment = Alignment.topCenter }) { + return MaterialApp( + home: Scaffold( + body: Row( + children: [ + Expanded( + child: Align( + alignment: alignment, + child: DropdownMenu( + expandedInsets: const EdgeInsets.all(16), + dropdownMenuEntries: menuChildren, + ), + ), + ), + ], + ), + ), + ); + } + + await tester.pumpWidget(buildMenuAnchor()); + + Offset textFieldPosition = tester.getTopLeft(find.byType(TextField)); + expect(textFieldPosition, equals(const Offset(16.0, 0.0))); + + await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.center)); + + textFieldPosition = tester.getTopLeft(find.byType(TextField)); + expect(textFieldPosition, equals(const Offset(16.0, 272.0))); + + await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.bottomCenter)); + + textFieldPosition = tester.getTopLeft(find.byType(TextField)); + expect(textFieldPosition, equals(const Offset(16.0, 544.0))); + }); } enum TestMenu {