Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Programmatically select text in unfocused textfield not working #96339

@imaachman

Description

@imaachman

Steps to Reproduce

  1. Execute flutter run on the code sample or run it in DartPad.
  2. Tap floating action button to select the entire text in text field, programmatically.
  3. Tap the button on right of text field to focus on text field.

Expected results:
Selection UI should appear even when text field is not focused. This restriction wasn't there in earlier versions of Flutter and has been added recently.

Actual results:
Selection UI appears only when text field has focus.

Code sample
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 const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final TextEditingController controller;
  late final FocusNode focusNode;

  @override
  void initState() {
    super.initState();

    focusNode = FocusNode(debugLabel: 'TextField FocusNode');
    controller = TextEditingController();
    controller.text = 'Select this text';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            width: 200,
            child: TextField(
              focusNode: focusNode,
              controller: controller,
            ),
          ),
          const SizedBox(width: 16),
          IconButton(
            tooltip: 'Focus text field',
            onPressed: () => focusNode.requestFocus(),
            icon: const Icon(Icons.center_focus_strong),
          )
        ],
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.selection = controller.selection
              .copyWith(baseOffset: 0, extentOffset: controller.text.length);
        },
        tooltip: 'Select text',
        child: const Icon(Icons.select_all_rounded),
      ),
    );
  }

  @override
  void dispose() {
    focusNode.dispose();
    controller.dispose();
    super.dispose();
  }
}
Logs

flutter run --verbose

[  +66 ms] executing: [C:\src\flutter\flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +474 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +2 ms] 77d935af4db863f6abd0b9c31c7e6df2a13de57b
[   +1 ms] executing: [C:\src\flutter\flutter/] git tag --points-at 77d935af4db863f6abd0b9c31c7e6df2a13de57b
[  +59 ms] Exit code 0 from: git tag --points-at 77d935af4db863f6abd0b9c31c7e6df2a13de57b
[        ] 2.8.1
[   +8 ms] executing: [C:\src\flutter\flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[  +33 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [C:\src\flutter\flutter/] git ls-remote --get-url origin
[  +30 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[ +102 ms] executing: [C:\src\flutter\flutter/] git rev-parse --abbrev-ref HEAD
[  +33 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[  +86 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +4 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'WindowsUwpEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +110 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +3 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'WindowsUwpEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +85 ms] Skipping pub get: version match.
[ +118 ms] Generating C:\Users\imaac\Desktop\Codelessly\Flutter Issues\bug\android\app\src\main\java\io\flutter\plugins\GeneratedPluginRegistrant.java
[ +131 ms] Launching lib\main.dart on Chrome in debug mode...
[ +111 ms] Updating assets
[  +79 ms] Waiting for connection from debug service on Chrome...
[ +113 ms] <- reset
[  +12 ms] C:\src\flutter\flutter\bin\cache\dart-sdk\bin\dart.exe --disable-dart-dev C:\src\flutter\flutter\bin\cache\artifacts\engine\windows-x64\frontend_server.dart.snapshot --sdk-root
C:\src\flutter\flutter\bin\cache\flutter_web_sdk/ --incremental --target=dartdevc --debugger-module-names --experimental-emit-debug-metadata -DFLUTTER_WEB_AUTO_DETECT=true --output-dill
C:\Users\imaac\AppData\Local\Temp\flutter_tools.46ac3dcf\flutter_tool.6a3bcce2\app.dill --libraries-spec file:///C:/src/flutter/flutter/bin/cache/flutter_web_sdk/libraries.json --packages
C:\Users\imaac\Desktop\Codelessly\Flutter Issues\bug\.dart_tool\package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts --track-widget-creation --filesystem-root
C:\Users\imaac\AppData\Local\Temp\flutter_tools.46ac3dcf\flutter_tools.cd6b3fba --filesystem-scheme org-dartlang-app --initialize-from-dill build\b1b715402d823b7fd5c2b68d2edcb2ce.cache.dill.track.dill --platform
file:///C:/src/flutter/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk_sound.dill --sound-null-safety
[  +15 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+17806 ms] Waiting for connection from debug service on Chrome... (completed in 17.9s)
[   +1 ms] Synced 28.4MB.
[        ] <- accept
[        ] Caching compiled dill
[ +508 ms] [CHROME]:
[   +1 ms] [CHROME]:DevTools listening on ws://127.0.0.1:60713/devtools/browser/164fcb5b-0ebb-4874-bea2-dbbac73b49a5
[ +616 ms] DwdsInjector: Received request for entrypoint at http://localhost:60691/main_module.bootstrap.js
[   +5 ms] MetadataProvider: Loading debug metadata...
[  +15 ms] MetadataProvider: Loaded debug metadata (sound null safety)
[   +8 ms] DwdsInjector: Injected debugging metadata for entrypoint at http://localhost:60691/main_module.bootstrap.js
[+2780 ms] ChromeProxyService: Initializing expression compiler for main_module.bootstrap.js with sound null safety: true
[  +99 ms] DevHandler: Debug service listening on ws://127.0.0.1:60738/QLdJTWn6Dk8=/ws

[  +24 ms] This app is linked to the debug service: ws://127.0.0.1:60738/QLdJTWn6Dk8=/ws
[  +12 ms] Debug service listening on ws://127.0.0.1:60738/QLdJTWn6Dk8=/ws
[   +1 ms]  Running with sound null safety 
[   +4 ms]   To hot restart changes while running, press "r" or "R".
[   +1 ms] For a more detailed help message, press "h". To quit, press "q".
[   +2 ms] An Observatory debugger and profiler on Chrome is available at: http://127.0.0.1:60738/QLdJTWn6Dk8=
[+2767 ms] The Flutter DevTools debugger and profiler on Chrome is available at: http://127.0.0.1:9102?uri=http://127.0.0.1:60738/QLdJTWn6Dk8=

flutter analyze

Analyzing bug...                                                        
No issues found! (ran in 2.5s)

flutter doctor -v

[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.22000.376], locale en-IN)
    • Flutter version 2.8.1 at C:\src\flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (3 weeks ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\imaac\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.8.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.8.30907.101
    • Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 4.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.63.2)
    • VS Code at C:\Users\imaac\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.29.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22000.376]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 96.0.4664.110
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 95.0.1020.40

! Doctor found issues in 1 category.

Video Demo

Link to video: https://www.loom.com/share/60e3a165a06e42ee84890a32d6fbc9f6

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsc: regressionIt was better in the past than it is nowf: focusFocus traversal, gaining or losing focusf: material designflutter/packages/flutter/material repository.found in release: 2.8Found to occur in 2.8found in release: 2.9Found to occur in 2.9frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions