diff --git a/packages/flutter/lib/src/material/input_date_picker_form_field.dart b/packages/flutter/lib/src/material/input_date_picker_form_field.dart index 3fcec07161..ca334e12d2 100644 --- a/packages/flutter/lib/src/material/input_date_picker_form_field.dart +++ b/packages/flutter/lib/src/material/input_date_picker_form_field.dart @@ -179,7 +179,7 @@ class _InputDatePickerFormFieldState extends State { if (_selectedDate != null) { final MaterialLocalizations localizations = MaterialLocalizations.of(context); _inputText = localizations.formatCompactDate(_selectedDate!); - TextEditingValue textEditingValue = _controller.value.copyWith(text: _inputText); + TextEditingValue textEditingValue = TextEditingValue(text: _inputText!); // Select the new text if we are auto focused and haven't selected the text before. if (widget.autofocus && !_autoSelected) { textEditingValue = textEditingValue.copyWith(selection: TextSelection( @@ -191,7 +191,7 @@ class _InputDatePickerFormFieldState extends State { _controller.value = textEditingValue; } else { _inputText = ''; - _controller.value = _controller.value.copyWith(text: _inputText); + _controller.value = TextEditingValue(text: _inputText!); } } diff --git a/packages/flutter/test/material/input_date_picker_form_field_test.dart b/packages/flutter/test/material/input_date_picker_form_field_test.dart index d1432d4eb6..43fe9c2d37 100644 --- a/packages/flutter/test/material/input_date_picker_form_field_test.dart +++ b/packages/flutter/test/material/input_date_picker_form_field_test.dart @@ -2,12 +2,33 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../widgets/clipboard_utils.dart'; +class TestMaterialLocalizations extends DefaultMaterialLocalizations { + @override + String formatCompactDate(DateTime date) { + return '${date.month}/${date.day}/${date.year}'; + } +} + +class TestMaterialLocalizationsDelegate extends LocalizationsDelegate { + @override + bool isSupported(Locale locale) => true; + + @override + Future load(Locale locale) { + return SynchronousFuture(TestMaterialLocalizations()); + } + + @override + bool shouldReload(TestMaterialLocalizationsDelegate old) => false; +} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); final MockClipboard mockClipboard = MockClipboard(); @@ -27,9 +48,11 @@ void main() { bool autofocus = false, Key? formKey, ThemeData? theme, + Iterable>? localizationsDelegates, }) { return MaterialApp( theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()), + localizationsDelegates: localizationsDelegates, home: Material( child: Form( key: formKey, @@ -300,5 +323,27 @@ void main() { expect(containerColor, equals(Colors.transparent)); }); + testWidgets('Date text localization', (WidgetTester tester) async { + final Iterable> delegates = >[ + TestMaterialLocalizationsDelegate(), + DefaultWidgetsLocalizations.delegate, + ]; + await tester.pumpWidget( + inputDatePickerField( + localizationsDelegates: delegates, + ) + ); + await tester.enterText(find.byType(TextField), '01/01/2022'); + await tester.pumpAndSettle(); + + // Verify that the widget can be updated to a new value after the + // entered text was transformed by the localization formatter. + await tester.pumpWidget( + inputDatePickerField( + initialDate: DateTime(2017), + localizationsDelegates: delegates, + ) + ); + }); }); }