Flutter模态底部弹窗内Google Map手势失效问题求助
解决Flutter模态底部弹窗中Google Map手势失效问题
你的问题核心在于两个点:外层GestureDetector拦截了地图的手势事件,同时GoogleMap的gestureRecognizers配置不全,缺少平移手势的识别器。以下是具体解决方法:
1. 移除或调整外层GestureDetector
当前外层的GestureDetector会优先拦截所有手势,导致地图无法接收捏合缩放和平移事件。如果需要点击空白处收起键盘,可将收起逻辑移至地图的onTap回调中,直接移除外层GestureDetector:
2. 完善GoogleMap的手势识别器配置
在gestureRecognizers中添加平移所需的PanGestureRecognizer,确保地图能响应所有需要的手势。
修改后的完整代码
void showLocationSelection( BuildContext context, ThemeData theme, CommonColorsExt? commonColors, MerchantQuestionsViewModel viewModel, ) { showModalBottomSheet( context: context, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(16.0), ), ), isScrollControlled: true, builder: (BuildContext context) { return Container( constraints: BoxConstraints( maxHeight: MediaQuery.of(context).size.height * 0.9, ), child: Column( children: [ Expanded( child: GoogleMap( initialCameraPosition: CameraPosition( target: LatLng(6.91177, 79.85043), zoom: 15.0, ), onTap: (LatLng position) { viewModel.setGeoLocation(position); FocusScope.of(context).unfocus(); // 在这里处理收起键盘 }, markers: viewModel.markers, mapType: MapType.normal, gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{ Factory<ScaleGestureRecognizer>(() => ScaleGestureRecognizer()), Factory<PanGestureRecognizer>(() => PanGestureRecognizer()), }, zoomGesturesEnabled: true, scrollGesturesEnabled: true, myLocationButtonEnabled: false, myLocationEnabled: false, ), ), ], ), ); }, ); }
若需保留外层GestureDetector的情况
如果弹窗顶部有其他区域需要点击收起键盘,可设置GestureDetector的behavior为HitTestBehavior.translucent,让它仅处理未被子组件拦截的手势:
return GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { FocusScope.of(context).unfocus(); }, child: Container( constraints: BoxConstraints( maxHeight: MediaQuery.of(context).size.height * 0.9, ), child: Column( children: [ // 顶部操作栏示例 Container( padding: EdgeInsets.symmetric(vertical: 16), child: Text("选择位置"), ), Expanded( child: GoogleMap( // 地图属性配置不变,确保gestureRecognizers包含Scale和Pan识别器 ), ), ], ), ), );
内容的提问来源于stack exchange,提问作者Dasaya_Developer
相关产品推荐
相关产品推荐

