setMenus method Null safety

  1. @override
void setMenus(
  1. List<MenuItem> 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<MenuItem> topLevelMenus) {
  _idMap.clear();
  final List<Map<String, Object?>> representation = <Map<String, Object?>>[];
  if (topLevelMenus.isNotEmpty) {
    int index = 0;
    for (final MenuItem childItem in topLevelMenus) {
      representation
          .addAll(childItem.toChannelRepresentation(this, index: index, count: topLevelMenus.length, getId: _getId));
      index += 1;
    }
  }
  // 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);
}