From 11cbe41555d5ee8f252b989e52648f911dc6236c Mon Sep 17 00:00:00 2001 From: John Angelo Cordero Date: Fri, 31 May 2024 00:57:00 +0800 Subject: [PATCH] allow changing textAlign of TextField in DropdownMenu (#148074) PR for #147991 --- .../lib/src/material/dropdown_menu.dart | 7 ++++++ .../test/material/dropdown_menu_test.dart | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 76799e474f..d977265f4e 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -154,6 +154,7 @@ class DropdownMenu extends StatefulWidget { this.enableFilter = false, this.enableSearch = true, this.textStyle, + this.textAlign = TextAlign.start, this.inputDecorationTheme, this.menuStyle, this.controller, @@ -264,6 +265,11 @@ class DropdownMenu extends StatefulWidget { /// if the dropdown menu theme's value is null. final TextStyle? textStyle; + /// The text align for the [TextField] of the [DropdownMenu]. + /// + /// Defaults to [TextAlign.start]. + final TextAlign textAlign; + /// Defines the default appearance of [InputDecoration] to show around the text field. /// /// By default, shows a outlined text field. @@ -754,6 +760,7 @@ class _DropdownMenuState extends State> { focusNode: widget.focusNode, canRequestFocus: canRequestFocus(), enableInteractiveSelection: canRequestFocus(), + textAlign: widget.textAlign, textAlignVertical: TextAlignVertical.center, style: effectiveTextStyle, controller: _localTextEditingController, diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index c2cb13c50f..fcff42b99f 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2293,6 +2293,29 @@ void main() { // One is layout for the _DropdownMenuBody, the other one is the real button item in the menu. expect(find.widgetWithText(MenuItemButton, labelText), findsNWidgets(2)); }); + + testWidgets('DropdownMenu allows customizing text field text align', (WidgetTester tester) async { + await tester.pumpWidget(const MaterialApp( + home: Scaffold( + body: Column( + children: >[ + DropdownMenu( + dropdownMenuEntries: >[], + ), + DropdownMenu( + textAlign: TextAlign.center, + dropdownMenuEntries: >[], + ), + ], + ), + ), + )); + + final List fields = tester.widgetList(find.byType(TextField)).toList(); + + expect(fields[0].textAlign, TextAlign.start); + expect(fields[1].textAlign, TextAlign.center); + }); } enum TestMenu {