Repository: flutter
Issue: Failed assertion when trying to set caret position to text position that is not visible
Labels: a: text input,c: new feature,c: crash,platform-android,framework,f: material design,a: error message,has reproducible steps,P2,found in release: 3.7,found in release: 3.9,team-framework,triaged-framework


```
════════ Exception caught by scheduler library ═════════════════════════════════
The following assertion was thrown during a scheduler callback:
'package:flutter/src/widgets/editable_text.dart': Failed assertion: line 3549 pos 16: '!composingRange.isValid || composingRange.isCollapsed': is not true.
editable_text.dart:3549
2

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack
#2      EditableTextState._updateComposingRectIfNeeded
editable_text.dart:3549
#3      EditableTextState._updateComposingRectIfNeeded.<anonymous closure>
editable_text.dart:3555
#4      SchedulerBinding._invokeFrameCallback
binding.dart:1289
#5      SchedulerBinding.handleDrawFrame
binding.dart:1227
#6      SchedulerBinding._handleDrawFrame
binding.dart:1076
#7      _invoke (dart:ui/hooks.dart:145:13)
#8      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#9      _drawFrame (dart:ui/hooks.dart:112:31)
(elided 2 frames from class _AssertionError)
```

## Steps to Reproduce

Create a `TextField` that does not allow multi-line input (i.e. don't set `maxLines: null`).

Then set the text of the `TextField` to a string that contains a newline (so that part of the text will not be visible in the text widget), using `setText` (shown below).

This method tries to set the caret position to a position past the end of the visible input in this case, which triggers the exception.

```dart
  final controller = TextEditingController();

  setText(String text) {
    controller.text = text;
    // Move cursor to end of text
    controller.selection = TextSelection.fromPosition(
      TextPosition(offset: controller.text.length),
    );
  }
```

**Expected results:** <!-- what did you want to see? -->

Probably the `TextPosition` should be set to the closest visible position, rather than failing an assertion if the requested position is not visible.

**Actual results:** <!-- what did you see? -->

The above error

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[luke@fedora clickdating_flutter]$ flutter doctor -v
[✓] Flutter (Channel stable, 3.7.5, on Fedora Linux 36 (Workstation Edition)
    6.1.14-100.fc36.x86_64, locale en_US.utf8)
    • Flutter version 3.7.5 on channel stable at /opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c07f788888 (2 weeks ago), 2023-02-22 17:52:33 -0600
    • Engine revision 0f359063c4
    • Dart version 2.19.2
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /home/luke/Android/Sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 14.0.5 (Fedora 14.0.5-2.fc36)
    • cmake version 3.25.2
    • ninja version 1.10.2
    • pkg-config version 1.8.0

[✓] Android Studio (version 2021.3)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 71.0.3
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.76.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.60.0

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 13
      (API 33) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Fedora
      Linux 36 (Workstation Edition) 6.1.14-100.fc36.x86_64
    • Chrome (web)                 • chrome        • web-javascript • Google
      Chrome 110.0.5481.177

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->