MenuAcceleratorLabel class

A widget that draws the label text for a menu item (typically a MenuItemButton or SubmenuButton) and renders its child with information about the currently active keyboard accelerator.

On platforms other than macOS and iOS, this widget listens for the Alt key to be pressed, and when it is down, will update the label by calling the builder again with the position of the accelerator in the label string. While the Alt key is pressed, it registers a shortcut with the ShortcutRegistry mapped to a VoidCallbackIntent containing the callback defined by the nearest MenuAcceleratorCallbackBinding.

Because the accelerators are registered with the ShortcutRegistry, any other shortcuts in the widget tree between the primaryFocus and the ShortcutRegistry that define Alt-based shortcuts using the same keys will take precedence over the accelerators.

Because accelerators aren't used on macOS and iOS, the label ignores the Alt key on those platforms, and the builder is always given -1 as an accelerator index. Accelerator labels are still stripped of their accelerator markers.

The built-in menu items MenuItemButton and SubmenuButton already provide the appropriate MenuAcceleratorCallbackBinding, so unless you are creating your own custom menu item type that takes a MenuAcceleratorLabel, it is not necessary to provide one.

This example shows a MenuBar that handles keyboard accelerators using MenuAcceleratorLabel. To use the accelerators, press the Alt key to see which letters are underlined in the menu bar, and then press the appropriate letter. Accelerators are not supported on macOS or iOS since those platforms don't support them natively, so this demo will only show a regular Material menu bar on those platforms.

To create a local project with this code sample, run:
flutter create --sample=material.MenuAcceleratorLabel.1 mysample



Creates a const MenuAcceleratorLabel.


builder MenuAcceleratorChildBuilder
The optional MenuAcceleratorChildBuilder which is used to build the widget that displays the label itself.
displayLabel String
Returns the label with any accelerator markers removed.
no setter
hasAccelerator bool
Whether label contains an accelerator definition.
no setter
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
label String
The label string that should be displayed.
runtimeType Type
A representation of the runtime type of the object.
no setterinherited


createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
createState() State<MenuAcceleratorLabel>
Creates the mutable state for this widget at a given location in the tree.
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
toString({DiagnosticLevel minLevel =}) String
A string representation of this object.
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
toStringShort() String
A short, textual description of this widget.


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

Static Methods

defaultLabelBuilder(BuildContext context, String label, int index) Widget
Serves as the default value for builder, rendering the label as a RichText widget with appropriate TextSpans for rendering the label with an underscore under the selected accelerator for the label when the index is non-negative, and a Text widget when the index is negative.
stripAcceleratorMarkers(String label, {void setIndex(int index)?}) String
Strips out any accelerator markers from the given label, and unescapes any escaped ampersands.