From 4276a91cf1c81e9ebc1a7bba86eae61a2b94cee7 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 14 Oct 2022 07:08:58 +0100 Subject: [PATCH] fix for flickering with fitbounds, moving calculations inside the layoutbuilder (#1376) --- lib/src/map/flutter_map_state.dart | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/src/map/flutter_map_state.dart b/lib/src/map/flutter_map_state.dart index 58a317cf7..09525a16c 100644 --- a/lib/src/map/flutter_map_state.dart +++ b/lib/src/map/flutter_map_state.dart @@ -17,6 +17,8 @@ class FlutterMapState extends MapGestureMixin final MapController _localController = MapControllerImpl(); + bool _hasFitInitialBounds = false; + @override MapOptions get options => widget.options; @@ -40,13 +42,7 @@ class FlutterMapState extends MapGestureMixin _pixelBounds = getPixelBounds(zoom); _bounds = _calculateBounds(); - move(options.center, zoom, source: MapEventSource.initialization); - WidgetsBinding.instance.addPostFrameCallback((_) { - // Finally, fit the map to restrictions - if (options.bounds != null) { - fitBounds(options.bounds!, options.boundsOptions); - } options.onMapReady?.call(); }); } @@ -160,17 +156,22 @@ class FlutterMapState extends MapGestureMixin }, ); - //Update on state change - _pixelBounds = getPixelBounds(zoom); - _bounds = _calculateBounds(); - _pixelOrigin = getNewPixelOrigin(_center); return LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { //Update on layout change setSize(constraints.maxWidth, constraints.maxHeight); + + if (options.bounds != null && !_hasFitInitialBounds) { + final target = getBoundsCenterZoom(options.bounds!, options.boundsOptions); + _zoom = target.zoom; + _center = target.center; + _hasFitInitialBounds = true; + } + _pixelBounds = getPixelBounds(zoom); _bounds = _calculateBounds(); + _pixelOrigin = getNewPixelOrigin(_center); return MapStateInheritedWidget( mapState: this,