Repository navigation
TextEditingValue selection bug #129272
Copy link
Copy link
Closed
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
Is there an existing issue for this?
- I have searched the existing issues
- I have read the guide to filing a bug
Steps to reproduce
- Create a TextFormField.
- Add TextController.
- Add a custom button with GestureDetector above the TextFormField via Stack.
onTap: () => _textController.text = '';- TextFormField is cleared.
- Try to input some text
Expected results
You can input the text.
Actual results
Focus is still on the TextFormField, but input no longer works, until I tap to the TextFormField once more.
It works though if I change to _textController.clear();
The reason behind is https://api.flutter.dev/flutter/services/TextEditingValue/TextEditingValue.html
Selection remains the same, while text now is empty string and length is 0.
Code sample
OutlinedLabeledTextField code fragment:
Stack(
children: [
TextFormField(
controller: widget.textController,
enabled: !widget.isLocked,
initialValue: widget.initialValue,
textCapitalization: widget.textCapitalization,
inputFormatters: widget.inputFormatters,
keyboardType: widget.keyboardType,
textInputAction: widget.textInputAction,
onChanged: widget.onChanged,
onFieldSubmitted: widget.onSubmitted,
maxLines: widget.maxLines,
),
),
SizedBox(
height: 44,
child: Align(
alignment: Alignment.centerRight,
child: Builder(
builder: (context) {
if (widget.hasEraseButton) {
return Padding(
padding: const EdgeInsets.only(right: 12),
child: GestureDetector(
onTap: widget.onErase,
child: Assets.icons.clearFill.svg(package: Strings.packageName),
),
);
}
return const SizedBox.shrink();
},
),
),
),
],
),Screenshots or Video
video_2023-06-21_16-13-17.mp4
Logs
?
Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.0.1 22A400 darwin-arm64, locale ru-RU)
• Flutter version 3.10.5 on channel stable at /opt/homebrew/Caskroom/flutter/3.3.10/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 796c8ef792 (8 days ago), 2023-06-13 15:51:02 -0700
• Engine revision 45f6e00911
• Dart version 3.0.5
• DevTools version 2.23.1
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/svmolchan1/Library/Android/sdk
• Platform android-33, build-tools 33.0.1
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14E300c
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] VS Code (version 1.79.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.66.0
[✓] Connected device (3 available)
• M2101K9AG (mobile) • cc706425 • android-arm64 • Android 13 (API 33)
• macOS (desktop) • macos • darwin-arm64 • macOS 13.0.1 22A400 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119
[✓] Network resources
• All expected network resources are available.
• No issues found!Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team

Activity
LongCatIsLooong commentedon Jun 21, 2023
This is working as expected (although not quite intuitive). The text field does not know where to insert the text you just entered if you set the selection to
(-1, -1). Consider setting the selection toconst TextSelection.collapsed(offset: 0)instead.subzero911 commentedon Jun 22, 2023
darshankawar commentedon Jun 22, 2023
@subzero911 Thanks for the feedback.
You may file a new proposal request highlighting the points you made, that'll help to properly track the issue.
If the solution helps to resolve your issue, you may close this one.
subzero911 commentedon Jun 22, 2023
I'm not quite happy with this solution, an existing behaviour is not intuitive as @LongCatIsLooong said above.
I proposed two considerations, how you can improve the developer's experience.
Why to close this one, if it's directly related to the problem?
maRci002 commentedon Jun 23, 2023
I believe it should be documented somewhere that one should use
_textController.clear()instead of_textController.text = ''. This is because in my case, while the software keyboard ( it is basicly a touch screen app ) functioned correctly, the hardware keyboard did not.LongCatIsLooong commentedon Jun 23, 2023
Using the
TextEditingController.textsetter should be discouraged in the documentation.justinmc commentedon Apr 3, 2024
Assigning @victorsanni to make a docs improvement here.
justinmc commentedon Apr 3, 2024
Actually, I'm unable to reproduce this bug. I believe the various platform embedders no longer allow -1 selection (did you make that change @LongCatIsLooong?), so they send it back as 0, and the bug no longer happens. Previously, I think this behavior was inconsistent among the different platforms.
I tried the code below on Android and iOS with both hardware and software keyboards, and on Linux, and Mac with a hardware keyboard, and I never saw the bug.
@victorsanni's PR may still go through to make the docs more clear, otherwise I think this issue should be closed, unless anyone else is still seeing this bug.
Code
Update documentation to discourage using the TextEditingController.te…
justinmc commentedon Apr 4, 2024
I talked to @LongCatIsLooong and he was not aware of a deliberate change to how any of these embedders handle -1 selection. However, I think we should still close this issue when #146151 is merged. If anyone can still reproduce this, please leave a comment explaning how and I'll be happy to reopen this and take a look.
github-actions commentedon Apr 19, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.Update documentation to discourage using the TextEditingController.te…