From eb941ce9d66aad5669c56e8bef8cba0681b88014 Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Tue, 11 Aug 2020 14:56:03 -0700 Subject: [PATCH] Removed the inputFormatters from the text input fields used by the Date Pickers (#63461) --- .../material/pickers/input_date_picker.dart | 49 ++----------------- .../pickers/input_date_range_picker.dart | 7 --- .../test/material/date_picker_test.dart | 3 +- 3 files changed, 7 insertions(+), 52 deletions(-) diff --git a/packages/flutter/lib/src/material/pickers/input_date_picker.dart b/packages/flutter/lib/src/material/pickers/input_date_picker.dart index dda9714251..2ef7544f57 100644 --- a/packages/flutter/lib/src/material/pickers/input_date_picker.dart +++ b/packages/flutter/lib/src/material/pickers/input_date_picker.dart @@ -16,12 +16,12 @@ import '../theme.dart'; import 'date_picker_common.dart'; 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 -/// and separators. When saved or submitted, the text will be parsed into a -/// [DateTime] according to the ambient locale. If the input text doesn't parse -/// into a date, the [errorFormatText] message will be displayed under the field. +/// When the field is saved or submitted, the text will be parsed into a +/// [DateTime] according to the ambient locale's compact date format. If the +/// input text doesn't parse into a date, the [errorFormatText] message will +/// be displayed under the field. /// /// [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 @@ -231,9 +231,6 @@ class _InputDatePickerFormFieldState extends State { labelText: widget.fieldLabelText ?? localizations.dateInputLabel, ), validator: _validateDate, - inputFormatters: [ - DateTextInputFormatter(localizations.dateSeparator), - ], keyboardType: TextInputType.datetime, onSaved: _handleSaved, onFieldSubmitted: _handleSubmitted, @@ -242,39 +239,3 @@ class _InputDatePickerFormFieldState extends State { ); } } - -/// 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), - ); - } -} diff --git a/packages/flutter/lib/src/material/pickers/input_date_range_picker.dart b/packages/flutter/lib/src/material/pickers/input_date_range_picker.dart index 60297333ab..fbe9b3bad6 100644 --- a/packages/flutter/lib/src/material/pickers/input_date_range_picker.dart +++ b/packages/flutter/lib/src/material/pickers/input_date_range_picker.dart @@ -14,7 +14,6 @@ import '../text_field.dart'; import '../theme.dart'; 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 /// end dates that represent a range of dates. @@ -124,7 +123,6 @@ class InputDateRangePickerState extends State { String _startErrorText; String _endErrorText; bool _autoSelected = false; - List _inputFormatters; @override void initState() { @@ -146,9 +144,6 @@ class InputDateRangePickerState extends State { void didChangeDependencies() { super.didChangeDependencies(); final MaterialLocalizations localizations = MaterialLocalizations.of(context); - _inputFormatters = [ - DateTextInputFormatter(localizations.dateSeparator), - ]; if (_startDate != null) { _startInputText = localizations.formatCompactDate(_startDate); final bool selectText = widget.autofocus && !_autoSelected; @@ -247,7 +242,6 @@ class InputDateRangePickerState extends State { labelText: widget.fieldStartLabelText ?? localizations.dateRangeStartLabel, errorText: _startErrorText, ), - inputFormatters: _inputFormatters, keyboardType: TextInputType.datetime, onChanged: _handleStartChanged, autofocus: widget.autofocus, @@ -264,7 +258,6 @@ class InputDateRangePickerState extends State { labelText: widget.fieldEndLabelText ?? localizations.dateRangeEndLabel, errorText: _endErrorText, ), - inputFormatters: _inputFormatters, keyboardType: TextInputType.datetime, onChanged: _handleEndChanged, ), diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index cd583e3285..2241bc8a9e 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -741,7 +741,8 @@ void main() { field.controller.clear(); 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); await tester.tap(find.text('OK'));