Skip to content

Proposal: Simple custom text selection toolbars #73574

@justinmc

Description

@justinmc

It should be easier to create and modify custom text selection toolbars.

Use case

Users often want to just do something simple like add an extra button to the existing text selection toolbar. That should look something like this:

TextField(
  textSelectionToolbar: TextSelectionToolbar(
    children: <Widget>[
      ...TextSelectionToolbar.defaultChildren,
      TextSelectionToolbarTextButton(
        onPressed: () { /* Do special stuff */ },
        child: const Text('My special button'),
      ),
    ],
  ),
)

Screen Shot 2021-01-08 at 10 17 01 AM

Context

Recently, #69428 made it possible to customize the Material text selection toolbar and buttons, and #73578 does the same for Cupertino. However, the current TextSelectionControls interface still requires a bunch of extra work in order to be able to use a custom toolbar. Rather than the simple example above, it looks more like this:

TextField(
  textSelectionControls: MyTextSelectionControls(),
)

...

class MyTextSelectionControls extends TextSelectionControls {
  @override
  Widget buildToolbar(
    // Lots of confusing parameters that I don't care about when I just want to add a button:
    BuildContext context,
    Rect globalEditableRegion,
    double textLineHeight,
    Offset selectionMidpoint,
    List<TextSelectionPoint> endpoints,
    TextSelectionDelegate delegate,
    ClipboardStatusNotifier clipboardStatus,
  ) {
    /*
      ... Lots of confusing math that's duplicating what we do internally to calculate the anchors...
   */

    return TextSelectionToolbar(
      anchorAbove: anchorAbove,
      anchorBelow: anchorBelow,
      children: <Widget>[
        ...aRecreationOfTheDefaultButtons,
        TextSelectionToolbarTextButton(
          onPressed: () { /* Do special stuff */ },
          child: const Text('My special button'),
        ),
      ],
    );
  }
}

Proposal

I propose that we make a breaking change to the existing TextSelectionControls and its Material and Cupertino counterparts. The new interface should allow something similar to the simplicity of the first example above. It should be built around TextSelectionToolbar and should not hardcode a preference for the existing default buttons.

I plan to update this with a full design doc if I'm able to get around to it.

Update: Design doc

Metadata

Metadata

Assignees

Labels

a: text inputEntering text in a text field or keyboard related problemsc: proposalA detailed proposal for a change to Flutterf: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions