-
Notifications
You must be signed in to change notification settings - Fork 29.2k
Description
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'),
),
],
),
)
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