maplibre / flutter-maplibre-gl

Customizable, performant and vendor-free vector and raster maps, flutter wrapper for maplibre-native and maplibre-gl-js (fork of flutter-mapbox-gl/maps)
https://pub.dev/packages/maplibre_gl
Other
186 stars 106 forks source link

[BUG] Unexpected reset of the attributionButtonPosition after setState #417

Open Linker-123 opened 1 month ago

Linker-123 commented 1 month ago

Platforms

web

Version of flutter maplibre_gl

0.18.0

Bug Description

The attributionButtonPosition parameter in the options of the map is ignored when calling setState on the widget, the parameter is specified as following:

Scaffold(

    ....

    body: MaplibreMap(
        ....
        attributionButtonPosition: AttributionButtonPosition.TopLeft,
        ....
    )

);

The full project to reproduce the issue can be seen on the following repository: https://github.com/Linker-123/maplibre-attribution-issue

Steps to Reproduce

Press the floating action button in the code sample - the attribution button will move to the bottom right corner, instead of staying at the TopLeft position.

Expected Results

Expected behavior: attributionButtonPosition == AttributionButtonPosition.TopLeft

Actual Results

After calling setState() for the whole Scaffold page, attributionButtonPosition in MaplibreMap() object becomes equal default value, e.i. attributionButtonPosition==AttributionButtonPosition.BottomRight.

Code Sample

import 'package:flutter/material.dart';

import 'package:maplibre_gl/maplibre_gl.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MaplibreMapController? mapController;
  final double _targetLat = 40.730610;
  final double _targetLon = -73.935242;

  bool _tilt = false;
  final double _zoom = 15;

  void _onMapCreated(MaplibreMapController controller) {
    mapController = controller;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MaplibreMap(
        onMapCreated: _onMapCreated,
        initialCameraPosition: CameraPosition(target: LatLng(_targetLat, _targetLon), zoom: _zoom),
        attributionButtonPosition: AttributionButtonPosition.TopLeft,
        myLocationEnabled: false,
        compassEnabled: false,
        zoomGesturesEnabled: _tilt,
        styleString: "assets/osm_style.json",
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          setState(() {
            _tilt = !_tilt;
          });
          await mapController!.animateCamera(
            CameraUpdate.newCameraPosition(CameraPosition(
              target: LatLng(_targetLat, _targetLon),
              zoom: _zoom,
              tilt: 75.0,
            )),
            duration: const Duration(milliseconds: 500),
          );
        },
        child: const Icon(Icons.navigation),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}