Flutter_map中Marker添加GestureDetector报错:缺少child及builder未定义
问题解决:flutter_map Marker添加GestureDetector报错
报错原因分析
builder参数未定义:flutter_map的Marker组件并没有builder这个参数,标记的内容需要通过child参数传入。- GestureDetector语法错误:代码中
onTap回调后的逗号位置错误,导致child没有被正确识别为GestureDetector的必填参数。
修正后的代码
MarkerLayer( markers: [ Marker( point: LatLng(-22.3285, -49.0523), width: 65, height: 50, // 替换builder为child child: GestureDetector( onTap: () { // 这里添加点击逻辑 }, // 修正逗号位置,将child作为GestureDetector的属性 child: Container( padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2), decoration: BoxDecoration( color: Colors.black.withOpacity(0.5), borderRadius: BorderRadius.circular(5), ), child: Text( 'BLOCOS C, D & E', style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), ), ), ), ], ),
关键修改点
- 将
Marker的builder替换为官方支持的child参数。 - 调整
GestureDetector中onTap后的逗号位置,确保child被正确传递给GestureDetector,满足其必填参数要求。
内容的提问来源于stack exchange,提问作者Raphael Michelassi
相关产品推荐
相关产品推荐

