Flutter中PopupMenuButton展开状态下如何滚动页面
问题本质
PopupMenuButton弹出的菜单本身是通过showMenu方法挂载到全局Overlay层的,路由自带一个全屏透明的ModalBarrier遮罩:
- 为了实现「点击任意菜单外区域关闭弹窗」的默认交互,这个遮罩会在手势竞争中优先赢过下层的滚动组件
- 哪怕遮罩本身是完全透明的,落在遮罩范围内的滑动/滚动手势也会被直接消费,无法传递到下层的可滚动页面
这就是为什么弹窗展开时,不先关闭弹窗就完全没法滚动页面。
可实现的两种解决方案
方案1:替换默认弹出逻辑,自定义可穿透滚动的菜单路由
不需要改动原有菜单项的业务逻辑,只需要把PopupMenuButton默认调用的showMenu逻辑,替换为自定义的PopupRoute,调整遮罩的手势识别规则,让遮罩只响应「点击关闭」的动作,滚动类手势直接穿透到下层页面:
- 先定义自定义的菜单路由,替换默认的全手势拦截遮罩:
class ScrollPassThroughPopupRoute<T> extends PopupRoute<T> { ScrollPassThroughPopupRoute({ required this.position, required this.itemBuilder, required this.barrierLabel, }); final RelativeRect position; final PopupMenuBuilder<T> itemBuilder; @override Color? get barrierColor => null; @override bool get barrierDismissible => true; @override final String barrierLabel; @override Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) { return CustomSingleChildLayout( delegate: _PopupMenuLayout(position: position), child: Material( elevation: 8, child: ListView( shrinkWrap: true, children: itemBuilder(context), ), ), ); } // 自定义遮罩:仅识别点击手势,不拦截滚动 @override Widget buildModalBarrier() { return GestureDetector( onTap: () => navigator?.pop(), behavior: HitTestBehavior.translucent, ); } } // 菜单位置计算代理,和原生PopupMenu的位置逻辑保持一致 class _PopupMenuLayout extends SingleChildLayoutDelegate { _PopupMenuLayout({required this.position}); final RelativeRect position; @override BoxConstraints getConstraintsForChild(BoxConstraints constraints) { return BoxConstraints.loose( constraints.biggest - const EdgeInsets.all(8), ); } @override Offset getPositionForChild(Size size, Size childSize) { return Offset( position.left - position.right, position.top - position.bottom, ); } @override bool shouldRelayout(_PopupMenuLayout oldDelegate) => position != oldDelegate.position; }
- 替换原有PopupMenuButton的触发逻辑,手动调用自定义路由:
GestureDetector( onTap: () { final renderBox = context.findRenderObject() as RenderBox; final overlay = Overlay.of(context).context.findRenderObject() as RenderBox; final targetRect = RelativeRect.fromRect( Rect.fromPoints( renderBox.localToGlobal(Offset.zero, ancestor: overlay), renderBox.localToGlobal(renderBox.size.bottomRight(Offset.zero), ancestor: overlay), ), Offset.zero & overlay.size, ); Navigator.of(context).push( ScrollPassThroughPopupRoute<int>( position: targetRect, itemBuilder: (context) => [ PopupMenuItem( value: 1, child: Row( children: [ Icon(Icons.person, color: Colors.black), SizedBox(width: 12), Text("xxx"), ], ), ), PopupMenuItem( value: 2, child: Row( children: [ Icon(Icons.settings, color: Colors.black), SizedBox(width: 12), Text("xxx"), ], ), ), ], barrierLabel: MaterialLocalizations.of(context).popupMenuLabel, ), ); }, // 完全复用原有PopupMenuButton的UI结构 child: Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end, children: [ Container( padding: EdgeInsets.all(2), decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), border: Border.all(width: 2, color: Colors.black)), child: Icon( Icons.person, color: Colors.black, size: 30, ), ), Icon( Icons.keyboard_arrow_down, color: Colors.black, ), ], ), )
方案效果:菜单展开时,点击菜单外的区域会正常关闭菜单,而在菜单外的页面区域滑动时,滚动事件会直接穿透到下层的滚动组件,页面可以正常滚动,不需要手动关闭菜单。
方案2:使用官方MenuAnchor组件替代原生PopupMenuButton
如果项目使用的Flutter版本在3.7及以上,官方推出的MenuAnchor组件原生支持「菜单展开时允许下层交互」的能力,不需要自己维护自定义路由的代码,长期兼容性更好:
MenuAnchor( builder: (context, controller, child) { return GestureDetector( onTap: controller.open, // 复用原有UI结构,不需要改动视觉效果 child: Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end, children: [ Container( padding: EdgeInsets.all(2), decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), border: Border.all(width: 2, color: Colors.black)), child: Icon( Icons.person, color: Colors.black, size: 30, ), ), Icon( Icons.keyboard_arrow_down, color: Colors.black, ), ], ), ); }, menuChildren: [ MenuItemButton( onPressed: () {}, child: Row( children: [ Icon(Icons.person, color: Colors.black), SizedBox(width: 12), Text("xxx"), ], ), ), MenuItemButton( onPressed: () {}, child: Row( children: [ Icon(Icons.settings, color: Colors.black), SizedBox(width: 12), Text("xxx"), ], ), ), ], )
内容的提问来源于stack exchange,提问作者wahyu
相关产品推荐
相关产品推荐

