contextMenuBuilder property
Builds the text selection toolbar when requested by the user.
The context menu is built when EditableTextState.showToolbar is called, typically by one of the callbacks installed by the widget created by TextSelectionGestureDetectorBuilder.buildGestureDetector. The widget returned by contextMenuBuilder is passed to a ContextMenuController.
If no callback is provided, no context menu will be shown.
The EditableTextContextMenuBuilder signature used by the contextMenuBuilder callback has two parameters, the BuildContext of the EditableText and the EditableTextState of the EditableText.
The EditableTextState has two properties that are especially useful when building the widgets for the context menu:
-
EditableTextState.contextMenuAnchors specifies the desired anchor position for the context menu.
-
EditableTextState.contextMenuButtonItems represents the buttons that should typically be built for this widget (e.g. cut, copy, paste).
The TextSelectionToolbarLayoutDelegate class may be particularly useful in honoring the preferred anchor positions.
For backwards compatibility, when EditableText.selectionControls is set to an object that does not mix in TextSelectionHandleControls, contextMenuBuilder is ignored and the TextSelectionControls.buildToolbar method is used instead.
To create a local project with this code sample, run:
flutter create --sample=widgets.EditableText.contextMenuBuilder.1 mysample
To create a local project with this code sample, run:
flutter create --sample=widgets.EditableText.contextMenuBuilder.2 mysample
See also:
- AdaptiveTextSelectionToolbar, which builds the default text selection toolbar for the current platform, but allows customization of the buttons.
- AdaptiveTextSelectionToolbar.getAdaptiveButtons, which builds the button Widgets for the current platform given ContextMenuButtonItems.
- BrowserContextMenu, which allows the browser's context menu on web to be disabled and Flutter-rendered context menus to appear.
Implementation
final EditableTextContextMenuBuilder? contextMenuBuilder;