Flutter 如何实现谷歌地图分屏及底部信息面板点击/滑动显隐
Flutter 地图分屏面板交互实现方案
需求说明
正在开发新的Flutter项目,需要实现上半屏展示Google Map、下半屏放置带信息与操作项的面板,交互逻辑如下:
- 点击关闭按钮、向下滑动面板时,隐藏面板使地图全屏
- 支持点击按钮恢复初始分屏视图
当前已实现的静态页面代码如下:
body: Stack( children: [ GoogleMap( padding: EdgeInsets.only(bottom: bottomPaddingOfMap), mapType: MapType.normal, compassEnabled:true, myLocationButtonEnabled: true, myLocationEnabled: true, zoomGesturesEnabled: true, zoomControlsEnabled: true, initialCameraPosition: _kGooglePlex, onMapCreated: (GoogleMapController controller){ _controllerGoogleMap.complete(controller); newGoogleMapController = controller; setState(() { bottomPaddingOfMap = 300.0; }); locatePosition(); }, ), //extra hamburger Drawer Positioned( top: 45.0, left: 22.0, child: GestureDetector( onTap: (){ scaffoldKey.currentState.openDrawer(); }, child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(22.0), boxShadow: [ BoxShadow( color: Colors.black, blurRadius: 6.0, spreadRadius: 0.5, offset: Offset( 0.7, 0.7, ), ), ], ), child: CircleAvatar( backgroundColor: Colors.white, child: Icon(Icons.menu, color: Colors.black,), ), ), ), ), Positioned( left: 0.0, right: 0.0, bottom: 0.0, child: Container( height: 350.0, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(18.0), topRight: Radius.circular(18.0)), boxShadow: [ BoxShadow( color: Colors.black, blurRadius: 16.0, spreadRadius: 0.5, offset: Offset(0.7, 0.7), ) ] ), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 18.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Center( child: SizedBox( child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ RaisedButton.icon( icon: Icon(Icons.cake), label: Text("Close"), onPressed: (){ print("pulsaado"); }, ), ], ), ), ), SizedBox(height: 6.0,), Text("Hi there!", style: TextStyle(fontSize: 18.0),), Text("Where do you want to create an alert?", style: TextStyle(fontSize: 20.0, fontFamily: "Brand-Bold"),), SizedBox(height: 20.0,), Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(5.0), boxShadow: [ BoxShadow( color: Colors.black, blurRadius: 6.0, spreadRadius: 0.5, offset: Offset(0.7, 0.7), ), ], ), child: Padding( padding: const EdgeInsets.all(12.0), child: Row( children: [ Icon(Icons.search, color: Colors.blueAccent ), SizedBox(width: 10.0,), Text("Search Drop Off"), ], ), ), ), SizedBox(height: 24.0,), Row( children: [ Icon(Icons.home, color: Colors.grey,), SizedBox(width: 12.0,), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Add Home"), SizedBox(height: 4.0,), Text("Your living home address", style: TextStyle(color: Colors.black54, fontSize: 12.0),), ], ), ], ), SizedBox(height: 10.0,), DividerWidget(), SizedBox(height: 16.0,), Row( children: [ Icon(Icons.home, color: Colors.grey,), SizedBox(width: 12.0,), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Add Work"), SizedBox(height: 4.0,), Text("Your office address", style: TextStyle(color: Colors.black54, fontSize: 12.0),), ], ), ], ) ], ), ), ), ), ], ),
可行实现方案
方案1:使用 sliding_up_panel 插件(推荐)
该插件已封装好拖拽手势、展开/收起动画、状态回调逻辑,无需手动处理手势冲突,适配地图padding调整非常方便。
- 在
pubspec.yaml添加依赖:sliding_up_panel: ^2.0.0+1 - 定义全局变量:
double bottomPaddingOfMap = 300; PanelController panelController = PanelController(); - 替换原有Stack中的硬编码面板部分,修改后的body结构:
body: Stack( children: [ GoogleMap( // 原有GoogleMap配置完全保留,仅保留padding绑定bottomPaddingOfMap即可 ), // 原有汉堡按钮逻辑完全保留 SlidingUpPanel( controller: panelController, panel: 原有的面板内容Widget, // 把原来面板的Padding+Column部分抽成独立组件传入 minHeight: 350, // 初始分屏状态的面板高度 maxHeight: 350, // 仅需要收起/展开两档时和minHeight保持一致 snappedSize: 0, // 拖拽到底时面板完全收起 onPanelSlide: (position) { // 面板滑动时动态调整地图底部padding,避免面板遮挡地图内容 setState(() { bottomPaddingOfMap = MediaQuery.of(context).size.height * position; }); }, onPanelClosed: () { // 面板完全收起时地图padding清零,实现全屏 setState(() { bottomPaddingOfMap = 0; }); }, // 完全保留原有面板的样式配置 borderRadius: BorderRadius.only( topLeft: Radius.circular(18.0), topRight: Radius.circular(18.0), ), boxShadow: [ BoxShadow( color: Colors.black, blurRadius: 16.0, spreadRadius: 0.5, offset: Offset(0.7, 0.7), ) ], ), ], ) - 关闭按钮点击逻辑修改为:
ElevatedButton.icon( icon: Icon(Icons.cake), label: Text("Close"), onPressed: () { panelController.close(); }, ) - 恢复分屏的按钮直接调用
panelController.open()即可。
方案2:手动实现(无第三方依赖)
如果不想引入额外依赖,可通过AnimationController+GestureDetector手动实现核心逻辑:
- 定义动画相关变量:
late AnimationController _panelController; double _currentPanelHeight = 350; // 初始面板高度 final double _maxPanelHeight = 350; // 初始分屏高度 final double _minPanelHeight = 0; // 收起后的高度 - 初始化动画控制器:
@override void initState() { super.initState(); _panelController = AnimationController( vsync: this, duration: const Duration(milliseconds: 300), lowerBound: _minPanelHeight, upperBound: _maxPanelHeight, value: _maxPanelHeight, ); // 动画值变化时同步更新地图padding _panelController.addListener(() { setState(() { _currentPanelHeight = _panelController.value; bottomPaddingOfMap = _currentPanelHeight; }); }); } @override void dispose() { _panelController.dispose(); super.dispose(); } - 给原有面板添加拖拽手势监听:
Positioned( left: 0, right: 0, bottom: 0, child: GestureDetector( onVerticalDragUpdate: (details) { // 拖拽时实时更新面板高度 _panelController.value -= details.primaryDelta! / _maxPanelHeight; }, onVerticalDragEnd: (details) { // 根据拖拽速度和当前高度判断最终状态 if (details.velocity.pixelsPerSecond.dy > 200 || _currentPanelHeight < _maxPanelHeight/2) { // 快速下滑或面板高度不到初始值一半时自动收起 _panelController.animateTo(_minPanelHeight); } else { // 否则弹回初始分屏高度 _panelController.animateTo(_maxPanelHeight); } }, child: Container( height: _currentPanelHeight, // 原有面板的样式、内容完全保留 ), ), ) - 关闭按钮点击逻辑直接调用
_panelController.animateTo(_minPanelHeight),恢复分屏按钮调用_panelController.animateTo(_maxPanelHeight)即可。
注意事项
- 手动实现的手势仅在面板区域生效,不会和GoogleMap的地图滑动手势产生冲突
- 原有代码中使用的
RaisedButton已在Flutter 2.0版本废弃,可替换为官方推荐的ElevatedButton
内容的提问来源于stack exchange,提问作者mvasco
相关产品推荐
相关产品推荐

