Repository: flutter
Issue: getLocalRectForCaret Returns inconsistent results based on what the last key press was
Labels: a: text input,framework,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.13,found in release: 3.15

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

* Create a EditableText widget
* Assign it a global key with an EditableTextState
* Wrap the text field with an https://api.flutter.dev/flutter/widgets/Overlay-class.html (I used flutter_portal because it makes it easer to manage, however has no influence on the issue so it's not package related)
* Add a listener to the textcontroller
* Just refresh state in the add listener event
* Create a 20px high (arbitrary to match font size) see-through red box and add it in the portal to be shown over the text field
* Get the getLocalRectForCaret from the editable and use the rect to draw the horizontal line

* type into the text field on the first line until you reach the second line
* Keep tying on the second line and somewhere in the middle press backspace, see how the highlight bar moves down to the caret location.

### Expected results

The red line follows where the caret currently is

### Actual results

The red line is positioned at the top of the text box when typing, however on backspace the caret goes to the correct location in the text box and highlights the text.



### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:crashtest/test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey<EditableTextState> editableText = GlobalKey();
  TextEditingController controller =
      TextEditingController(text: "Hello world!");
  FocusNode focus = FocusNode();

  @override
  void initState() {
    super.initState();

    controller.addListener(() {
      setState(() {});
    });
  }

  Widget getfollower() {
    if (editableText.currentState == null) {
      return SizedBox.shrink();
    }

    EditableTextState state = editableText.currentState!;
    Rect rect =
        state.renderEditable.getLocalRectForCaret(controller.selection.base);

    print("rect: " + rect.toString());

    return IgnorePointer(
        ignoring: true,
        child: Padding(
            padding: EdgeInsets.only(top: rect.top),
            child: SizedBox(
                height: 20,
                width: MediaQuery.of(context).size.width,
                child: Container(
                    child: SizedBox.expand(),
                    color: Colors.red.withOpacity(.2)))));
  }

  @override
  Widget build(BuildContext context) {
    return Portal(
        child: Scaffold(
      appBar: AppBar(
          title: Text(
        "Text getLocalRectForCaret issue",
      )),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              //Other widgets
              Container(
                margin: const EdgeInsets.symmetric(horizontal: 20),
                color: Color.fromARGB(255, 48, 48, 48),
                child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: PortalTarget(
                        anchor: Aligned(
                          follower: Alignment.topLeft,
                          target: Alignment.topLeft,
                          widthFactor: 1,
                          offset: Offset(0, 0),
                          backup: const Aligned(
                            follower: Alignment.bottomLeft,
                            target: Alignment.topLeft,
                            widthFactor: 1,
                          ),
                        ),
                        portalFollower: getfollower(),
                        child: EditableText(
                          key: editableText,
                          controller: controller,
                          focusNode: focus,
                          minLines: null,
                          maxLines: null,
                          cursorColor: Colors.blue,
                          backgroundCursorColor: Colors.red,
                          style: TextStyle(),
                        ))),
              ),
            ],
          ),
        ),
      ),
    ));
  }
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>


https://github.com/flutter/flutter/assets/95936504/274a0eef-6923-498a-93c3-8dff4c071ed7


</details>


### Logs

<details><summary>Logs</summary>

When just typing any text on the second text field line
```console
rect: Rect.fromLTRB(-0.1, 0.2, 1.9, 16.2)
```
When backspacing on the second text field line
```console
Rect.fromLTRB(75.5, 16.2, 77.5, 32.2)
```
</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.10.6, on Microsoft Windows [Version 10.0.19045.3448], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.5.3)
[√] Android Studio (version 2022.1)
[√] VS Code, 64-bit edition (version 1.76.2)
[√] Connected device (3 available)
[√] Network resources
```

</details>