Skip to content

Commit 21e9bc6

Browse files
Do not copy the old selection when applying localization to dates in InputDatePickerFormField (#107268)
Fixes flutter/flutter#107155
1 parent 7e96d89 commit 21e9bc6

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/flutter/lib/src/material/input_date_picker_form_field.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
179179
if (_selectedDate != null) {
180180
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
181181
_inputText = localizations.formatCompactDate(_selectedDate!);
182-
TextEditingValue textEditingValue = _controller.value.copyWith(text: _inputText);
182+
TextEditingValue textEditingValue = TextEditingValue(text: _inputText!);
183183
// Select the new text if we are auto focused and haven't selected the text before.
184184
if (widget.autofocus && !_autoSelected) {
185185
textEditingValue = textEditingValue.copyWith(selection: TextSelection(
@@ -191,7 +191,7 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
191191
_controller.value = textEditingValue;
192192
} else {
193193
_inputText = '';
194-
_controller.value = _controller.value.copyWith(text: _inputText);
194+
_controller.value = TextEditingValue(text: _inputText!);
195195
}
196196
}
197197

packages/flutter/test/material/input_date_picker_form_field_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/services.dart';
78
import 'package:flutter_test/flutter_test.dart';
89

910
import '../widgets/clipboard_utils.dart';
1011

12+
class TestMaterialLocalizations extends DefaultMaterialLocalizations {
13+
@override
14+
String formatCompactDate(DateTime date) {
15+
return '${date.month}/${date.day}/${date.year}';
16+
}
17+
}
18+
19+
class TestMaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
20+
@override
21+
bool isSupported(Locale locale) => true;
22+
23+
@override
24+
Future<MaterialLocalizations> load(Locale locale) {
25+
return SynchronousFuture<MaterialLocalizations>(TestMaterialLocalizations());
26+
}
27+
28+
@override
29+
bool shouldReload(TestMaterialLocalizationsDelegate old) => false;
30+
}
31+
1132
void main() {
1233
TestWidgetsFlutterBinding.ensureInitialized();
1334
final MockClipboard mockClipboard = MockClipboard();
@@ -27,9 +48,11 @@ void main() {
2748
bool autofocus = false,
2849
Key? formKey,
2950
ThemeData? theme,
51+
Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates,
3052
}) {
3153
return MaterialApp(
3254
theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()),
55+
localizationsDelegates: localizationsDelegates,
3356
home: Material(
3457
child: Form(
3558
key: formKey,
@@ -300,5 +323,27 @@ void main() {
300323
expect(containerColor, equals(Colors.transparent));
301324
});
302325

326+
testWidgets('Date text localization', (WidgetTester tester) async {
327+
final Iterable<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[
328+
TestMaterialLocalizationsDelegate(),
329+
DefaultWidgetsLocalizations.delegate,
330+
];
331+
await tester.pumpWidget(
332+
inputDatePickerField(
333+
localizationsDelegates: delegates,
334+
)
335+
);
336+
await tester.enterText(find.byType(TextField), '01/01/2022');
337+
await tester.pumpAndSettle();
338+
339+
// Verify that the widget can be updated to a new value after the
340+
// entered text was transformed by the localization formatter.
341+
await tester.pumpWidget(
342+
inputDatePickerField(
343+
initialDate: DateTime(2017),
344+
localizationsDelegates: delegates,
345+
)
346+
);
347+
});
303348
});
304349
}

0 commit comments

Comments
 (0)