Skip to content

Map gets rebuild with every frame while navigating #768

@JohnnyRainbow81

Description

@JohnnyRainbow81

As far as I understand flutter, widgets should only rebuild when configuration changes happen.

Maybe this is intentional here because of some internal, non visible magic of the leaflet map implementation. In this case, just let me know if this is wanted. Otherwise - what did I do wrong? My code example shows mostly a c/p version of the official example from https://pub.dev/packages/flutter_map.

The problem is: Rebuilding the FlutterMap also means its children rebuild, in my case, pretty expensive marker widgets, which I didn’t include in the example code (because they re not necessary to prove my point). The performance is quite laggy, so here’s my question:

Is the way it works now intentional or can I avoid the permanent rebuilding or is it a bug in the leaflet framework?

Thank you for your help!

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: LeafletMap());
  }
}

class LeafletMap extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("LeafletMap.build()");
    return FlutterMap(
      options: MapOptions(
        center: LatLng(51.5, -0.09),
        zoom: 13.0,
      ),
      layers: [
        TileLayerOptions(
            urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", subdomains: ['a', 'b', 'c']),
        MarkerLayerOptions(
          markers: [
            Marker(
                width: 80.0, height: 80.0,
                point: new LatLng(51.5, -0.09),
                builder: (ctx) => MyWidget()),
          ],
        ),
      ],
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("MyWidget.build()");
    return FlutterLogo();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions