import 'package:flutter/material.dart'; /// The app's anchored menu: a [MenuAnchor] whose panel is in the root /// overlay, so it opens above every floating window and is never clipped to /// one, and which takes the keyboard while open, so Escape closes it and the /// arrow keys move through it wherever the keyboard was. Closing gives the /// keyboard back to what had it. /// /// Use it instead of [MenuAnchor] everywhere: any content can end up in a /// window, where only the root overlay reaches past the window's edges. class AppMenuAnchor extends StatefulWidget { const AppMenuAnchor({ super.key, required this.menuChildren, required this.builder, this.controller, this.style, this.alignmentOffset, this.child, }); final List menuChildren; final MenuAnchorChildBuilder builder; final MenuController? controller; final MenuStyle? style; final Offset? alignmentOffset; final Widget? child; @override State createState() => _AppMenuAnchorState(); } class _AppMenuAnchorState extends State { final FocusNode _focus = FocusNode( debugLabel: 'menu anchor', skipTraversal: true, ); FocusNode? _previous; @override void dispose() { _focus.dispose(); super.dispose(); } void _opened() { final current = FocusManager.instance.primaryFocus; _previous = identical(current, _focus) ? null : current; _focus.requestFocus(); } void _closed() { final previous = _previous; _previous = null; if (!_focus.hasPrimaryFocus) return; if (previous != null && previous.context != null && previous.canRequestFocus) { previous.requestFocus(); } else { _focus.unfocus(); } } @override Widget build(BuildContext context) => MenuAnchor( useRootOverlay: true, controller: widget.controller, style: widget.style, alignmentOffset: widget.alignmentOffset, childFocusNode: _focus, onOpen: _opened, onClose: _closed, menuChildren: widget.menuChildren, builder: (context, menu, child) => Focus( focusNode: _focus, includeSemantics: false, child: widget.builder(context, menu, child), ), child: widget.child, ); }