【发布时间】:2019-05-16 22:36:48
【问题描述】:
我只是在玩新的Google Map Flutter 包。
我想使用PopupMenuButton 更改地图的MapType,如混合、卫星、无等。下面是我的代码。
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打了电话,但可以工作。
【问题讨论】:
-
你得到解决方案了吗?
标签: google-maps dart flutter