Repository: flutter
Issue: Unexpected deltas after changing text in `performAction`
Labels: a: text input,framework,has reproducible steps,P2,team-text-input,triaged-text-input,found in release: 3.22

### Steps to reproduce

1. Run the sample code on iOS 17.5
2. Type "run tom"
3. Press the action button

### Expected results

The editing value should be `. `, since it is the value sent to the IME in  `performAction`.

### Actual results

The following deltas are reported AFTER sending the editing value to the IME in `performAction`:

```dart
TextEditingDeltaReplacement(
    oldText: '. run tom',
    replacementText: 'Tom',
    replacedRange: TextRange(start: 6, end: 9),
    selection: TextSelection.collapsed(
      offset: 9,
      affinity: TextAffinity.downstream,
    ),
    composing: TextRange(start: -1, end: -1),
  );
TextEditingDeltaInsertion(
    oldText: '. run Tom',
    textInserted: '\n',
    insertionOffset: 9,
    selection: TextSelection.collapsed(
      offset: 10,
      affinity: TextAffinity.downstream,
    ),
    composing: TextRange(start: -1, end: -1),
  );
```

This can be confirmed by looking at the logs.

### Code sample

<details><summary>Code sample</summary>

```dart
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: '. ',
    selection: TextSelection.collapsed(offset: 2),
  );

  @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;

    for (final delta in textEditingDeltas) {
      print(delta);
      newEditingValue = delta.apply(newEditingValue);
    }

    setState(() {
      _currentEditingValue = newEditingValue;
    });
  }

  @override
  void performAction(TextInputAction action) {
    if (action == TextInputAction.newline) {
      setState(() {
        // I'm reseting the editing value, I don't expect to receive replacemente deltas
        // refering to the previous value.
        _currentEditingValue = const TextEditingValue(
          text: '. ',
          selection: TextSelection.collapsed(offset: 2),
        );
        _inputConnection!.setEditingState(_currentEditingValue);
      });
    }
  }


  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);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Focus(
        focusNode: _focusNode,
        autofocus: true,
        child: DecoratedBox(
          decoration: BoxDecoration(border: Border.all()),
          child: Text(
            key: _textKey,
            _currentEditingValue.text,
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
  }

  @override
  void connectionClosed() {}

  @override
  AutofillScope? get currentAutofillScope => null;

  @override
  TextEditingValue? get currentTextEditingValue => null;

  @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) {}
}

```

</details>


### Screenshots or Video

_No response_

### Logs

<details open><summary>Logs</summary>

```console
flutter: TextEditingDeltaInsertion#7e39e(oldText: ., textInserted: R, insertionOffset: 2, selection: TextSelection.collapsed(offset: 3, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#c4e44(oldText: . R, textInserted: u, insertionOffset: 3, selection: TextSelection.collapsed(offset: 4, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#daa6a(oldText: . Ru, textInserted: n, insertionOffset: 4, selection: TextSelection.collapsed(offset: 5, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaReplacement#cc868(oldText: . Run, textReplaced: Run, replacementText: run, replacedRange: TextRange(start: 2, end: 5), selection: TextSelection.collapsed(offset: 5, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#f1e9f(oldText: . run, textInserted:, insertionOffset: 5, selection: TextSelection.collapsed(offset: 6, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#db2c2(oldText: . run, textInserted: t, insertionOffset: 6, selection: TextSelection.collapsed(offset: 7, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#bb566(oldText: . run t, textInserted: o, insertionOffset: 7, selection: TextSelection.collapsed(offset: 8, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#f8aaa(oldText: . run to, textInserted: m, insertionOffset: 8, selection: TextSelection.collapsed(offset: 9, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaReplacement#4ba7a(oldText: . run tom, textReplaced: tom, replacementText: Tom, replacedRange: TextRange(start: 6, end: 9), selection: TextSelection.collapsed(offset: 9, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))
flutter: TextEditingDeltaInsertion#7e677(oldText: . run Tom, textInserted:
  , insertionOffset: 9, selection: TextSelection.collapsed(offset: 10, 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.22.0, on macOS 14.4.1 23E224 darwin-arm64, locale
    en-BR)
    • Flutter version 3.22.0 on channel stable at
      /Users/angelosilvestre/dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (9 days ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

[✓] 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 used to retrieve the iOS and macOS platform side's plugin
        code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see
      https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods
      for instructions.

[✓] 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.89.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.88.0

[✓] Connected device (4 available)
    • iPhone 15 Pro (mobile)          • 79D2CA2A-3C07-49AD-A0DF-E4EBCC6BCC1E • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-17-5 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   •
      macOS 14.4.1 23E224 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         •
      macOS 14.4.1 23E224 darwin-arm64
    • Chrome (web)                    • chrome                               • web-javascript •
      Google Chrome 125.0.6422.60

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>