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
This commit is contained in:
Jefferey Neuffer
2024-07-12 04:30:57 +10:00
committed by GitHub
parent b13f50a918
commit 90025e2964
2 changed files with 42 additions and 0 deletions

View File

@@ -159,6 +159,7 @@ class DropdownMenu<T> 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<T> 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<T> extends State<DropdownMenu<T>> {
focusNode: widget.focusNode,
canRequestFocus: canRequestFocus(),
enableInteractiveSelection: canRequestFocus(),
keyboardType: widget.keyboardType,
textAlign: widget.textAlign,
textAlignVertical: TextAlignVertical.center,
style: effectiveTextStyle,

View File

@@ -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<TestMenu>(
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<TestMenu>(
dropdownMenuEntries: menuChildren,
),
),
),
),
);
final TextField textField = tester.widget(find.byType(TextField));
expect(textField.keyboardType, TextInputType.text);
});
}
enum TestMenu {