buildEditableTextContextMenu function

Widget buildEditableTextContextMenu(
  1. BuildContext innerContext,
  2. EditableTextState editableTextState, {
  3. UndoHistoryController? undoHistoryController,
  4. TargetPlatform? platform,
})

Builds an appropriate context menu for editable text based on platform.

Automatically selects between desktop and mobile context menu implementations based on the current platform.

Parameters:

  • innerContext (BuildContext, required): Build context.
  • editableTextState (EditableTextState, required): Text field state.
  • undoHistoryController (UndoHistoryController?, optional): Undo controller.
  • platform (TargetPlatform?, optional): Override platform detection.

Note: If platform is not provided, it will be inferred from the theme, and on web, it may be treated as mobile on small screens (width < height * 0.8).

Returns: Platform-appropriate context menu widget.

Implementation

Widget buildEditableTextContextMenu(
    BuildContext innerContext, EditableTextState editableTextState,
    {UndoHistoryController? undoHistoryController, TargetPlatform? platform}) {
  if (platform == null) {
    // First we check if the user specified a platform via the theme.
    // When set, this one is favored.
    platform ??= Theme.of(innerContext).specifiedPlatform;

    // If the user did not specify a platform, we do some heuristics for web.
    // On web, we may treat it as mobile on small screens.
    if (kIsWeb && platform == null) {
      final size = MediaQuery.of(innerContext).size;
      // that is, if the width is significantly smaller than height
      if (size.width < size.height * 0.8) {
        // Treat as mobile on small web screens
        platform = TargetPlatform.iOS;
      }
    }

    // Finally, if still null, fall back to default platform.
    platform ??= defaultTargetPlatform;
  }

  switch (platform) {
    case TargetPlatform.android:
    case TargetPlatform.iOS:
      return MobileEditableTextContextMenu(
        anchorContext: innerContext,
        editableTextState: editableTextState,
        undoHistoryController: undoHistoryController,
      );
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
    case TargetPlatform.linux:
    case TargetPlatform.fuchsia:
      return DesktopEditableTextContextMenu(
        anchorContext: innerContext,
        editableTextState: editableTextState,
        undoHistoryController: undoHistoryController,
      );
    // flutter forks might have some additional platforms
    // (e.g. flutter ohos has ohos platforms in TargetPlatform enum)
    // ignore: unreachable_switch_default
    default:
      return DesktopEditableTextContextMenu(
        anchorContext: innerContext,
        editableTextState: editableTextState,
        undoHistoryController: undoHistoryController,
      );
  }
}