addMenuItem method
Future<bool>
addMenuItem({
- required String itemId,
- required String title,
- int? index,
- SingleActivator? shortcut,
- bool enabled = true,
override
Adds a menu item to the specified menu.
menuId is the identifier of the menu to add the item to (e.g., "main", "File", etc.).
itemId is a unique identifier for this menu item.
title is the display text for the menu item.
index is the optional position to insert the item (null to append).
shortcut is the optional keyboard shortcut using SingleActivator.
enabled determines whether the menu item is initially enabled.
Returns true if the menu item was successfully added.
Implementation
@override
Future<bool> addMenuItem({
required String menuId,
required String itemId,
required String title,
int? index,
SingleActivator? shortcut,
bool enabled = true,
}) async {
try {
final args = <String, dynamic>{
'menuId': menuId,
'itemId': itemId,
'title': title,
if (index != null) 'index': index,
'enabled': enabled,
};
// Add shortcut data if provided
if (shortcut != null) {
final shortcutData = shortcut.toMap();
args['keyEquivalent'] = shortcutData['keyEquivalent'];
args['keyModifiers'] = shortcutData['keyModifiers'];
} else {
args['keyEquivalent'] = '';
args['keyModifiers'] = <String>[];
}
final result = await methodChannel.invokeMethod<bool>(
'addMenuItem',
args,
);
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Error adding menu item: ${e.message}');
return false;
}
}