如何修复“在一个小部件的子列表中多次使用GlobalKey”

时间:2019-10-20 16:29:12

标签: google-maps flutter

因此,我正在YouTube上关注有关Google地图的本教程。我正确地遵循了它,但是当该运行该应用程序的时候,我收到一条错误消息“ GlobalKey使用了多次...”,我尝试添加一个 浮动动作按钮上的herotag,但     它什么也没做我遇到的第一个问题是     ``有多个英雄      在子树中共享相同的标签。”然后我尝试在新的flutter项目上运行它,然后出现这种错误

      Completer<GoogleMapController> _controller = Completer();
      static const LatLng _center = const LatLng(45.521563, -122.677433);
      final Set<Marker> _marker ={};
      LatLng _lastMapPosition = _center;
      MapType _currentMapType = MapType.normal;

      static final CameraPosition _position1 = CameraPosition(
        bearing: 192.833,
        target:  LatLng(45.521563, -122.677433),
        tilt: 59.440,
        zoom: 11.0
      );

      Future<void> _goToPosition1() async {
        final GoogleMapController controller = await _controller.future;
        controller.animateCamera((CameraUpdate.newCameraPosition(_position1)));
      }

      _onMapCreated(GoogleMapController controller){
        _controller.complete(controller);
      }
      _onCameraMove(CameraPosition position){
        _lastMapPosition = position.target;
      }

      _onMapTypeButtonPressed(){
        setState(() {
          _currentMapType = _currentMapType == MapType.normal ?
          MapType.satellite:
          MapType.normal;
        });
      }

      _onAddMarkerButtonPressed() {
        setState(() {
          _marker.add(Marker(
            markerId: MarkerId(_lastMapPosition.toString()),
            position: _lastMapPosition,
            infoWindow: InfoWindow(
                title: 'sample',
                snippet: 'sample'
            ),
            icon: BitmapDescriptor.defaultMarker,
          ));
        });
      }

      Widget button(Function function, IconData icon){
        return FloatingActionButton(
          onPressed: function,
          materialTapTargetSize: MaterialTapTargetSize.padded,
          backgroundColor: Color(0xffff9966),
          child: Icon(icon, size: 36.0,),
        );
      }
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Stack(
              children: <Widget>[
                GoogleMap(
                  onMapCreated: _onMapCreated,
                  initialCameraPosition: CameraPosition(
                    target: _center,
                    zoom: 11.0,
                  ),
                  mapType: _currentMapType,
                  markers: _marker,
                  onCameraMove: _onCameraMove,
                ),
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Align(
                    alignment: Alignment.topRight,
                    child: Column(
                      children: <Widget>[
                        button(_onMapTypeButtonPressed, Icons.map),
                        SizedBox(height: 16.0,),
                        button(_onAddMarkerButtonPressed, Icons.add_location),
                        SizedBox(height: 16.0,),
                        button(_goToPosition1, Icons.location_searching)
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );

0 个答案:

没有答案
相关问题