Flutter给Google Map添加搜索栏时出现位置参数过多问题求助
问题原因及修复方案
你出现这个错误是因为Scaffold组件只能接受指定的命名参数(比如appBar、body),你现在既设置了body: Column(...),又把GoogleMap直接作为Scaffold的子组件写在外面,相当于给Scaffold传了多余的位置参数,所以触发了"too many positional argument"错误。
修复后的代码
正确的做法是把GoogleMap放到Column的children列表里,并且用Expanded包裹它,这样地图才能占满搜索栏下方的剩余空间:
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Google Maps'), ), body: Column( children: [ Row( children: [ // 给TextFormField加Expanded避免占满整行挤压搜索按钮 Expanded( child: TextFormField( decoration: const InputDecoration( hintText: '搜索地点', contentPadding: EdgeInsets.symmetric(horizontal: 16), ), // 后续集成Places API时,在这里监听输入内容 onChanged: (value) { // 调用Google Places API获取搜索建议 }, ), ), IconButton(onPressed: () {}, icon: const Icon(Icons.search)) ], ), // 用Expanded让地图占满剩余垂直空间 Expanded( child: GoogleMap( mapType: MapType.normal, markers: _markers, initialCameraPosition: const CameraPosition(target: _kGooglePlex, zoom: 14), onMapCreated: _onMapCreated, ), ) ], ), ); }
后续集成Google Places API的简单思路
- 在
pubspec.yaml中添加google_maps_flutter和google_maps_webservice依赖 - 去Google Cloud Console申请API密钥,启用「Places API」和「Maps SDK for Android/iOS」
- 在TextFormField的
onChanged回调里,将输入内容传给Places API,获取地点建议列表 - 点击建议项时,通过GoogleMap控制器将相机移动到对应地点的经纬度,同时添加标记
内容的提问来源于stack exchange,提问作者Flutter Beginner
相关产品推荐
相关产品推荐

