From 5ecbb1c7928edac17c5b608716cef4d3fc343b40 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 14 Apr 2022 17:19:45 +0100 Subject: [PATCH 1/2] fix for initial layoutBuilder sometimes not having a size, which causes bounds params and fitBounds to break. --- lib/src/map/flutter_map_state.dart | 23 +++++++++++++++++------ lib/src/map/map.dart | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/src/map/flutter_map_state.dart b/lib/src/map/flutter_map_state.dart index f6caf5a14..5787e1489 100644 --- a/lib/src/map/flutter_map_state.dart +++ b/lib/src/map/flutter_map_state.dart @@ -77,14 +77,25 @@ class FlutterMapState extends MapGestureMixin { Widget build(BuildContext context) { _disposeStreamGroups(); return LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { - mapState.setOriginalSize(constraints.maxWidth, constraints.maxHeight); - var size = mapState.size; + builder: (BuildContext context, BoxConstraints constraints) { - var scaleGestureTeam = GestureArenaTeam(); + var hasLateSize = mapState.hasLateSize(constraints); - var scaleGestureDetector = ({required Widget child}) => - RawGestureDetector( + mapState.setOriginalSize(constraints.maxWidth, constraints.maxHeight); + + // It's possible on first call to LayoutBuilder, it may not know a size + // which will cause methods like fitBounds to break. These methods + // could be called in initIfLateSize() + if(hasLateSize) { + mapState.initIfLateSize(); + } + var size = mapState.size; + + var scaleGestureTeam = GestureArenaTeam(); + + var scaleGestureDetector = ({required Widget child}) => + + RawGestureDetector( gestures: { ScaleGestureRecognizer: GestureRecognizerFactoryWithHandlers( diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index 6823f0470..65338df19 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -178,6 +178,24 @@ class MapState { } } + // Check if we've just got a new size constraints. Initially a layoutBuilder + // May not be able to calculate a size, and end up with 0,0 + bool hasLateSize(constraints) { + if(options.bounds != null && originalSize != null && + originalSize!.x == 0.0 && constraints.maxWidth != 0.0) { + return true; + } + return false; + } + + // If we've just calculated a size, we may want to call some methods that + // rely on it, like fitBounds. Add any others here. + void initIfLateSize() { + if(options.bounds != null) { + fitBounds(options.bounds!, options.boundsOptions); + } + } + void _handleMoveEmit(LatLng targetCenter, double targetZoom, hasGesture, MapEventSource source, String? id) { if (source == MapEventSource.flingAnimationController) { From 1ba3f5640fb1fe35db1f96ac99d84244109c7cb7 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 14 Apr 2022 17:23:58 +0100 Subject: [PATCH 2/2] dart format run --- lib/src/map/flutter_map_state.dart | 28 +++++++++++++--------------- lib/src/map/map.dart | 8 +++++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/src/map/flutter_map_state.dart b/lib/src/map/flutter_map_state.dart index 5787e1489..eb7740c7a 100644 --- a/lib/src/map/flutter_map_state.dart +++ b/lib/src/map/flutter_map_state.dart @@ -77,25 +77,23 @@ class FlutterMapState extends MapGestureMixin { Widget build(BuildContext context) { _disposeStreamGroups(); return LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { + builder: (BuildContext context, BoxConstraints constraints) { + var hasLateSize = mapState.hasLateSize(constraints); - var hasLateSize = mapState.hasLateSize(constraints); + mapState.setOriginalSize(constraints.maxWidth, constraints.maxHeight); - mapState.setOriginalSize(constraints.maxWidth, constraints.maxHeight); - - // It's possible on first call to LayoutBuilder, it may not know a size - // which will cause methods like fitBounds to break. These methods - // could be called in initIfLateSize() - if(hasLateSize) { - mapState.initIfLateSize(); - } - var size = mapState.size; - - var scaleGestureTeam = GestureArenaTeam(); + // It's possible on first call to LayoutBuilder, it may not know a size + // which will cause methods like fitBounds to break. These methods + // could be called in initIfLateSize() + if (hasLateSize) { + mapState.initIfLateSize(); + } + var size = mapState.size; - var scaleGestureDetector = ({required Widget child}) => + var scaleGestureTeam = GestureArenaTeam(); - RawGestureDetector( + var scaleGestureDetector = ({required Widget child}) => + RawGestureDetector( gestures: { ScaleGestureRecognizer: GestureRecognizerFactoryWithHandlers( diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index 65338df19..6a87d9bc5 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -181,8 +181,10 @@ class MapState { // Check if we've just got a new size constraints. Initially a layoutBuilder // May not be able to calculate a size, and end up with 0,0 bool hasLateSize(constraints) { - if(options.bounds != null && originalSize != null && - originalSize!.x == 0.0 && constraints.maxWidth != 0.0) { + if (options.bounds != null && + originalSize != null && + originalSize!.x == 0.0 && + constraints.maxWidth != 0.0) { return true; } return false; @@ -191,7 +193,7 @@ class MapState { // If we've just calculated a size, we may want to call some methods that // rely on it, like fitBounds. Add any others here. void initIfLateSize() { - if(options.bounds != null) { + if (options.bounds != null) { fitBounds(options.bounds!, options.boundsOptions); } }