getSelectableButtonItems static method

List<ContextMenuButtonItem> getSelectableButtonItems({
  1. required SelectionGeometry selectionGeometry,
  2. required VoidCallback onCopy,
  3. required VoidCallback onSelectAll,
  4. required VoidCallback? onShare,
})

Returns the ContextMenuButtonItems 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:

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),
  ];
}