Repository: flutter
Issue: Text selection in a reversed ListView in combination with a text input is buggy
Labels: a: text input,framework,f: scrolling,has reproducible steps,P2,f: selection,team-framework,triaged-framework,found in release: 3.16,found in release: 3.19

### Steps to reproduce

1. Add a Selection Area
2. Add a ListView with multiple text widgets and `reversed: true` as child
3. Open the keyboard
4. Press and hold on some text to select it => the keyboard will lose focus and gets closed, the word you pressed gets marked
5. extend the selection, and you will see that the selection is buggy until you reach the position on the screen where you started the selection while the keyboard was still open

Remove the reversed and everything will work fine as the ListView content will not move with the keyboard.

For a minimal working example, see the link below. In the example, you can change the reversed flag by a toggle in the AppBar.

### Expected results

The selection should just work as expected after the keyboard has closed. The fact that the selected text is then at a different place on the screen should not change the behavior.

### Actual results

The text is not selected until you reach the original position of the selected text. After you reached that once, everything is working as expected again.

### Code sample

<details open><summary>Code sample</summary>

[Demo repo on github](https://github.com/guenth39/selection_demo)

```dart
import 'package:flutter/material.dart';
import 'package:flutter_lorem/flutter_lorem.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(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Selection Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> messages = [];

  var reversed = false;

  final controller = TextEditingController();

  @override
  void initState() {
    // Add initial messages with lorem ipsum text.
    for (int i = 0; i < 10; i++) {
      _addMessage();
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
          actions: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text('Normal'),
                  Switch(
                    value: reversed,
                    onChanged: (value) => setState(() => reversed = value),
                  ),
                  const Text('Reverse'),
                ],
              ),
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Expanded(child: _buildContentContainer()),
              _buildInputWidget(),
            ],
          ),
        ),
      );

  void _addMessage([String? message]) =>
      messages.add(message ?? lorem(paragraphs: 1, words: 25));

  Widget _buildContentContainer() => SelectionArea(
        child: ListView.builder(
          reverse: reversed,
          itemCount: messages.length,
          itemBuilder: (context, index) => Message(message: messages[index]),
        ),
      );

  Widget _buildInputWidget() => SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: [
              Expanded(
                child: TextField(
                  controller: controller,
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Message',
                  ),
                ),
              ),
              IconButton(
                onPressed: () {
                  _addMessage(controller.text);
                  controller.clear();
                },
                icon: const Icon(Icons.send),
              ),
            ],
          ),
        ),
      );
}

class Message extends StatelessWidget {
  const Message({
    super.key,
    required this.message,
  });

  final String message;

  @override
  Widget build(BuildContext context) => Padding(
        padding: const EdgeInsets.all(8.0),
        child: Card(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(message),
          ),
        ),
      );
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/flutter/flutter/assets/29251617/1d4a8a84-6c5a-4943-b083-239fc3cf2dea

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.16.6, on macOS 14.2.1 23C71 darwin-arm64, locale de-DE)
    • Flutter version 3.16.6 on channel stable at /Users/A1D678A/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 46787ee49c (2 days ago), 2024-01-09 14:36:07 -0800
    • Engine revision 3f3e560236
    • Dart version 3.2.3
    • DevTools version 2.28.4

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/A1D678A/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.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] 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.85.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (4 available)
    • Pixel 8 Pro (mobile)       • 37071FDJG0008D                       • android-arm64  • Android 14 (API 34)
    • iPhone 15 Pro Max (mobile) • 61F56D74-8A51-4511-B398-5E7D389E6493 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)            • macos                                • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)               • chrome                               • web-javascript • Google Chrome 120.0.6099.216

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>