setMenus method

  1. @override
void setMenus(
  1. List<PlatformMenuItem> topLevelMenus
)
override

Sets the entire menu hierarchy for a platform-rendered menu bar.

The topLevelMenus argument is the list of menus that appear in the menu bar, which themselves can have children.

To update the menu hierarchy or menu item state, call setMenus with the modified hierarchy, and it will overwrite the previous menu state.

See also:

  • PlatformMenuBar, the widget that adds a platform menu bar to an application.
  • PlatformMenu, the class that describes a menu item with children that appear in a cascading menu.
  • PlatformMenuItem, the class that describes the leaves of a menu hierarchy.

Implementation

@override
void setMenus(List<PlatformMenuItem> topLevelMenus) {
  _idMap.clear();
  final List<Map<String, Object?>> representation = <Map<String, Object?>>[];
  if (topLevelMenus.isNotEmpty) {
    for (final PlatformMenuItem childItem in topLevelMenus) {
      representation.addAll(childItem.toChannelRepresentation(this, getId: _getId));
    }
  }
  // Currently there's only ever one window, but the channel's format allows
  // more than one window's menu hierarchy to be defined.
  final Map<String, Object?> windowMenu = <String, Object?>{
    '0': representation,
  };
  channel.invokeMethod<void>(_kMenuSetMethod, windowMenu);
}