0

I have a homePage in flutter with a flutter_google_map. The problem is when i change page (using go_router), and go again on the homePage, my map always reload and take 2 to 3 sec charging. I want to keep alive the map.

I tried with AutomaticKeepAliveClientMixin no success. Any ideas ?

Video exemple

Below the home Page and the Google Map Component:

class _HomeTaxisPageWidgetState extends State<HomeTaxisPageWidget>
    with TickerProviderStateMixin {
  late HomeTaxisPageModel _model;
  final scaffoldKey = GlobalKey<ScaffoldState>();
  final animationsMap = <String, AnimationInfo>{};
  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => HomeTaxisPageModel());
  }
  @override
  void dispose() {
    _model.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    context.watch<FFAppState>();
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),
      child: WillPopScope(
        onWillPop: () async => false,
        child: Scaffold(
          key: scaffoldKey,
          backgroundColor: FlutterFlowTheme.of(context).primaryAppBackground,
          ),
          body: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              // * TopStack
              Expanded(
                flex: 1,
                child: SizedBox(
                  height: double.infinity,
                  child: Stack(
                    children: [
                      // Google Map
                      const SizedBox(
                        width: double.infinity,
                        height: double.infinity,
                        child: HomeMapPageWidget(),
                      ),

...

[Google Map Widget]

class HomeMapPageWidgetState extends State<HomeMapPageWidget>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  @override
  bool get wantKeepAlive => true;

  final Completer<GoogleMapController> _controller =
      Completer<GoogleMapController>();

  BitmapDescriptor? icon;
  Set<Marker> markers = {};
  // bg.Location? _lastLocation;

  var carRotation = 90.0;
  late AnimationController _animationController;
  late Animation<double> _latAnimation;
  late Animation<double> _lngAnimation;

  @override
  void initState() {
    super.initState();
    getIcon();
    addMarker();
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    context.watch<FFAppState>();
    return Scaffold(
      body: GoogleMap(
        cloudMapId: 'd180e3ed1c3cabf6',
        initialCameraPosition: CameraPosition(
          target: currentPosition,
          zoom: 16,
        ),
        myLocationButtonEnabled: false,
        compassEnabled: false,
        mapToolbarEnabled: false,
        trafficEnabled: false,
        markers: markers,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
    );
  }


0

Browse other questions tagged or ask your own question.