mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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.
This commit is contained in:
parent
e9fb545c05
commit
260bee4d08
@ -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.
|
||||
|
||||
@ -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<Widget> createTestMenus({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user