Pass-Through inputFormatters in DropdownMenu (#143250)

Pass-Through `inputFormatters` in `DropdownMenu`.

Fixes: #142374
This commit is contained in:
Tirth 2024-02-13 09:27:15 +05:30 committed by GitHub
parent d271791e8c
commit e93a10d1fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 0 deletions

View file

@ -164,6 +164,7 @@ class DropdownMenu<T> extends StatefulWidget {
this.expandedInsets,
this.searchCallback,
required this.dropdownMenuEntries,
this.inputFormatters,
});
/// Determine if the [DropdownMenu] is enabled.
@ -389,6 +390,20 @@ class DropdownMenu<T> extends StatefulWidget {
/// which contains the contents of the text input field.
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
State<DropdownMenu<T>> createState() => _DropdownMenuState<T>();
}
@ -755,6 +770,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
_enableFilter = widget.enableFilter;
});
},
inputFormatters: widget.inputFormatters,
decoration: InputDecoration(
enabled: widget.enabled,
label: widget.label,

View file

@ -1972,6 +1972,54 @@ void main() {
// Test input border when focused.
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 {