From 90025e29645bf76d7760659b22d2f7d5874eb317 Mon Sep 17 00:00:00 2001 From: Jefferey Neuffer Date: Fri, 12 Jul 2024 04:30:57 +1000 Subject: [PATCH] expose keyboardType in DropdownMenu #150894 (#150896) This PR exposes the `keyboardType` parameter in DropdownMenu, allowing the type of keyboard to be used for the text input to be changed. fixes #150894 --- .../lib/src/material/dropdown_menu.dart | 7 ++++ .../test/material/dropdown_menu_test.dart | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 7e634cd2aa..2e8b442dc2 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -159,6 +159,7 @@ class DropdownMenu extends StatefulWidget { this.selectedTrailingIcon, this.enableFilter = false, this.enableSearch = true, + this.keyboardType, this.textStyle, this.textAlign = TextAlign.start, this.inputDecorationTheme, @@ -266,6 +267,11 @@ class DropdownMenu extends StatefulWidget { /// Defaults to true as the search function could be commonly used. final bool enableSearch; + /// The type of keyboard to use for editing the text. + /// + /// Defaults to [TextInputType.text]. + final TextInputType? keyboardType; + /// The text style for the [TextField] of the [DropdownMenu]; /// /// Defaults to the overall theme's [TextTheme.bodyLarge] @@ -804,6 +810,7 @@ class _DropdownMenuState extends State> { focusNode: widget.focusNode, canRequestFocus: canRequestFocus(), enableInteractiveSelection: canRequestFocus(), + keyboardType: widget.keyboardType, textAlign: widget.textAlign, textAlignVertical: TextAlignVertical.center, style: effectiveTextStyle, diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index e9f84e1b0c..ea70dc859e 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2397,6 +2397,41 @@ void main() { expect(fields[0].textAlign, TextAlign.start); expect(fields[1].textAlign, TextAlign.center); }); + + testWidgets('DropdownMenu correctly sets keyboardType on TextField', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SafeArea( + child: DropdownMenu( + dropdownMenuEntries: menuChildren, + keyboardType: TextInputType.number, + ), + ), + ), + ), + ); + + final TextField textField = tester.widget(find.byType(TextField)); + expect(textField.keyboardType, TextInputType.number); + }); + + testWidgets('DropdownMenu keyboardType defaults to TextInputType.text', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SafeArea( + child: DropdownMenu( + dropdownMenuEntries: menuChildren, + ), + ), + ), + ), + ); + + final TextField textField = tester.widget(find.byType(TextField)); + expect(textField.keyboardType, TextInputType.text); + }); } enum TestMenu {