Unverified Commit eb941ce9 authored by Darren Austin's avatar Darren Austin Committed by GitHub

Removed the inputFormatters from the text input fields used by the Date Pickers (#63461)

parent df8e537f
...@@ -16,12 +16,12 @@ import '../theme.dart'; ...@@ -16,12 +16,12 @@ import '../theme.dart';
import 'date_picker_common.dart'; import 'date_picker_common.dart';
import 'date_utils.dart' as utils; import 'date_utils.dart' as utils;
/// A [TextFormField] configured to accept and validate a date entered by the user. /// A [TextFormField] configured to accept and validate a date entered by a user.
/// ///
/// The text entered into this field will be constrained to only allow digits /// When the field is saved or submitted, the text will be parsed into a
/// and separators. When saved or submitted, the text will be parsed into a /// [DateTime] according to the ambient locale's compact date format. If the
/// [DateTime] according to the ambient locale. If the input text doesn't parse /// input text doesn't parse into a date, the [errorFormatText] message will
/// into a date, the [errorFormatText] message will be displayed under the field. /// be displayed under the field.
/// ///
/// [firstDate], [lastDate], and [selectableDayPredicate] provide constraints on /// [firstDate], [lastDate], and [selectableDayPredicate] provide constraints on
/// what days are valid. If the input date isn't in the date range or doesn't pass /// what days are valid. If the input date isn't in the date range or doesn't pass
...@@ -231,9 +231,6 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> { ...@@ -231,9 +231,6 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
labelText: widget.fieldLabelText ?? localizations.dateInputLabel, labelText: widget.fieldLabelText ?? localizations.dateInputLabel,
), ),
validator: _validateDate, validator: _validateDate,
inputFormatters: <TextInputFormatter>[
DateTextInputFormatter(localizations.dateSeparator),
],
keyboardType: TextInputType.datetime, keyboardType: TextInputType.datetime,
onSaved: _handleSaved, onSaved: _handleSaved,
onFieldSubmitted: _handleSubmitted, onFieldSubmitted: _handleSubmitted,
...@@ -242,39 +239,3 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> { ...@@ -242,39 +239,3 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
); );
} }
} }
/// A `TextInputFormatter` set up to format dates.
//
// This is not publicly exported (see pickers.dart), as it is
// just meant for internal use by `InputDatePickerFormField` and
// `InputDateRangePicker`.
class DateTextInputFormatter extends TextInputFormatter {
/// Creates a date formatter with the given separator.
DateTextInputFormatter(
this.separator
) : _filterFormatter = FilteringTextInputFormatter.allow(RegExp('[\\d$_commonSeparators\\$separator]+'));
/// List of common separators that are used in dates. This is used to make
/// sure that if given platform's [TextInputType.datetime] keyboard doesn't
/// provide the given locale's separator character, they can still enter the
/// separator using one of these characters (slash, period, comma, dash, or
/// space).
static const String _commonSeparators = r'\/\.,-\s';
/// The date separator for the current locale.
final String separator;
// Formatter that will filter out all characters except digits and date
// separators.
final TextInputFormatter _filterFormatter;
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
final TextEditingValue filteredValue = _filterFormatter.formatEditUpdate(oldValue, newValue);
return filteredValue.copyWith(
// Replace any non-digits with the given separator
text: filteredValue.text.replaceAll(RegExp(r'[\D]'), separator),
);
}
}
...@@ -14,7 +14,6 @@ import '../text_field.dart'; ...@@ -14,7 +14,6 @@ import '../text_field.dart';
import '../theme.dart'; import '../theme.dart';
import 'date_utils.dart' as utils; import 'date_utils.dart' as utils;
import 'input_date_picker.dart' show DateTextInputFormatter;
/// Provides a pair of text fields that allow the user to enter the start and /// Provides a pair of text fields that allow the user to enter the start and
/// end dates that represent a range of dates. /// end dates that represent a range of dates.
...@@ -124,7 +123,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -124,7 +123,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
String _startErrorText; String _startErrorText;
String _endErrorText; String _endErrorText;
bool _autoSelected = false; bool _autoSelected = false;
List<TextInputFormatter> _inputFormatters;
@override @override
void initState() { void initState() {
...@@ -146,9 +144,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -146,9 +144,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
final MaterialLocalizations localizations = MaterialLocalizations.of(context); final MaterialLocalizations localizations = MaterialLocalizations.of(context);
_inputFormatters = <TextInputFormatter>[
DateTextInputFormatter(localizations.dateSeparator),
];
if (_startDate != null) { if (_startDate != null) {
_startInputText = localizations.formatCompactDate(_startDate); _startInputText = localizations.formatCompactDate(_startDate);
final bool selectText = widget.autofocus && !_autoSelected; final bool selectText = widget.autofocus && !_autoSelected;
...@@ -247,7 +242,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -247,7 +242,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
labelText: widget.fieldStartLabelText ?? localizations.dateRangeStartLabel, labelText: widget.fieldStartLabelText ?? localizations.dateRangeStartLabel,
errorText: _startErrorText, errorText: _startErrorText,
), ),
inputFormatters: _inputFormatters,
keyboardType: TextInputType.datetime, keyboardType: TextInputType.datetime,
onChanged: _handleStartChanged, onChanged: _handleStartChanged,
autofocus: widget.autofocus, autofocus: widget.autofocus,
...@@ -264,7 +258,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -264,7 +258,6 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
labelText: widget.fieldEndLabelText ?? localizations.dateRangeEndLabel, labelText: widget.fieldEndLabelText ?? localizations.dateRangeEndLabel,
errorText: _endErrorText, errorText: _endErrorText,
), ),
inputFormatters: _inputFormatters,
keyboardType: TextInputType.datetime, keyboardType: TextInputType.datetime,
onChanged: _handleEndChanged, onChanged: _handleEndChanged,
), ),
......
...@@ -741,7 +741,8 @@ void main() { ...@@ -741,7 +741,8 @@ void main() {
field.controller.clear(); field.controller.clear();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), '20202014'); await tester.enterText(find.byType(TextField), '20 days, 3 months, 2003');
expect(find.text('20 days, 3 months, 2003'), findsOneWidget);
expect(find.text(errorFormatText), findsNothing); expect(find.text(errorFormatText), findsNothing);
await tester.tap(find.text('OK')); await tester.tap(find.text('OK'));
......
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