Repository: flutter
Issue: TextSelection is lost when clicking on a button
Labels: a: text input,framework,a: typography,f: focus,has reproducible steps,P2,found in release: 2.5,found in release: 2.6,team-framework,triaged-framework

## Steps to Reproduce

1. Run `flutter create bug`.
2. Update the files as stated below: main.dart
3. Run `flutter run -d chrome`.
4. Write text in the TextField, select some of it, and press the button

The problem appears to be only on web, works fine in mobile.

main.dart:
```
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: const Play(),
    );
  }
}

class Play extends StatefulWidget {
  const Play({Key? key}) : super(key: key);

  @override
  _PlayState createState() => _PlayState();
}

class _PlayState extends State<Play> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _node = FocusNode();

  void _onPressed() {
    TextSelection selection = _controller.selection;

    print("selection.start: ${selection.start}");
    print("selection.end: ${selection.end}");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TextFormField(
            focusNode: _node,
            controller: _controller,
          ),
          ElevatedButton(
            onPressed: _onPressed,
            child: const Text("do something"),
          ),
        ],
      ),
    );
  }
}


```



**Expected results:**
The TextSelection should mirror the one actually performed.

**Actual results:**
The focus on the TextField was lost, the selection dropped and the TextSelection goes from -1 to -1 aka no selection at all.

<details>
  <summary>Logs</summary>

```
PS C:\_workspace\bug> flutter run -d chrome -v
[  +60 ms] executing: [C:\_libs\flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +375 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +1 ms] ffb2ecea5223acdd139a5039be2f9c796962833d
[        ] executing: [C:\_libs\flutter/] git tag --points-at ffb2ecea5223acdd139a5039be2f9c796962833d
[  +54 ms] Exit code 0 from: git tag --points-at ffb2ecea5223acdd139a5039be2f9c796962833d
[        ] 2.5.1
[   +7 ms] executing: [C:\_libs\flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[  +29 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [C:\_libs\flutter/] git ls-remote --get-url origin
[  +26 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[  +93 ms] executing: [C:\_libs\flutter/] git rev-parse --abbrev-ref HEAD
[  +28 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[  +61 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.
[   +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.
[  +81 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.
[   +2 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.
[  +74 ms] Skipping pub get: version match.
[  +89 ms] Generating C:\_workspace\bug\android\app\src\main\java\io\flutter\plugins\GeneratedPluginRegistrant.java
[  +94 ms] Launching lib\main.dart on Chrome in debug mode...
[  +97 ms] Updating assets
[  +71 ms] Waiting for connection from debug service on Chrome...
[  +78 ms] <- reset
[   +6 ms] C:\_libs\flutter\bin\cache\dart-sdk\bin\dart.exe --disable-dart-dev C:\_libs\flutter\bin\cache\artifacts\engine\windows-x64\frontend_server.dart.snapshot --sdk-root C:\_libs\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\Manuel\AppData\Local\Temp\flutter_tools.24b1569d\flutter_tool.bd3675d1\app.dill   
--libraries-spec file:///C:/_libs/flutter/bin/cache/flutter_web_sdk/libraries.json --packages C:\_workspace\bug\.dart_tool\package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts
--track-widget-creation --filesystem-root C:\Users\Manuel\AppData\Local\Temp\flutter_tools.24b1569d\flutter_tools.6937be43 --filesystem-scheme org-dartlang-app --initialize-from-dill
build\b1b715402d823b7fd5c2b68d2edcb2ce.cache.dill.track.dill --platform file:///C:/_libs/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk_sound.dill --sound-null-safety
[   +8 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+12407 ms] Waiting for connection from debug service on Chrome... (completed in 12,5s)
[   +1 ms] Synced 26.2MB.
[        ] <- accept
[        ] Caching compiled dill
[ +142 ms] [CHROME]:
[   +1 ms] [CHROME]:DevTools listening on ws://127.0.0.1:54240/devtools/browser/82c8bbad-2ad7-4107-b4f2-f0b6e8825e20
[ +325 ms] DwdsInjector: Received request for entrypoint at http://localhost:54229/main_module.bootstrap.js
[   +3 ms] MetadataProvider: Loading debug metadata...
[  +10 ms] MetadataProvider: Loaded debug metadata (sound null safety)
[   +5 ms] DwdsInjector: Injected debugging metadata for entrypoint at http://localhost:54229/main_module.bootstrap.js
[+1207 ms] ChromeProxyService: Initializing expression compiler for main_module.bootstrap.js with sound null safety: true
[  +55 ms] DevHandler: Debug service listening on ws://127.0.0.1:50407/ndIJOheW8hk=/ws

[  +20 ms] This app is linked to the debug service: ws://127.0.0.1:50407/ndIJOheW8hk=/ws
[   +9 ms] Debug service listening on ws://127.0.0.1:50407/ndIJOheW8hk=/ws
[   +1 ms]  Running with sound null safety 
[   +3 ms]   To hot restart changes while running, press "r" or "R".
[        ] For a more detailed help message, press "h". To quit, press "q".
[   +1 ms] An Observatory debugger and profiler on Chrome is available at: http://127.0.0.1:50407/ndIJOheW8hk=
[ +393 ms] DevTools activation throttled until 2021-09-28 00:32:28.431874.
[ +631 ms] The Flutter DevTools debugger and profiler on Chrome is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:50407/ndIJOheW8hk=
[+4731 ms] selection.start: -1
[   +1 ms] selection.end: -1
```

```
PS C:\_workspace\bug> flutter analyze
Analyzing bug...

   info - Avoid `print` calls in production code - lib\main.dart:36:5 - avoid_print
   info - Avoid `print` calls in production code - lib\main.dart:37:5 - avoid_print

2 issues found. (ran in 1.9s)
```

```
PS C:\_workspace\bug> flutter doctor -v
[√] Flutter (Channel stable, 2.5.1, on Microsoft Windows [Version 10.0.18363.1801], locale de-AT)
    • Flutter version 2.5.1 at C:\_libs\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffb2ecea52 (10 days ago), 2021-09-17 15:26:33 -0400
    • Engine revision b3af521a05
    • Dart version 2.14.2

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\Manuel\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\Users\Manuel\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\203.7678000\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Users\Manuel\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\203.7678000
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] IntelliJ IDEA Ultimate Edition (version 2021.2)
    • IntelliJ at C:\Users\Manuel\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\212.5080.55
    • 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

[√] VS Code (version 1.60.2)
    • VS Code at C:\Users\Manuel\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.24.0

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 93.0.4577.82
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 94.0.992.31

• No issues found!
```

</details>