diff --git a/packages/flutter/lib/src/cupertino/text_form_field_row.dart b/packages/flutter/lib/src/cupertino/text_form_field_row.dart index b61668c795..7fa5f854c1 100644 --- a/packages/flutter/lib/src/cupertino/text_form_field_row.dart +++ b/packages/flutter/lib/src/cupertino/text_form_field_row.dart @@ -360,7 +360,7 @@ class _CupertinoTextFormFieldRowState extends FormFieldState { super.didChange(value); if (value != null && _effectiveController.text != value) { - _effectiveController.text = value; + _effectiveController.value = TextEditingValue(text: value); } } @@ -368,7 +368,7 @@ class _CupertinoTextFormFieldRowState extends FormFieldState { void reset() { // Set the controller value before calling super.reset() to let // _handleControllerChanged suppress the change. - _effectiveController.text = widget.initialValue!; + _effectiveController.value = TextEditingValue(text: widget.initialValue ?? ''); super.reset(); _cupertinoTextFormFieldRow.onChanged?.call(_effectiveController.text); } diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index 0506a4233c..065dda5d68 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -292,12 +292,10 @@ abstract class SearchDelegate { /// /// Setting the query string programmatically moves the cursor to the end of the text field. set query(String value) { - _queryTextController.text = value; - if (_queryTextController.text.isNotEmpty) { - _queryTextController.selection = TextSelection.fromPosition( - TextPosition(offset: _queryTextController.text.length), - ); - } + _queryTextController.value = TextEditingValue( + text: value, + selection: TextSelection.collapsed(offset: value.length), + ); } /// Transition from the suggestions returned by [buildSuggestions] to the diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart index 6c9081ce25..f5f8a43e28 100644 --- a/packages/flutter/lib/src/material/search_anchor.dart +++ b/packages/flutter/lib/src/material/search_anchor.dart @@ -491,7 +491,7 @@ class _SearchAnchorState extends State { void _closeView(String? selectedText) { if (selectedText != null) { - _searchController.text = selectedText; + _searchController.value = TextEditingValue(text: selectedText); } Navigator.of(context).pop(); } diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index aee8fe6c6c..81cddddc47 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -403,7 +403,7 @@ class _TextFormFieldState extends FormFieldState { super.didChange(value); if (_effectiveController.text != value) { - _effectiveController.text = value ?? ''; + _effectiveController.value = TextEditingValue(text: value ?? ''); } } @@ -411,7 +411,7 @@ class _TextFormFieldState extends FormFieldState { void reset() { // Set the controller value before calling super.reset() to let // _handleControllerChanged suppress the change. - _effectiveController.text = widget.initialValue ?? ''; + _effectiveController.value = TextEditingValue(text: widget.initialValue ?? ''); super.reset(); _textFormField.onChanged?.call(_effectiveController.text); } diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index c73131a7f1..78fd6f702f 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -2098,7 +2098,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora // version yet. if (!controllerHasBeenSet.value) { controllerHasBeenSet.value = true; - controller.value.text = _formattedValue; + controller.value.value = TextEditingValue(text: _formattedValue); } } diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 6a1f500edc..aa99784d69 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -258,17 +258,19 @@ class TextEditingController extends ValueNotifier { /// The current string the user is editing. String get text => value.text; - /// Setting this will notify all the listeners of this [TextEditingController] + /// Updates the current [text] to the given `newText`, and removes existing + /// selection and composing range held by the controller. + /// + /// This setter is typically only used in tests, as it resets the cursor + /// position and the composing state. For production code, **consider using the + /// [value] setter to update the [text] value instead**, and specify a + /// reasonable selection range within the new [text]. + /// + /// Setting this notifies all the listeners of this [TextEditingController] /// that they need to update (it calls [notifyListeners]). For this reason, /// this value should only be set between frames, e.g. in response to user - /// actions, not during the build, layout, or paint phases. - /// - /// This property can be set from a listener added to this - /// [TextEditingController]; **however, one should not also set [selection] - /// in a separate statement. To change both the [text] and the [selection] - /// change the controller's [value].** Setting this here will clear - /// the current selection and composing range, so avoid using it directly - /// unless that is the desired behavior. + /// actions, not during the build, layout, or paint phases. This property can + /// be set from a listener added to this [TextEditingController]. set text(String newText) { value = value.copyWith( text: newText, diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 56b3d74dfb..aad4713012 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -48,6 +48,9 @@ void main() { textField.controller!.selection, TextSelection(baseOffset: delegate.query.length, extentOffset: delegate.query.length), ); + + delegate.query = ''; + expect(textField.controller!.selection, const TextSelection.collapsed(offset: 0)); }); testWidgets('Can open and close search', (WidgetTester tester) async {