From 260bee4d08ba59aa96abcaaa226167f2f09d8f72 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Tue, 22 Oct 2024 20:49:15 +0200 Subject: [PATCH] Fix MenuController.isOpen throws when unattached (#157331) ## Description This PR change `MenuController.isOpen` to return false when the `MenuController` is not attached. Previously `MenuController.isOpen` thrown an assert error when the menu controller was unattached. With this PR, it is now possible to have some widgets outside the `MenuAnchor` body depending on the menu controller state. ## Related Issue Fixes [Querying isOpen state for a menu controller that was never attached throws an assertion error](https://github.com/flutter/flutter/issues/157329). ## Tests Adds 1 test. --- packages/flutter/lib/src/material/menu_anchor.dart | 3 +-- packages/flutter/test/material/menu_anchor_test.dart | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index 3ce62dbe287..2f8cee2d507 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -650,8 +650,7 @@ class MenuController { /// Whether or not the associated menu is currently open. bool get isOpen { - assert(_anchor != null); - return _anchor!._isOpen; + return _anchor?._isOpen ?? false; } /// Close the menu that this menu controller is associated with. diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index 96f66c2933e..e5ad19fa47a 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -4732,6 +4732,11 @@ void main() { await tester.pump(); expect(tester.takeException(), isNull); }); + + testWidgets('Unattached MenuController returns false when calling isOpen', (WidgetTester tester) async { + final MenuController controller = MenuController(); + expect(controller.isOpen, false); + }); } List createTestMenus({