Skip to content

TextEditingValue selection bug #129272

Closed

Description

@subzero911

Is there an existing issue for this?

Steps to reproduce

  1. Create a TextFormField.
  2. Add TextController.
  3. Add a custom button with GestureDetector above the TextFormField via Stack.
  4. onTap: () => _textController.text = '';
  5. TextFormField is cleared.
  6. 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

image

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!

Activity

  1. LongCatIsLooong commented on Jun 21, 2023

    @LongCatIsLooong
    Contributor

    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 to const TextSelection.collapsed(offset: 0) instead.

  2. added
    in triagePresently being triaged by the triage team
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 22, 2023
  3. subzero911 commented on Jun 22, 2023

    @subzero911
    Author
    1. You may log the error to the console "attempting to insert text with the selection set to (-1, -1)", so it's easier to spot the problem.
    2. You may use setter on the "text" property to reset the selection, if the user inputs an empty string.
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 22, 2023
  5. darshankawar commented on Jun 22, 2023

    @darshankawar
    Member

    @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.

  6. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 22, 2023
  7. subzero911 commented on Jun 22, 2023

    @subzero911
    Author

    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?

  8. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 22, 2023
  9. maRci002 commented on Jun 23, 2023

    @maRci002
    Contributor

    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.

  10. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jun 23, 2023
  11. LongCatIsLooong commented on Jun 23, 2023

    @LongCatIsLooong
    Contributor

    Using the TextEditingController.text setter should be discouraged in the documentation.

  12. added
    P2Important issues not at the top of the work list
    on Jun 23, 2023
  13. justinmc commented on Apr 3, 2024

    @justinmc
    Contributor

    Assigning @victorsanni to make a docs improvement here.

  14. justinmc commented on Apr 3, 2024

    @justinmc
    Contributor

    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
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      final TextEditingController controller = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                TextField(
                  controller: controller,
                  decoration: InputDecoration(
                    suffixIcon: IconButton(
                      onPressed: () {
                        controller.text = '';
                      },
                      icon: const Icon(Icons.phonelink_erase),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
  15. added a commit that references this issue on Apr 4, 2024
    226546b
  16. justinmc commented on Apr 4, 2024

    @justinmc
    Contributor

    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.

  17. added
    r: fixedIssue is closed as already fixed in a newer version
    on Apr 5, 2024
  18. github-actions commented on Apr 19, 2024

    @github-actions

    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 -v and a minimal reproduction of the issue.

  19. locked as resolved and limited conversation to collaborators on Apr 19, 2024
  20. added a commit that references this issue on Apr 22, 2024
    5891270
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions