From 9e211cabbd72de59d79decacfe0ad6f707c61366 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 10 Jan 2025 12:59:21 -0800 Subject: [PATCH] Update `TextEditingController.text` documentation to recommend against using it in production code (#157769) The `TextEditingController.text` setter has been a major source of confusion, as using it would usually result in loss of cursor position if the text field is active. Arguably it also should not be part of the controller's interface since it is a convenience method. There are many issues, for example: https://github.com/flutter/flutter/issues/65547 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../src/cupertino/text_form_field_row.dart | 4 ++-- packages/flutter/lib/src/material/search.dart | 10 ++++------ .../lib/src/material/search_anchor.dart | 2 +- .../lib/src/material/text_form_field.dart | 4 ++-- .../flutter/lib/src/material/time_picker.dart | 2 +- .../lib/src/widgets/editable_text.dart | 20 ++++++++++--------- .../flutter/test/material/search_test.dart | 3 +++ 7 files changed, 24 insertions(+), 21 deletions(-) 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 {