Flutter Google Maps不必要多次重建父窗口小部件

时间:2019-11-14 16:30:19

标签: google-maps flutter flutter-dependencies

我在“父级小部件”中有Google Maps小部件, 每次Google Maps Widget更新时,父Widget都会重建, 在Navigator.push上,谷歌地图小部件导致父小部件重建3次。 当我从父小部件中删除google maps小部件时,问题就解决了!

这是我的父小部件代码:

class AptInfo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var data = MediaQuery.of(context);
    print('arpts page rebuilt');
    return SafeArea(
        child: Scaffold(
            backgroundColor: Colors.black,
            body: Row(children: <Widget>[
              Flexible(
                child: MenuButts('ARPTS'),
              ),
              Align(
                  alignment: Alignment.center,
                  child: Container(
                      height: 90 * SizeConfig.heightMultiplier,
                      width: 85 * SizeConfig.widthMultiplier,
                      child: Column(
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Column(
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.only(top: 15),
                                    alignment: Alignment.center,
                                    child: Text(
                                      'Search Airports : ',
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize:
                                              2.2 * SizeConfig.textMultiplier,
                                          fontWeight: FontWeight.bold),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        top: 15, bottom: 5),
                                    child: Container(
                                        alignment: Alignment.center,
                                        child: AutoCompleteAp()),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                        width: data.size.width - 90,
                                        height: data.size.height / 3,
                                        alignment: Alignment.center,
                                        child: MapSample()),
                                  ),
                                ],
                              ),
                            ],
                          ),
                          Expanded(
                              child: Container(
                                  alignment: Alignment.center, child: Rwys())),
                        ],
                      ))),
            ])));
  }
}

这是Google Maps小部件的代码:


class MapSample extends StatefulWidget {
  @override
  State<MapSample> createState() => _MapSampleState();
}

class _MapSampleState extends State<MapSample> {
  GoogleMapController mapController2;

  Position _currentPosition;

  @override
  Widget build(BuildContext context) {
    final AirportsList airsList =
        Provider.of<AirportsList>(context, listen: false);
    final int myind = Provider.of<AirportsList>(context).selectedInd;
    if (myind != null) {
      var lat = airsList.airports[airsList.selectedInd].lat;
      var long = airsList.airports[airsList.selectedInd].long;

      if (mapController2 != null) {
        if (_currentPosition.latitude != lat) {
          mapController2.animateCamera(CameraUpdate.newCameraPosition(
              CameraPosition(target: LatLng(lat, long), zoom: 12)));
          print('$lat , $long');
        }
      }
      _currentPosition = Position(latitude: lat, longitude: long);
    }
    var data = MediaQuery.of(context);
    return myind != null
        ? GoogleMap(
            gestureRecognizers: Set()
              ..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer()))
              ..add(Factory<ScaleGestureRecognizer>(
                  () => ScaleGestureRecognizer()))
              ..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer()))
              ..add(Factory<VerticalDragGestureRecognizer>(
                  () => VerticalDragGestureRecognizer())),
            tiltGesturesEnabled: true,
            scrollGesturesEnabled: true,
            rotateGesturesEnabled: true,
            indoorViewEnabled: false,
            myLocationButtonEnabled: false,
            myLocationEnabled: false,
            mapType: MapType.satellite,
            initialCameraPosition: CameraPosition(
                target: LatLng(
                    _currentPosition.latitude, _currentPosition.longitude),
                zoom: 12),
            onMapCreated: (GoogleMapController controller) {
              mapController2 = controller;
            },
            markers: Set<Marker>.of(airsList.markers),
            zoomGesturesEnabled: true,
          )
        : Container();
  }
}

我几乎尝试了一切!仍然无法正常工作。

0 个答案:

没有答案