Repository: flutter
Issue: RangeError when selecting text in SelectionArea
Labels: framework,f: material design,has reproducible steps,f: selection,team-framework,found in release: 3.27,found in release: 3.28

### Steps to reproduce

1. Select the text "Hello".
2. Use `Shift + Left Arrow` to expand the selected text towards the start.
3. After reaching the beginning of the text, pressing `Shift + Left Arrow` causes an error (**RangeError**).
4. While "Hello" is selected, click on the text in the first line (**AssertionError**).

### Expected results

- The text selection stops gracefully at the beginning of the text.
- No errors or unexpected behavior occur when interacting with the text on Line 1.

### Actual results

- Using `Shift + Left Arrow` beyond the start of the text causes a runtime error.
- Clicking on the text in the first line triggers an AssertionError.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: const Center(
        child: SelectionArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Directionality(
                textDirection: TextDirection.ltr,
                child: Text('This is the first line.'),
              ),
              Directionality(
                textDirection: TextDirection.ltr,
                child: Text('hello'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Video demonstration</summary>

https://github.com/user-attachments/assets/465ae9e0-8539-4857-a74d-4f388678b26b

</details>


### Logs

<details open><summary>Logs</summary>

```console
══╡ EXCEPTION CAUGHT BY SERVICES LIBRARY ╞══════════════════════════════════════════════════════════
The following IndexError was thrown while processing the key message handler:
RangeError (index): Index out of range: index should be less than 1: 1

When the exception was thrown, this was the stack

KeyMessage: KeyMessage([KeyDownEvent#3c2fc(physicalKey: PhysicalKeyboardKey#70050(usbHidUsage:
  "0x00070050", debugName: "Arrow Left"), logicalKey: LogicalKeyboardKey#7a008(keyId: "0x100000302",
  keyLabel: "Arrow Left", debugName: "Arrow Left"), character: null, timeStamp: 0:00:08.077599)])
════════════════════════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: Assertion failed: file:///opt/flutter/packages/flutter/lib/src/widgets/selectable_region.dart:2807:14
```

</details>


### Flutter Doctor output

This has been tested on DartPad with Dart SDK 3.6.1 and Flutter SDK 3.27.2 (Stable Channel). However, I also encountered this issue on Windows.
<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel master, 3.28.0-2.0.pre.38795, on Microsoft Windows [Version 10.0.22635.4800]) [6.7s]
    • Flutter version 3.28.0-2.0.pre.38795 on channel master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 79550446df (16 hours ago), 2025-01-20 19:10:34 +0330
    • Engine revision 63110570da
    • Dart version 3.8.0 (build 3.8.0-1.0.dev)
    • DevTools version 2.42.0

[√] Windows Version (11 Enterprise 64-bit, 23H2, 2009) [3.3s]

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [11.7s]
    • Platform android-35, build-tools 33.0.1
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
    • All Android licenses accepted.

[√] Chrome - develop for the web [194ms]

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.4) [193ms]
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.12.35707.178
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1) [34ms]
    • 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)

[√] Connected device (3 available) [219ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22635.4800]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 131.0.6778.265
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 132.0.2957.111

[√] Network resources [893ms]
    • All expected network resources are available.

• No issues found!
```

</details>