pub struct RawMenuAnchor {
pub key: Option<KeyRef>,
pub on_open: Option<VoidCallback>,
pub on_close: Option<VoidCallback>,
pub on_open_requested: RawMenuAnchorOpenRequestedCallback,
pub on_close_requested: RawMenuAnchorCloseRequestedCallback,
pub builder: Option<RawMenuAnchorChildBuilder>,
pub child: Option<WidgetRef>,
pub overlay_builder: RawMenuAnchorOverlayBuilder,
pub use_root_overlay: bool,
pub child_focus_node: Option<FocusNodeRef>,
pub consume_outside_taps: bool,
pub controller: MenuControllerRef,
}Expand description
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, overlay_builder 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 overlay_builder provides the anchor’s
Rect, the Size of the overlay, the TapRegion::group_id 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 overlay_builder 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 show_overlay callback passed to
on_open_requested should be called before the first frame of
the opening animation. Conversely, hide_overlay within
on_close_requested 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::maybe_of to
retrieve the controller for the closest RawMenuAnchor ancestor of a given
BuildContext. More detailed usage of MenuController is available in its type
documentation.
Fields§
§key: Option<KeyRef>See Widget::key.
on_open: Option<VoidCallback>Called when the menu overlay is shown.
When MenuController::open is called, on_open_requested
is invoked with a show_overlay callback that, when called, shows the menu overlay and
triggers on_open.
The default implementation of on_open_requested calls
show_overlay synchronously, thereby calling on_open synchronously.
In this case, on_open is called regardless of whether the menu
overlay is already showing.
Custom implementations of on_open_requested can delay the
call to show_overlay, or not call it at all, in which case on_open
will not be called. Calling show_overlay after disposal is a no-op, and will not
trigger on_open.
A typical usage is to respond when the menu first becomes interactive, such as by setting focus to a menu item.
on_close: Option<VoidCallback>Called when the menu overlay is hidden.
When MenuController::close is called,
on_close_requested is invoked with a hide_overlay
callback that, when called, hides the menu overlay and triggers
on_close.
The default implementation of on_close_requested calls
hide_overlay synchronously, thereby calling on_close
synchronously. In this case, on_close is called regardless of
whether the menu overlay is already hidden.
Custom implementations of on_close_requested can delay the
call to hide_overlay or not call it at all, in which case
on_close will not be called. Calling hide_overlay after disposal
is a no-op, and will not trigger on_close.
on_open_requested: RawMenuAnchorOpenRequestedCallbackCalled when a request is made to open the menu.
This callback is triggered every time MenuController::open is called, even when the
menu overlay is already showing. As a result, this callback is a good place to begin
menu opening animations, or observe when a menu is repositioned.
After an open request is intercepted, the show_overlay callback should be called when
the menu overlay (the widget built by overlay_builder) is
ready to be shown. This can occur immediately (the default behavior), or after a delay.
Calling show_overlay sets MenuController::is_open to true, builds (or rebuilds)
the overlay widget, and shows the menu overlay at the front of the overlay stack.
If show_overlay is not called, the menu will stay hidden. Calling show_overlay after
disposal is a no-op, meaning it will not trigger on_open or show the
menu overlay.
If a RawMenuAnchor is used in a themed menu that plays an opening animation, the
themed menu should show the overlay before starting the opening animation, since the
animation plays on the overlay itself.
The position argument is the position that MenuController::open was called
with.
A typical on_open_requested consists of the following
steps:
- Optional delay.
- Call
show_overlay(whose call chain eventually invokeson_open). - Optionally start the opening animation.
Defaults to a callback that immediately shows the menu.
on_close_requested: RawMenuAnchorCloseRequestedCallbackCalled when a request is made to close the menu.
This callback can be used to add a delay or a closing animation before the menu is hidden.
This callback is triggered every time MenuController::close is called, even when the
menu overlay is already hidden.
This callback is also triggered when a sibling RawMenuAnchor is opened. In this
case, the callback can be used to add a delay or a closing animation while the sibling
menu opens. When implementing this behavior, consider disabling interactions so that the
closing menu does not interfere with the opening sibling menu. Also consider disabling
semantics, focus, and hit testing for the closing menu for the duration of the closing
animation.
Pending timers or animations started in a previous call to
on_close_requested should be canceled when this callback
is triggered to prevent them from closing the menu at an unintended time.
If the menu is not closed, this callback will also be called when the root menu anchor is scrolled and when the screen is resized.
After a close request is intercepted and closing behaviors have completed, the
hide_overlay callback should be called. This callback sets MenuController::is_open
to false and hides the menu overlay widget. If the RawMenuAnchor is used in a themed
menu that plays a closing animation, hide_overlay should be called after the closing
animation has ended, since the animation plays on the overlay itself. This means that
MenuController::is_open will stay true while closing animations are running.
Calling hide_overlay after disposal is a no-op, meaning it will not trigger
on_close or hide the menu overlay.
Typically, on_close_requested consists of the following
steps:
- Optionally start the closing animation and wait for it to complete.
- Call
hide_overlay(whose call chain eventually invokeson_close).
Throughout the closing sequence, menus should typically not be focusable or interactive.
Defaults to a callback that immediately hides the menu.
builder: Option<RawMenuAnchorChildBuilder>A builder that builds the widget that this RawMenuAnchor surrounds.
Typically, this is a button used to open the menu by calling MenuController::open on
the controller passed to the builder.
If not supplied, then the RawMenuAnchor will be the size that its parent allocates
for it.
child: Option<WidgetRef>The optional child to be passed to the builder.
Supply this child if there is a portion of the widget tree built in
builder that doesn’t depend on the controller or context supplied
to the builder. It will be more efficient, since the framework
doesn’t then need to rebuild this child when those change.
overlay_builder: RawMenuAnchorOverlayBuilderCalled to build and position the menu overlay.
The overlay_builder function is passed a
RawMenuOverlayInfo object that defines the anchor’s Rect, the Size of the
overlay, the TapRegion::group_id for the menu system, and the position Offset
passed to MenuController::open.
To ensure taps are properly consumed, the RawMenuOverlayInfo::tap_region_group_id
should be passed to a TapRegion widget that wraps the menu panel.
use_root_overlay: boolWhether the menu panel should be rendered in the root Overlay.
When true, the menu is mounted in the root overlay. Rendering the menu in the root overlay prevents the menu from being obscured by other widgets.
When false, the menu is rendered in the nearest ancestor Overlay.
Submenus will always use the same overlay as their top-level ancestor, so setting a
use_root_overlay value on a submenu will have no effect.
Defaults to false on overlay menus.
child_focus_node: Option<FocusNodeRef>The focus node attached to the widget that takes focus when the menu is opened or closed.
If not supplied, the anchor will not retain focus when the menu is opened.
consume_outside_taps: boolWhether a tap event that closes the menu will be permitted to continue on to the gesture arena.
If false, then tapping outside of a menu when the menu is open will both close the menu, and allow the tap to participate in the gesture arena.
If true, then it will only close the menu, and the tap event will be consumed.
Defaults to false.
controller: MenuControllerRefA MenuController that allows opening and closing of the menu from other widgets.
Implementations§
Source§impl RawMenuAnchor
impl RawMenuAnchor
Sourcepub fn new(
controller: MenuControllerRef,
overlay_builder: impl Fn(&mut App, BuildContext, RawMenuOverlayInfo) -> WidgetRef + 'static,
) -> RawMenuAnchor
pub fn new( controller: MenuControllerRef, overlay_builder: impl Fn(&mut App, BuildContext, RawMenuOverlayInfo) -> WidgetRef + 'static, ) -> RawMenuAnchor
A RawMenuAnchor that delegates overlay construction to an
overlay_builder; Dart’s optional named arguments are the
setters.
Sourcepub fn key(self, key: KeyRef) -> RawMenuAnchor
pub fn key(self, key: KeyRef) -> RawMenuAnchor
Dart RawMenuAnchor(key:).
Sourcepub fn child_focus_node(self, child_focus_node: FocusNodeRef) -> RawMenuAnchor
pub fn child_focus_node(self, child_focus_node: FocusNodeRef) -> RawMenuAnchor
Dart RawMenuAnchor(childFocusNode:).
Sourcepub fn consume_outside_taps(self, consume_outside_taps: bool) -> RawMenuAnchor
pub fn consume_outside_taps(self, consume_outside_taps: bool) -> RawMenuAnchor
Dart RawMenuAnchor(consumeOutsideTaps:).
Sourcepub fn on_open(self, on_open: VoidCallback) -> RawMenuAnchor
pub fn on_open(self, on_open: VoidCallback) -> RawMenuAnchor
Dart RawMenuAnchor(onOpen:).
Sourcepub fn on_close(self, on_close: VoidCallback) -> RawMenuAnchor
pub fn on_close(self, on_close: VoidCallback) -> RawMenuAnchor
Dart RawMenuAnchor(onClose:).
Sourcepub fn on_open_requested(
self,
on_open_requested: RawMenuAnchorOpenRequestedCallback,
) -> RawMenuAnchor
pub fn on_open_requested( self, on_open_requested: RawMenuAnchorOpenRequestedCallback, ) -> RawMenuAnchor
Dart RawMenuAnchor(onOpenRequested:).
Sourcepub fn on_close_requested(
self,
on_close_requested: RawMenuAnchorCloseRequestedCallback,
) -> RawMenuAnchor
pub fn on_close_requested( self, on_close_requested: RawMenuAnchorCloseRequestedCallback, ) -> RawMenuAnchor
Dart RawMenuAnchor(onCloseRequested:).
Sourcepub fn use_root_overlay(self, use_root_overlay: bool) -> RawMenuAnchor
pub fn use_root_overlay(self, use_root_overlay: bool) -> RawMenuAnchor
Dart RawMenuAnchor(useRootOverlay:).
Sourcepub fn builder(
self,
builder: impl Fn(&mut App, BuildContext, AnyMenuController, Option<&WidgetRef>) -> WidgetRef + 'static,
) -> RawMenuAnchor
pub fn builder( self, builder: impl Fn(&mut App, BuildContext, AnyMenuController, Option<&WidgetRef>) -> WidgetRef + 'static, ) -> RawMenuAnchor
Dart RawMenuAnchor(builder:).
Sourcepub fn child<K>(self, child: impl IntoWidget<K>) -> RawMenuAnchor
pub fn child<K>(self, child: impl IntoWidget<K>) -> RawMenuAnchor
Dart RawMenuAnchor(child:).
Trait Implementations§
Source§impl Debug for RawMenuAnchor
impl Debug for RawMenuAnchor
Source§impl StatefulWidget for RawMenuAnchor
impl StatefulWidget for RawMenuAnchor
Source§type State = RawMenuAnchorState
type State = RawMenuAnchorState
Source§fn debug_fill_properties(
&self,
_app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )
debugFillProperties: the properties a dump shows for this widget, in Dart’s
order. Dart’s Widget base only sets the dense tree style, which the framework does.Source§fn create_state(&self) -> RawMenuAnchorState
fn create_state(&self) -> RawMenuAnchorState
Source§fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
Widget::type_name. A stateful form another widget kind becomes (an animated or
status transition widget) names the widget it wraps.Auto Trait Implementations§
impl !RefUnwindSafe for RawMenuAnchor
impl !Send for RawMenuAnchor
impl !Sync for RawMenuAnchor
impl !UnwindSafe for RawMenuAnchor
impl Forget for RawMenuAnchor
impl Freeze for RawMenuAnchorwhere
*const RcInner<dyn Key> is !null: Freeze,
PhantomData<RcInner<dyn Key>>: Freeze,
*const RcInner<dyn Fn(&mut App)> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App)>>: Freeze,
*const RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>>: Freeze,
*const RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>>: Freeze,
*const RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: Freeze,
*const RcInner<dyn Widget> is !null: Freeze,
PhantomData<RcInner<dyn Widget>>: Freeze,
*const RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>>: Freeze,
NonZero<u32>: Freeze,
&'static FocusNodeVTable: Freeze,
*const RcInner<Keep> is !null: Freeze,
PhantomData<RcInner<Keep>>: Freeze,
&'static MenuControllerVTable: Freeze,
impl Move for RawMenuAnchor
impl Unpin for RawMenuAnchorwhere
Rc<dyn Key>: Unpin,
Rc<dyn Fn(&mut App)>: Unpin,
Rc<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>: Unpin,
Rc<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>: Unpin,
Rc<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>: Unpin,
Rc<dyn Widget>: Unpin,
Rc<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>: Unpin,
NonZero<u32>: Unpin,
&'static FocusNodeVTable: Unpin,
Rc<Keep>: Unpin,
&'static MenuControllerVTable: Unpin,
impl UnsafeUnpin for RawMenuAnchorwhere
*const RcInner<dyn Key> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Key>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App)> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App)>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: UnsafeUnpin,
*const RcInner<dyn Widget> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Widget>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>>: UnsafeUnpin,
NonZeroU32Inner: UnsafeUnpin,
&'static FocusNodeVTable: UnsafeUnpin,
*const RcInner<Keep> is !null: UnsafeUnpin,
PhantomData<RcInner<Keep>>: UnsafeUnpin,
&'static MenuControllerVTable: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.