- {required SelectionGeometry selectionGeometry,
- required VoidCallback onCopy,
- required VoidCallback onSelectAll,
Returns the ContextMenuButtonItem
s representing the buttons in this
platform's default selection menu.
For example, SelectableRegion uses this to generate the default buttons for its context menu.
See also:
- SelectableRegionState.contextMenuButtonItems, which gives the
ContextMenuButtonItem
s for a specific SelectableRegion. - EditableText.getEditableButtonItems, which performs a similar role but for content that is both selectable and editable.
- AdaptiveTextSelectionToolbar, which builds the toolbar itself, and can
take a list of
ContextMenuButtonItem
s with AdaptiveTextSelectionToolbar.buttonItems. - AdaptiveTextSelectionToolbar.getAdaptiveButtons, which builds the button
Widgets for the current platform given
ContextMenuButtonItem
s.
Implementation
static List<ContextMenuButtonItem> getSelectableButtonItems({
required final SelectionGeometry selectionGeometry,
required final VoidCallback onCopy,
required final VoidCallback onSelectAll,
required final VoidCallback? onShare,
}) {
final bool canCopy = selectionGeometry.status == SelectionStatus.uncollapsed;
final bool canSelectAll = selectionGeometry.hasContent;
final bool platformCanShare = switch (defaultTargetPlatform) {
TargetPlatform.android
=> selectionGeometry.status == SelectionStatus.uncollapsed,
TargetPlatform.macOS
|| TargetPlatform.fuchsia
|| TargetPlatform.linux
|| TargetPlatform.windows
=> false,
// TODO(bleroux): the share button should be shown on iOS but the share
// functionality requires some changes on the engine side because, on iPad,
// it needs an anchor for the popup.
// See: https://github.com/flutter/flutter/issues/141775.
TargetPlatform.iOS
=> false,
};
final bool canShare = onShare != null && platformCanShare;
// On Android, the share button is before the select all button.
final bool showShareBeforeSelectAll = defaultTargetPlatform == TargetPlatform.android;
// Determine which buttons will appear so that the order and total number is
// known. A button's position in the menu can slightly affect its
// appearance.
return <ContextMenuButtonItem>[
if (canCopy)
ContextMenuButtonItem(
onPressed: onCopy,
type: ContextMenuButtonType.copy,
),
if (canShare && showShareBeforeSelectAll)
ContextMenuButtonItem(
onPressed: onShare,
type: ContextMenuButtonType.share,
),
if (canSelectAll)
ContextMenuButtonItem(
onPressed: onSelectAll,
type: ContextMenuButtonType.selectAll,
),
if (canShare && !showShareBeforeSelectAll)
ContextMenuButtonItem(
onPressed: onShare,
type: ContextMenuButtonType.share,
),
];
}