Do not copy the old selection when applying localization to dates in InputDatePickerFormField (#107268)

Fixes https://github.com/flutter/flutter/issues/107155
This commit is contained in:
Jason Simmons
2022-07-19 00:24:13 -07:00
committed by GitHub
parent 7e96d89aea
commit 21e9bc6bd3
2 changed files with 47 additions and 2 deletions

View File

@@ -179,7 +179,7 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
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<InputDatePickerFormField> {
_controller.value = textEditingValue;
} else {
_inputText = '';
_controller.value = _controller.value.copyWith(text: _inputText);
_controller.value = TextEditingValue(text: _inputText!);
}
}

View File

@@ -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<MaterialLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<MaterialLocalizations> load(Locale locale) {
return SynchronousFuture<MaterialLocalizations>(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<LocalizationsDelegate<dynamic>>? 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<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[
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,
)
);
});
});
}