Flutter google_maps_flutter не может изменить MapType с помощью PopupMenuButton - PullRequest
0 голосов
/ 16 декабря 2018

Я просто играю с новым Google Map пакетом 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, но могу сработать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...