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 methodsMenu.SetandMenu.Clear.- Serialization: Converts
PlatformMenu,PlatformMenuItem, andPlatformMenuItemGroupinto 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) andEnhancedPlatformMenuItem(checked, optional icon).
- Menus:
- Shortcuts: Serializes
MenuSerializableShortcutusingserializeForMenu().toChannelRepresentation(). - Selection handling: Registers a per-item callback keyed by a generated
id. When the host sendsMenu.Selectedwith thatid, the delegate:- Tries to invoke the item’s
onSelectedIntentviaActions(using the current focus context or a debug-locked context), and if enabled, calls it; otherwise - Falls back to calling
onSelected.
- Tries to invoke the item’s
Debug locking (assert-only)
debugLockDelegate(context)anddebugUnlockDelegate(context)enforce that only oneBuildContext(the locker) may be used to resolveActionswhen no focused context is available. In release builds these always returntrue.
Channel contract
- Outbound:
Menu.Setwith{ menus: [...] }Menu.Clear
- Inbound:
Menu.Selectedwith{ id: String }to trigger the registered callback.
Notes
enabledfor a leaf is true ifonSelectedoronSelectedIntentis present.- Uses
FocusManager.instance.primaryFocus?.contextor the locked context to resolveActions.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
-
- Object
- PlatformMenuDelegate
- EnhancedPlatformMenuDelegate
Constructors
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