Skip to content

[Selection] The selected text in RichText widget is not in correct order when a WidgetSpan is included #127942

@crizant

Description

@crizant

Is there an existing issue for this?

Steps to reproduce

  1. Launch the flutter app with the following code sample
  2. Select all text on the screen
  3. Tap copy or Ctrl+C on the keyboard
  4. Go to another application, such as a browser, paste the copied text.

Expected results

The copied text should be My name is Dash.

Actual results

The copied text is DashMy name is .

Code sample

Code sample
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(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: SelectionArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              _MyWidget(),
            ],
          ),
        ),
      ),
    );
  }
}

class _MyWidget extends StatelessWidget {
  const _MyWidget();

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return RichText(
      selectionRegistrar: SelectionContainer.maybeOf(context),
      selectionColor: DefaultSelectionStyle.of(context).selectionColor,
      text: TextSpan(
        style: const TextStyle(color: Colors.black),
        children: <InlineSpan>[
          const TextSpan(
            text: 'My name is ',
          ),
          WidgetSpan(
            alignment: PlaceholderAlignment.middle,
            child: GestureDetector(
              onTap: () {
                debugPrint('Tap');
              },
              child: Text(
                'Dash',
                style: theme.textTheme.bodyMedium!.copyWith(
                  color: Colors.red,
                ),
              ),
            ),
          ),
          const TextSpan(
            text: '.',
          ),
        ],
      ),
    );
  }
}

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.2, on macOS 13.3.1 22E772610a
    darwin-x64, locale en-AU)
[✓] Android toolchain - develop for Android devices (Android SDK
    version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.78.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: selectionSelectableRegion, SelectionArea, SelectionContainer, Selectable, and related APIsfound in release: 3.10Found to occur in 3.10found in release: 3.11Found to occur in 3.11frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions