Skip to content

Commit 11cbe41

Browse files
allow changing textAlign of TextField in DropdownMenu (#148074)
PR for #147991
1 parent 02739d7 commit 11cbe41

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class DropdownMenu<T> extends StatefulWidget {
154154
this.enableFilter = false,
155155
this.enableSearch = true,
156156
this.textStyle,
157+
this.textAlign = TextAlign.start,
157158
this.inputDecorationTheme,
158159
this.menuStyle,
159160
this.controller,
@@ -264,6 +265,11 @@ class DropdownMenu<T> extends StatefulWidget {
264265
/// if the dropdown menu theme's value is null.
265266
final TextStyle? textStyle;
266267

268+
/// The text align for the [TextField] of the [DropdownMenu].
269+
///
270+
/// Defaults to [TextAlign.start].
271+
final TextAlign textAlign;
272+
267273
/// Defines the default appearance of [InputDecoration] to show around the text field.
268274
///
269275
/// By default, shows a outlined text field.
@@ -754,6 +760,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
754760
focusNode: widget.focusNode,
755761
canRequestFocus: canRequestFocus(),
756762
enableInteractiveSelection: canRequestFocus(),
763+
textAlign: widget.textAlign,
757764
textAlignVertical: TextAlignVertical.center,
758765
style: effectiveTextStyle,
759766
controller: _localTextEditingController,

packages/flutter/test/material/dropdown_menu_test.dart

+23
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,29 @@ void main() {
22932293
// One is layout for the _DropdownMenuBody, the other one is the real button item in the menu.
22942294
expect(find.widgetWithText(MenuItemButton, labelText), findsNWidgets(2));
22952295
});
2296+
2297+
testWidgets('DropdownMenu allows customizing text field text align', (WidgetTester tester) async {
2298+
await tester.pumpWidget(const MaterialApp(
2299+
home: Scaffold(
2300+
body: Column(
2301+
children: <DropdownMenu<int>>[
2302+
DropdownMenu<int>(
2303+
dropdownMenuEntries: <DropdownMenuEntry<int>>[],
2304+
),
2305+
DropdownMenu<int>(
2306+
textAlign: TextAlign.center,
2307+
dropdownMenuEntries: <DropdownMenuEntry<int>>[],
2308+
),
2309+
],
2310+
),
2311+
),
2312+
));
2313+
2314+
final List<TextField> fields = tester.widgetList<TextField>(find.byType(TextField)).toList();
2315+
2316+
expect(fields[0].textAlign, TextAlign.start);
2317+
expect(fields[1].textAlign, TextAlign.center);
2318+
});
22962319
}
22972320

22982321
enum TestMenu {

0 commit comments

Comments
 (0)