Skip to content

MenuAnchor.builder does not update properly #157606

@nate-thegrate

Description

@nate-thegrate

The MenuAnchor's child property is documented as follows:

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 Flutter doesn't then need to rebuild this child when those change.


Expected Behavior

The builder updates each time the controller opens or closes.

Actual Behavior

The builder does not update when the controller opens, but does update when it closes.


Code Sample

DartPad link: https://dartpad.dev/?id=a0e913ed86a5cf11e223abda4ebd4535&channel=main

import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MenuAnchor(
            menuChildren: [
              MenuItemButton(child: Text('menu item')),
            ],
            builder: anchorBuilder,
          ),
        ),
      ),
    ),
  );
}

Widget anchorBuilder(
  BuildContext context,
  MenuController controller,
  Widget? child,
) {
  final bool isOpen = controller.isOpen;
  print(isOpen ? 'menu open!' : 'menu closed!');

  return FilledButton(
    onPressed: () {
      // Using controller.isOpen is necessary here, since
      // the value of isOpen does not update properly.
      controller.isOpen ? controller.close() : controller.open();
    },
    child: Text(isOpen ? 'close menu' : 'open menu'),
  );
}

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.24Found to occur in 3.24found in release: 3.27Found to occur in 3.27frameworkflutter/packages/flutter repository. See also f: labels.team-designOwned by Design Languages team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions