Repository: flutter
Issue: [macOS] Dismissing the IME accent panel produces invalid character
Labels: a: text input,platform-mac,has reproducible steps,P2,team-text-input,triaged-text-input,found in release: 3.24,found in release: 3.25

### Steps to reproduce

1. Run the sample app on macOS
2. Press and hold "u" until the IME accent panel is displayed
3. Press right arrow
4. Press left arrow
5. Press ESC
6. Press left arrow

### Expected results

The accented letter should be removed.

### Actual results

An invalid character is reported.

### Code sample

<details open><summary>Code sample</summary>

```dart
// ignore_for_file: avoid_print

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: TextInputExample(),
      ),
    ),
  );
}

class TextInputExample extends StatefulWidget {
  const TextInputExample({super.key});

  @override
  State<TextInputExample> createState() => _TextInputExampleState();
}

class _TextInputExampleState extends State<TextInputExample> implements DeltaTextInputClient {
  final FocusNode _focusNode = FocusNode();
  final GlobalKey _textKey = GlobalKey();

  TextInputConnection? _inputConnection;
  TextEditingValue _currentEditingValue = const TextEditingValue(
    text: 'Press and hold "u" here: ',
    selection: TextSelection.collapsed(offset: 25),
  );

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _attach();
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  void updateEditingValueWithDeltas(List<TextEditingDelta> textEditingDeltas) {
    TextEditingValue newEditingValue = _currentEditingValue;

    print('received ${textEditingDeltas.length} delta(s)');
    for (final delta in textEditingDeltas) {
      print('apply delta: $delta');
      newEditingValue = delta.apply(newEditingValue);
      print('new editing value: $newEditingValue');
    }

    setState(() {
      _currentEditingValue = newEditingValue;
    });
  }

  void _attach() {
    if (_inputConnection != null) {
      return;
    }
    _inputConnection = TextInput.attach(
      this,
      const TextInputConfiguration(
        enableDeltaModel: true,
        inputType: TextInputType.multiline,
        textCapitalization: TextCapitalization.sentences,
        inputAction: TextInputAction.newline,
        keyboardAppearance: Brightness.light,
      ),
    );
    _inputConnection!.show();
    _inputConnection!.setEditingState(_currentEditingValue);

    _reportVisualsToIme();
  }

  KeyEventResult _onKeyEvent(FocusNode node, KeyEvent event) {
    return KeyEventResult.skipRemainingHandlers;
  }

  /// Position the IME popovers close to the text.
  void _reportVisualsToIme() {
    final renderBox = _textKey.currentContext!.findRenderObject() as RenderBox;

    _inputConnection!.setEditableSizeAndTransform(renderBox.size, renderBox.getTransformTo(null));
    _inputConnection!.setCaretRect(Offset.zero & renderBox.size);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Focus(
        focusNode: _focusNode,
        autofocus: true,
        onKeyEvent: _onKeyEvent,
        child: DecoratedBox(
          decoration: BoxDecoration(border: Border.all()),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              key: _textKey,
              _currentEditingValue.text,
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }

  @override
  void connectionClosed() {}

  @override
  AutofillScope? get currentAutofillScope => null;

  @override
  TextEditingValue? get currentTextEditingValue => _currentEditingValue;

  @override
  void insertTextPlaceholder(Size size) {}

  @override
  void performPrivateCommand(String action, Map<String, dynamic> data) {}

  @override
  void removeTextPlaceholder() {}

  @override
  void showAutocorrectionPromptRect(int start, int end) {}

  @override
  void showToolbar() {}

  @override
  void updateEditingValue(TextEditingValue value) {}

  @override
  void updateFloatingCursor(RawFloatingCursorPoint point) {}

  @override
  void didChangeInputControl(TextInputControl? oldControl, TextInputControl? newControl) {}

  @override
  void insertContent(KeyboardInsertedContent content) {}

  @override
  void performSelector(String selectorName) {}

  @override
  void performAction(TextInputAction action) {}
}

```

</details>



### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/user-attachments/assets/b0d79e36-c1e3-4cc2-8dcf-a8a65bea3cad

Here's the behavior in Apple Notes:

https://github.com/user-attachments/assets/abb1995a-864a-413a-9f56-a3a1da414633

</details>


### Logs

<details open><summary>Logs</summary>

```console
Launching lib/main.dart on macOS in debug mode...
--- xcodebuild: WARNING: Using the first of multiple matching destinations:
{ platform:macOS, arch:arm64, id:00006020-001161A802C3C01E, name:My Mac }
{ platform:macOS, arch:x86_64, id:00006020-001161A802C3C01E, name:My Mac }
✓ Built build/macos/Build/Products/Debug/text_input_demo.app
Connecting to VM Service at ws://127.0.0.1:57181/9Q8cH4TpP6Y=/ws
Connected to the VM Service.
flutter: received 1 delta(s)
flutter: apply delta: TextEditingDeltaInsertion#def36(oldText: Press and hold "u" here:, textInserted: u, insertionOffset: 25, selection: TextSelection.collapsed(offset: 26, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: new editing value: TextEditingValue(text: ┤Press and hold "u" here: u├, selection: TextSelection.collapsed(offset: 26, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: received 1 delta(s)
flutter: apply delta: TextEditingDeltaReplacement#e0e10(oldText: Press and hold "u" here: u, textReplaced: u, replacementText: ú, replacedRange: TextRange(start: 25, end: 26), selection: TextSelection(baseOffset: 25, extentOffset: 26, isDirectional: false), composing: TextRange(start: 25, end: 26))
flutter: new editing value: TextEditingValue(text: ┤Press and hold "u" here: ú├, selection: TextSelection(baseOffset: 25, extentOffset: 26, isDirectional: false), composing: TextRange(start: 25, end: 26))
flutter: received 1 delta(s)
flutter: apply delta: TextEditingDeltaReplacement#36776(oldText: Press and hold "u" here: ú, textReplaced: ú, replacementText: , replacedRange: TextRange(start: 25, end: 26), selection: TextSelection.collapsed(offset: 26, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: new editing value: TextEditingValue(text: ┤Press and hold "u" here: ├, selection: TextSelection.collapsed(offset: 26, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))

```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.24.0, on macOS 14.5 23F79 darwin-arm64, locale
    en-BR)
    • Flutter version 3.24.0 on channel stable at
      /Users/angelosilvestre/dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 80c2e84975 (3 weeks ago), 2024-07-30 23:06:49 +0700
    • Engine revision b8800d88be
    • Dart version 3.5.0
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/angelosilvestre/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    ! CocoaPods 1.12.1 out of date (1.13.0 is recommended).
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      To update CocoaPods, see
      https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.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
      17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.92.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.94.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 127.0.6533.120

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>