From 6fdcb0a5115c76b327f0791130e3264770491e3e Mon Sep 17 00:00:00 2001 From: Byeongjin Kang Date: Thu, 25 Jul 2024 06:17:06 +0900 Subject: [PATCH] feat: Add alignmentOffset to DropdownMenu (#151731) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: - Introduced `alignmentOffset` property to `DropdownMenu` to allow adjustment of menu alignment. - Updated the `MenuAnchor` widget to utilize the new `alignmentOffset` property. - Default value for `alignmentOffset` is `Offset.zero`, meaning no additional spacing by default. Motivation: - This PR closes #151524 - @nate-thegrate, please let me know if I need to make changes in order for this PR to be merged The motivation behind this change is to provide developers with more control over the spacing between the `MenuAnchor` and `dropdownMenuEntries` in the `DropdownMenu` widget. This enhancement allows for better alignment and customization of the dropdown menu appearance. | before | after | | :---: | :---: | | Screenshot 2024-07-14 at 8 03 14 PM | Screenshot 2024-07-14 at 8 03 27 PM | Initially, it was suggested to use a `SizedBox` to introduce spacing. However, upon further examination of the source code, it was discovered that the `DropdownMenuEntries` are rendered on the screen via an `OverlayPortal`. This necessitated leveraging the existing `alignmentOffset` property within the `MenuAnchor` for a more seamless and effective alignment adjustment. --- .../lib/src/material/dropdown_menu.dart | 5 +++++ .../flutter/lib/src/material/menu_anchor.dart | 2 ++ .../test/material/dropdown_menu_test.dart | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 6f53b9b4b1..cf1ae70510 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -175,6 +175,7 @@ class DropdownMenu extends StatefulWidget { this.expandedInsets, this.filterCallback, this.searchCallback, + this.alignmentOffset, required this.dropdownMenuEntries, this.inputFormatters, }) : assert(filterCallback == null || enableFilter); @@ -475,6 +476,9 @@ class DropdownMenu extends StatefulWidget { /// and notifies its listeners on [TextEditingValue] changes. final List? inputFormatters; + /// {@macro flutter.material.MenuAnchor.alignmentOffset} + final Offset? alignmentOffset; + @override State> createState() => _DropdownMenuState(); } @@ -784,6 +788,7 @@ class _DropdownMenuState extends State> { Widget menuAnchor = MenuAnchor( style: effectiveMenuStyle, + alignmentOffset: widget.alignmentOffset, controller: _controller, menuChildren: menu, crossAxisUnconstrained: false, diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index 28783d1c94..83076af810 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -181,6 +181,7 @@ class MenuAnchor extends StatefulWidget { /// Defaults to the ambient [MenuThemeData.style]. final MenuStyle? style; + /// {@template flutter.material.MenuAnchor.alignmentOffset} /// The offset of the menu relative to the alignment origin determined by /// [MenuStyle.alignment] on the [style] attribute and the ambient /// [Directionality]. @@ -201,6 +202,7 @@ class MenuAnchor extends StatefulWidget { /// [alignmentOffset] move the menu position to the left. /// /// Defaults to [Offset.zero]. + /// {@endtemplate} final Offset? alignmentOffset; /// {@macro flutter.material.Material.clipBehavior} diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index ea70dc859e..913478c407 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2432,6 +2432,28 @@ void main() { final TextField textField = tester.widget(find.byType(TextField)); expect(textField.keyboardType, TextInputType.text); }); + + testWidgets('DropdownMenu passes an alignmentOffset to MenuAnchor', (WidgetTester tester) async { + const Offset alignmentOffset = Offset(0, 16); + + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: DropdownMenu( + alignmentOffset: alignmentOffset, + dropdownMenuEntries: >[ + DropdownMenuEntry(value: '1', label: 'One'), + DropdownMenuEntry(value: '2', label: 'Two'), + ], + ), + ), + ), + ); + + final MenuAnchor menuAnchor = tester.widget(find.byType(MenuAnchor)); + + expect(menuAnchor.alignmentOffset, alignmentOffset); + }); } enum TestMenu {