EnhancedPlatformMenuDelegate class

EnhancedPlatformMenuDelegate

A PlatformMenuDelegate that bridges Flutter’s platform menu API to a host-platform implementation over a MethodChannel ("enhanced_platform_menu"). It serializes Flutter PlatformMenuItem trees (including enhanced variants) into a channel-friendly payload, sends them to the host, and routes menu selection callbacks back into Flutter.

What it does

  • setMenus / clearMenus: Publishes or clears the entire top-level menu structure via channel methods Menu.Set and Menu.Clear.
  • Serialization: Converts PlatformMenu, PlatformMenuItem, and PlatformMenuItemGroup into a nested map:
    • Menus: { kind: 'menu', label, children, [identifier], [removeDefaults], [icon...] }
    • Leaf items: { kind: 'leaf', id, label, enabled, [shortcut], [checked], [icon...] }
    • Group separators are emitted between grouped items as { kind: 'separator' }.
    • Supports EnhancedPlatformMenu (identifier, removeDefaults, optional icon) and EnhancedPlatformMenuItem (checked, optional icon).
  • Shortcuts: Serializes MenuSerializableShortcut using serializeForMenu().toChannelRepresentation().
  • Selection handling: Registers a per-item callback keyed by a generated id. When the host sends Menu.Selected with that id, the delegate:
    1. Tries to invoke the item’s onSelectedIntent via Actions (using the current focus context or a debug-locked context), and if enabled, calls it; otherwise
    2. Falls back to calling onSelected.

Debug locking (assert-only)

  • debugLockDelegate(context) and debugUnlockDelegate(context) enforce that only one BuildContext (the locker) may be used to resolve Actions when no focused context is available. In release builds these always return true.

Channel contract

  • Outbound:
    • Menu.Set with { menus: [...] }
    • Menu.Clear
  • Inbound:
    • Menu.Selected with { id: String } to trigger the registered callback.

Notes

  • enabled for a leaf is true if onSelected or onSelectedIntent is present.
  • Uses FocusManager.instance.primaryFocus?.context or the locked context to resolve Actions.find / Actions.invoke.
  • Intended to run on the UI isolate; keep lifecycle of the delegate aligned with your app’s menu bar lifecycle.

Should be set in app's main function via WidgetsBinding.instance.platformMenuDelegate = EnhancedPlatformMenuDelegate();

Inheritance

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

attachChannelHandler() → void
clearMenus() Future<void>
Clears any existing platform-rendered menus and leaves the application with no menus.
override
debugLockDelegate(BuildContext context) bool
This is called by PlatformMenuBar when it is initialized, to be sure that only one is active at a time.
override
debugUnlockDelegate(BuildContext context) bool
This is called by PlatformMenuBar when it is disposed, so that another one can take over.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setMenus(List<PlatformMenuItem> topLevelMenus) Future<void>
Sets the entire menu hierarchy for a platform-rendered menu bar.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited