Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial fit bounds #1216

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/src/map/flutter_map_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ class FlutterMapState extends MapGestureMixin {
_disposeStreamGroups();
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
var hasLateSize = mapState.hasLateSize(constraints);

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();
Expand Down
20 changes: 20 additions & 0 deletions lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ 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) {
Expand Down