RawMenuAnchor class

A widget that wraps a child and anchors a floating menu.

The child can be any widget, but is typically a button, a text field, or, in the case of context menus, the entire screen.

The menu overlay of a RawMenuAnchor is shown by calling MenuController.open on an attached MenuController.

When a RawMenuAnchor is opened, overlayBuilder is called to construct the menu contents within an Overlay. The Overlay allows the menu to "float" on top of other widgets. The info argument passed to overlayBuilder provides the anchor's Rect, the Size of the overlay, the TapRegion.groupId used by members of the menu system, and the position argument passed to MenuController.open.

If MenuController.open is called with a position argument, it will be passed to the info argument of the overlayBuilder function.

The RawMenuAnchor does not manage semantics and focus of the menu.

Adding animations to menus

A RawMenuAnchor has no knowledge of animations, as evident from its APIs, which don't involve AnimationController at all. It only knows whether the overlay is shown or hidden.

If another widget intends to implement a menu with opening and closing transitions, RawMenuAnchor's overlay should remain visible throughout both the opening and closing animation durations.

This means that the showOverlay callback passed to onOpenRequested should be called before the first frame of the opening animation. Conversely, hideOverlay within onCloseRequested should only be called after the closing animation has completed.

This also means that, if MenuController.open is called while the overlay is already visible, RawMenuAnchor has no way of knowing whether the menu is currently opening, closing, or stably displayed. The parent widget will need to manage additional information (such as the state of an AnimationController) to determine how to respond in such scenarios.

To programmatically control a RawMenuAnchor, like opening or closing it, or checking its state, you can get its associated MenuController. Use MenuController.maybeOf(BuildContext context) to retrieve the controller for the closest RawMenuAnchor ancestor of a given BuildContext. More detailed usage of MenuController is available in its class documentation.

This example uses a RawMenuAnchor to build a basic select menu with four items.
link

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

This example uses RawMenuAnchor.onOpenRequested and RawMenuAnchor.onCloseRequested to build an animated menu.
link

To create a local project with this code sample, run:
flutter create --sample=widgets.RawMenuAnchor.2 mysample

This example uses RawMenuAnchor.onOpenRequested and RawMenuAnchor.onCloseRequested to build an animated nested menu.
link

To create a local project with this code sample, run:
flutter create --sample=widgets.RawMenuAnchor.3 mysample

Inheritance

Constructors

RawMenuAnchor.new({Key? key, FocusNode? childFocusNode, bool consumeOutsideTaps = false, VoidCallback? onOpen, VoidCallback? onClose, RawMenuAnchorOpenRequestedCallback onOpenRequested = _defaultOnOpenRequested, RawMenuAnchorCloseRequestedCallback onCloseRequested = _defaultOnCloseRequested, bool useRootOverlay = false, RawMenuAnchorChildBuilder? builder, required MenuController controller, required RawMenuAnchorOverlayBuilder overlayBuilder, Widget? child})
A RawMenuAnchor that delegates overlay construction to an overlayBuilder.
const

Properties

builder RawMenuAnchorChildBuilder?
A builder that builds the widget that this RawMenuAnchor surrounds.
final
child Widget?
The optional child to be passed to the builder.
final
childFocusNode FocusNode?
The FocusNode attached to the widget that takes focus when the menu is opened or closed.
final
consumeOutsideTaps bool
Whether a tap event that closes the menu will be permitted to continue on to the gesture arena.
final
controller MenuController
A MenuController that allows opening and closing of the menu from other widgets.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onClose VoidCallback?
Called when the menu overlay is hidden.
final
onCloseRequested RawMenuAnchorCloseRequestedCallback
Called when a request is made to close the menu.
final
onOpen VoidCallback?
Called when the menu overlay is shown.
final
onOpenRequested RawMenuAnchorOpenRequestedCallback
Called when a request is made to open the menu.
final
overlayBuilder RawMenuAnchorOverlayBuilder
Called to build and position the menu overlay.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
useRootOverlay bool
Whether the menu panel should be rendered in the root Overlay.
final

Methods

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

Operators

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