show method

Future<void> show(
  1. Rect targetRect
)

Shows the system context menu anchored on the given Rect.

The Rect represents what the context menu is pointing to. For example, for some text selection, this would be the selection Rect.

There can only be one system context menu visible at a time. Calling this while another system context menu is already visible will remove the old menu before showing the new menu.

Currently this system context menu is bound to text input. The buttons that are shown and the actions they perform are dependent on the currently active TextInputConnection. Using this without an active TextInputConnection will be a noop.

This is only supported on iOS 16.0 and later.

See also:

Implementation

Future<void> show(Rect targetRect) {
  assert(!_isDisposed);
  assert(
    TextInput._instance._currentConnection != null,
    'Currently, the system context menu can only be shown for an active text input connection',
  );

  // Don't show the same thing that's already being shown.
  if (_lastShown != null && _lastShown!._isVisible && _lastShown!._lastTargetRect == targetRect) {
    return Future<void>.value();
  }

  assert(
    _lastShown == null || _lastShown == this || !_lastShown!._isVisible,
    'Attempted to show while another instance was still visible.',
  );

  _lastTargetRect = targetRect;
  _lastShown = this;
  _hiddenBySystem = false;
  return _channel.invokeMethod<Map<String, dynamic>>(
    'ContextMenu.showSystemContextMenu',
    <String, dynamic>{
      'targetRect': <String, double>{
        'x': targetRect.left,
        'y': targetRect.top,
        'width': targetRect.width,
        'height': targetRect.height,
      },
    },
  );
}