Repository: flutter
Issue: TextField as WidgetSpan mixes parent TextField initial text when delete or arrow keys used
Labels: a: text input,framework,has reproducible steps,P2,team-text-input,triaged-text-input,found in release: 3.24,found in release: 3.27

### Steps to reproduce

Have attached complete sample code which shows the issue but steps to recreate are:

1. create a new flutter app
2. create a custom TextEditingController and set the "text" value within the constructor to any text string (this is to make the issue easier to see, if this value isn't set instead of the text being replaced in the WidgetSpan TextField it is deleted, so still a problem)
3. within the custom controller add a WidgetSpan with a child of a TextField and return it as the child of a TextSpan
4. in the app add a TextField within MainApp using the custom controller as its controller
5. run the app against a Windows or Mac target (I assume linux)
6. in the WidgetSpan text field enter some text then press the delete key or arrow keys

See attached example for a bare bones app showing the problem

### Expected results

Normal text field behaviour or deleting, moving cursor etc...

### Actual results

Windows and Mac - Text field text is replaced the the "text" value from the constructor of the parent text field.

Web - seems to work correctly

Android - when using the software keyboard the delete key works correctly and can navigate using touch, however when using a hardware keyboard via the emulator the problem occurs

Ios - software keyboard works fine too, however when using a hardware keyboard via the emulator the delete key works fine but when pressing an arrow left or right the problem occurs

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {

  late CustomTextController _controller;
  late ChildTextField _childTextField;

  @override
  void initState() {
    super.initState();
    _childTextField = ChildTextField();
    _controller = CustomTextController(childTextField: _childTextField);
  }

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
      home: Scaffold(
        body: Container(
          height: 300,
          width: 300,
          child: TextField(
            controller: _controller,
            maxLines: null,
          ),
        ),
      ),
    );
  }
}

class CustomTextController extends TextEditingController {
  final ChildTextField childTextField;

  CustomTextController({
    required this.childTextField,
  }){
    text = "Initial text in parent text field";
  }

  @override
  TextSpan buildTextSpan({
    required BuildContext context,
    TextStyle? style,
    required bool withComposing,
  }) {
    final List<InlineSpan> children = [];

    children.add(TextSpan(text: "First line in parent text field", style: style));
    children.add(TextSpan(text: "\n\n\n", style: style));
    children.add(
      WidgetSpan(
        child: Container(
              height: 40, width: 500, decoration: BoxDecoration(border: Border.all(color: Colors.red)), child: childTextField),        
      ),
    );
    children.add(TextSpan(text: "\n\n", style: style));
    children.add(TextSpan(text: "End line of parent text field", style: style));

    return TextSpan(style: style, children: children);
  }
}

class ChildTextField extends TextField {
  const ChildTextField({super.key});

  @override
  State<ChildTextField> createState() => _ChildTextFieldState();
}

class _ChildTextFieldState extends State<ChildTextField> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(      
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
    );
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

Video showing the issue on Windows, when the delete key was pressed the text was replaced with the text constructor value from the parent TextField.

</details>

https://github.com/user-attachments/assets/e3bf21a8-f422-4c6f-a279-41fcb8ac7fde

In case it's of use when I was reviewing editable_text.dart in the Flutter source code I could see occurrences of the wrong value being passed in to function, i.e. the text value from the parent TextField was being passed into a function being called for the WidgetSpan TextField.  The watch window shows _value variable being the child TextField value and the value parameter is set to text from the parent TextField.

<img width="1237" alt="Flutter WidgetSpan TextField Bug Debug Screenshot EditableText" src="https://github.com/user-attachments/assets/dc90c8c5-2d76-4844-abd2-174fafdd5ad0">


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
PS C:\dev\repos\widget_span_text_field_example> flutter doctor -v
[√] Flutter (Channel stable, 3.24.5, on Microsoft Windows [Version 10.0.22631.4460], locale en-GB)
    • Flutter version 3.24.5 on channel stable at C:\dev\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision dec2ee5c1f (6 days ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\t\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/to/windows-android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Enterprise 2022 17.12.0 Preview 1.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Preview
    • Visual Studio Enterprise 2022 version 17.12.35209.166
    • The current Visual Studio installation is a pre-release version. It may not be supported by Flutter yet.
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1)
    • Android Studio at C:\Users\t\AppData\Local\Programs\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 17.0.11+0--11852314)

[√] IntelliJ IDEA Community Edition (version 2024.2)
    • IntelliJ at C:\Users\t\AppData\Local\Programs\IntelliJ IDEA Community Edition
    • Flutter plugin version 81.1.3
    • Dart plugin version 242.20629

[√] IntelliJ IDEA Ultimate Edition (version 2023.3)
    • IntelliJ at C:\Users\t\AppData\Local\Programs\IntelliJ IDEA Ultimate
    • Flutter plugin version 77.0.1
    • Dart plugin version 233.13135.65

[√] VS Code, 64-bit edition (version unknown)
    • VS Code at C:\Program Files\Microsoft VS Code Insiders
    • Flutter extension version 3.101.20241031
    X Unable to determine VS Code version.

[√] VS Code (version 1.96.0-insider)
    • VS Code at C:\Users\t\AppData\Local\Programs\Microsoft VS Code Insiders
    • Flutter extension version 3.101.20241031

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.22631.4460]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 131.0.6778.69
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 131.0.2903.51

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>