Repository: flutter/flutter
File: packages/flutter/lib/src/material/menu_anchor.dart (Dart)

Code before:
  ///
  /// If there is no ancestor [MenuAnchor] widget in the tree, this will throw a
  /// [FlutterError].
  ///
  /// See also:
  ///
  ///  * [maybeOf], which is similar to this method, but returns null if
  ///    no [MenuAnchor] ancestor is found.
  static MenuController of(BuildContext context) {
    final MenuController? result = MenuAnchor.maybeOf(context);
    assert(() {
      if (result == null) {
        throw FlutterError(
          'MenuAnchor.of() was called with a context that does not contain a MenuAnchor widget.\n'
          'No MenuAnchor widget ancestor could be found starting from the context that was passed to '
          'MenuAnchor.of().\n'
          'The context used was:\n'
          '  $context',
        );
      }
      return true;
    }());
    return result!;
  }

  /// Returns the [MenuController] from the closest [MenuAnchor] ancestor,
  /// or null if there is no ancestor [MenuAnchor] widget in the tree.
  ///
  /// See also:
  ///
  ///  * [of], which is similar to this method, but throws if
  ///    no [MenuAnchor] ancestor is found.
  static MenuController? maybeOf(BuildContext context) {
    final _MenuAnchorState? result = _MenuAnchorState._maybeOf(context);
    return result?._menuController;
  }

  @override
  State<MenuAnchor> createState() => _MenuAnchorState();

  @override
  List<DiagnosticsNode> debugDescribeChildren() {
    return menuChildren.map<DiagnosticsNode>((Widget child) => child.toDiagnosticsNode()).toList();
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(
      FlagProperty('anchorTapClosesMenu', value: anchorTapClosesMenu, ifTrue: 'AUTO-CLOSE'),
    );

Review comment (suggestion):
```suggestion
  /// Returns the [MenuController] from the nearest [MenuAnchor] ancestor,
```

Code after:
  /// the [builder]. It will be more efficient, since Flutter doesn't then need
  /// to rebuild this child when those change.
  final Widget? child;

  @override
  State<MenuAnchor> createState() => _MenuAnchorState();

  @override
  List<DiagnosticsNode> debugDescribeChildren() {
    return menuChildren.map<DiagnosticsNode>((Widget child) => child.toDiagnosticsNode()).toList();
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(
      FlagProperty('anchorTapClosesMenu', value: anchorTapClosesMenu, ifTrue: 'AUTO-CLOSE'),
    );
    properties.add(DiagnosticsProperty<FocusNode?>('focusNode', childFocusNode));
    properties.add(DiagnosticsProperty<MenuStyle?>('style', style));
    properties.add(EnumProperty<Clip>('clipBehavior', clipBehavior));
    properties.add(DiagnosticsProperty<Offset?>('alignmentOffset', alignmentOffset));
  }
}

class _MenuAnchorState extends State<MenuAnchor> {
  Axis get _orientation => Axis.vertical;
  MenuController get _menuController => widget.controller ?? _internalMenuController!;
  MenuController? _internalMenuController;
  final FocusScopeNode _menuScopeNode = FocusScopeNode();
  _MenuAnchorState? get _parent => _MenuAnchorState._maybeOf(context);

  @override
  void initState() {
    super.initState();
    if (widget.controller == null) {
      _internalMenuController = MenuController();
    }
  }

  @override
  void didUpdateWidget(MenuAnchor oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.controller != widget.controller) {
      _internalMenuController = widget.controller != null ? MenuController() : null;
    }
  }

  @override
  void dispose() {
    assert(_debugMenuInfo('Disposing of $this'));