Unverified Commit e93a10d1 authored by Tirth's avatar Tirth Committed by GitHub

Pass-Through `inputFormatters` in `DropdownMenu` (#143250)

Pass-Through `inputFormatters` in `DropdownMenu`.

Fixes: #142374
parent d271791e
...@@ -164,6 +164,7 @@ class DropdownMenu<T> extends StatefulWidget { ...@@ -164,6 +164,7 @@ class DropdownMenu<T> extends StatefulWidget {
this.expandedInsets, this.expandedInsets,
this.searchCallback, this.searchCallback,
required this.dropdownMenuEntries, required this.dropdownMenuEntries,
this.inputFormatters,
}); });
/// Determine if the [DropdownMenu] is enabled. /// Determine if the [DropdownMenu] is enabled.
...@@ -389,6 +390,20 @@ class DropdownMenu<T> extends StatefulWidget { ...@@ -389,6 +390,20 @@ class DropdownMenu<T> extends StatefulWidget {
/// which contains the contents of the text input field. /// which contains the contents of the text input field.
final SearchCallback<T>? searchCallback; final SearchCallback<T>? searchCallback;
/// Optional input validation and formatting overrides.
///
/// Formatters are run in the provided order when the user changes the text
/// this widget contains. When this parameter changes, the new formatters will
/// not be applied until the next time the user inserts or deletes text.
/// Formatters don't run when the text is changed
/// programmatically via [controller].
///
/// See also:
///
/// * [TextEditingController], which implements the [Listenable] interface
/// and notifies its listeners on [TextEditingValue] changes.
final List<TextInputFormatter>? inputFormatters;
@override @override
State<DropdownMenu<T>> createState() => _DropdownMenuState<T>(); State<DropdownMenu<T>> createState() => _DropdownMenuState<T>();
} }
...@@ -755,6 +770,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> { ...@@ -755,6 +770,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
_enableFilter = widget.enableFilter; _enableFilter = widget.enableFilter;
}); });
}, },
inputFormatters: widget.inputFormatters,
decoration: InputDecoration( decoration: InputDecoration(
enabled: widget.enabled, enabled: widget.enabled,
label: widget.label, label: widget.label,
......
...@@ -1972,6 +1972,54 @@ void main() { ...@@ -1972,6 +1972,54 @@ void main() {
// Test input border when focused. // Test input border when focused.
expect(box, paints..rrect(color: theme.colorScheme.primary)); expect(box, paints..rrect(color: theme.colorScheme.primary));
}); });
testWidgets('DropdownMenu honors inputFormatters', (WidgetTester tester) async {
int called = 0;
final TextInputFormatter formatter = TextInputFormatter.withFunction(
(TextEditingValue oldValue, TextEditingValue newValue) {
called += 1;
return newValue;
},
);
final TextEditingController controller = TextEditingController();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownMenu<String>(
controller: controller,
dropdownMenuEntries: const <DropdownMenuEntry<String>>[
DropdownMenuEntry<String>(
value: 'Blue',
label: 'Blue',
),
DropdownMenuEntry<String>(
value: 'Green',
label: 'Green',
),
],
inputFormatters: <TextInputFormatter>[
formatter,
FilteringTextInputFormatter.deny(RegExp('[0-9]'))
],
),
),
),
);
final EditableTextState state = tester.firstState(find.byType(EditableText));
state.updateEditingValue(const TextEditingValue(text: 'Blue'));
expect(called, 1);
expect(controller.text, 'Blue');
state.updateEditingValue(const TextEditingValue(text: 'Green'));
expect(called, 2);
expect(controller.text, 'Green');
state.updateEditingValue(const TextEditingValue(text: 'Green2'));
expect(called, 3);
expect(controller.text, 'Green');
});
} }
enum TestMenu { enum TestMenu {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment