Skip to content

Commit

Permalink
Fix MenuAnchor closes on internal scroll (#122696)
Browse files Browse the repository at this point in the history
Fix MenuAnchor closes on internal scroll
  • Loading branch information
bleroux authored Mar 15, 2023
1 parent 67e5f66 commit 5e54139
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/material/menu_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,10 @@ class _MenuAnchorState extends State<MenuAnchor> {
}

void _handleScroll() {
// If an ancestor scrolls, and we're a top level or root anchor, then close
// the menus. Don't just close it on *any* scroll, since we want to be able
// to scroll menus themselves if they're too big for the view.
if (_isTopLevel || _isRoot) {
// If an ancestor scrolls, and we're a root anchor, then close the menus.
// Don't just close it on *any* scroll, since we want to be able to scroll
// menus themselves if they're too big for the view.
if (_isRoot) {
_root._close();
}
}
Expand Down
72 changes: 72 additions & 0 deletions packages/flutter/test/material/menu_anchor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,78 @@ void main() {
expect(closed, isNotEmpty);
});

testWidgets('menus do not close on root menu internal scroll', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/122168.
final ScrollController scrollController = ScrollController();
bool rootOpened = false;

await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
menuButtonTheme: MenuButtonThemeData(
// Increase menu items height to make root menu scrollable.
style: TextButton.styleFrom(minimumSize: const Size.fromHeight(200)),
),
),
home: Material(
child: SingleChildScrollView(
controller: scrollController,
child: Container(
height: 1000,
alignment: Alignment.topLeft,
child: MenuAnchor(
controller: controller,
alignmentOffset: const Offset(0, 10),
builder: (BuildContext context, MenuController controller, Widget? child) {
return FilledButton.tonal(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('Show menu'),
);
},
onOpen: () { rootOpened = true; },
onClose: () { rootOpened = false; },
menuChildren: createTestMenus(
onPressed: onPressed,
onOpen: onOpen,
onClose: onClose,
includeExtraGroups: true,
),
),
),
),
),
),
);

await tester.tap(find.text('Show menu'));
await tester.pump();
expect(rootOpened, true);

// Hover the first item.
final TestPointer pointer = TestPointer(1, PointerDeviceKind.mouse);
await tester.sendEventToBinding(pointer.hover(tester.getCenter(find.text(TestMenu.mainMenu0.label))));
await tester.pump();
expect(opened, isNotEmpty);

// Menus do not close on internal scroll.
await tester.sendEventToBinding(pointer.scroll(const Offset(0.0, 30.0)));
await tester.pump();
expect(rootOpened, true);
expect(closed, isEmpty);

// Menus close on external scroll.
scrollController.jumpTo(1000);
await tester.pump();
expect(rootOpened, false);
expect(closed, isNotEmpty);
});

testWidgets('menus close on view size change', (WidgetTester tester) async {
final ScrollController scrollController = ScrollController();
final MediaQueryData mediaQueryData = MediaQueryData.fromView(tester.view);
Expand Down

0 comments on commit 5e54139

Please sign in to comment.