google-maps - Flutter google_maps_flutter 无法通过 PopupMenuButton 更改 MapType
问题描述
我只是在玩新的 Flutter谷歌地图包。
我想MapType
通过使用PopupMenuButton
. 下面是我的代码。
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapPage extends StatefulWidget {
@override
_MapPageState createState() => _MapPageState();
}
class _MapPageState extends State<MapPage> {
GoogleMapController mapController;
List arrMapTyep = [
{'title': 'none', 'value': MapType.none},
{'title': 'hybrid', 'value': MapType.hybrid},
{'title': 'normal', 'value': MapType.normal},
{'title': 'satellite', 'value': MapType.satellite},
{'title': 'terrain', 'value': MapType.terrain}
];
MapType currentMapType = MapType.normal;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Google Map"),
actions: <Widget>[
PopupMenuButton(
icon: Icon(Icons.more_vert),
initialValue: currentMapType,
onSelected: (value) {
setState(() {
currentMapType = value;
});
},
itemBuilder: (BuildContext context) => _setupAllMapType()
)
],
),
body: Container(
child: GoogleMap(
onMapCreated: _onMapCreated,
options: GoogleMapOptions(
mapType: currentMapType,
compassEnabled: true,
myLocationEnabled: true,
cameraPosition: CameraPosition(
target: LatLng(37.785834, -122.406417),
zoom: 16,
)
),
),
),
);
}
void _onMapCreated(GoogleMapController controller) {
setState(() {
mapController = controller;
});
}
List<PopupMenuItem> _setupAllMapType() {
List<PopupMenuItem> temArr = List();
for (var i = 0; i < arrMapTyep.length; i++) {
temArr.add(
PopupMenuItem(
value: arrMapTyep[i]['value'],
child: Text(arrMapTyep[i]['title']),
),
);
}
return temArr;
}
}
我打电话setState
但可以工作。
解决方案
我不确定现在回答这个问题是否为时已晚,但万一像我这样的人偶然发现了这个问题;
MapType 的实时更改,可以使用 setState() 调用 currentMapType 变量来实现。我已经在我的应用程序中实现了这一点,它像黄油一样顺畅!
希望能帮助到你。快乐飘飘!
快速参考(将法线贴图类型更改为卫星贴图类型):
RaisedButton(
child: Text("Satellite"),
onPressed: () {
setState(() {
currentMapType = MapType.satellite;
});
},
)
推荐阅读
- javascript - 如何使用另一个对象访问 JavaScript 对象?
- php - 升级 Wordpress 5.0.3 后,Woocommerce 订单未全部显示
- python - 如何创建具有不同长度的数组系列的 DataFrame(某些部分中缺少数据)
- docker - 读取外部容器 Docker
- python - 从矩阵中获取轴
- node.js - 我自己的 TypeScript 模块的“找不到模块”
- swift - 将 github 存储库部署到 heoku 时遇到问题
- c# - 使用另一个应用程序实现 UI 自动化
- android - Firebase 远程配置获取缓存差异 16.3.0 与 16.0.0
- html - 如何使所有分辨率的右侧边距相等?