Skip to content

[BUG] MapOptions.keepAlive has no effect when changing screens #2013

@Errechydy

Description

@Errechydy

What is the bug?

MapOptions.keepAlive has no effect when changing screens, the map always get reset to the initial location when bottom navigation tabs changes, i don't know if keepAlive supposed to work in this case or not, if not then how can we make the map keep it's state in this case.
I also tried final GlobalKey myGlobalKey = GlobalKey(); and give it to the MyApp widget but none of that worked.

How can we reproduce it?

Run the following code, go to map screen, drag the map, go to another screen then go back to map screen it will reset to the initial state even thought keepAlive: true.

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    Center(child: Text('Home Page', style: TextStyle(fontSize: 24))),
    const MyMap(),
    Center(child: Text('Profile Page', style: TextStyle(fontSize: 24))),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bottom Navigation Bar'),
        backgroundColor: Colors.blue,
      ),
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.map_outlined),
            label: 'Map',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          ),
        ],
      ),
    );
  }
}

class MyMap extends StatelessWidget {
  const MyMap({super.key});

  @override
  Widget build(BuildContext context) {
    return FlutterMap(
      options: MapOptions(
        keepAlive: true,
      ),
      children: [
        TileLayer(
          urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
        ),
      ],
    );
  }
}

Do you have a potential solution?

Not really

Platforms

Android, Web

Severity

Obtrusive: Prevents normal functioning but causes no errors in the console

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue reports broken functionality or another errorneeds triageThis new bug report needs reproducing and prioritizing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions