Skip to content
New issue

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

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

Already on GitHub? # to your account

Cancel animations on map controller move events #1043

Merged
merged 5 commits into from
Feb 1, 2022
Merged
Changes from 1 commit
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
Next Next commit
Cancel animations on map controller move events
  • Loading branch information
Robbendebiene committed Sep 29, 2021
commit d4485e6f202ec6ab2c6de3f80bbd8bad4a5f2609
31 changes: 31 additions & 0 deletions lib/src/gestures/gestures.dart
Original file line number Diff line number Diff line change
@@ -86,6 +86,8 @@ abstract class MapGestureMixin extends State<FlutterMap>
late Animation _doubleTapZoomAnimation;
late Animation _doubleTapCenterAnimation;

StreamSubscription<MapEvent>? _mapControllerAnimationInterruption;

int _tapUpCounter = 0;
Timer? _doubleTapHoldMaxDelay;

@@ -94,6 +96,8 @@ abstract class MapGestureMixin extends State<FlutterMap>

MapState get mapState;

MapController get mapController;

MapOptions get options;

@override
@@ -225,6 +229,8 @@ abstract class MapGestureMixin extends State<FlutterMap>
if (_flingController.isAnimating) {
_flingController.stop();

_stopListeningForAnimationInterruptions();

mapState.emitMapEvent(
MapEventFlingAnimationEnd(
center: mapState.center, zoom: mapState.zoom, source: source),
@@ -236,6 +242,8 @@ abstract class MapGestureMixin extends State<FlutterMap>
if (_doubleTapController.isAnimating) {
_doubleTapController.stop();

_stopListeningForAnimationInterruptions();

mapState.emitMapEvent(
MapEventDoubleTapZoomEnd(
center: mapState.center, zoom: mapState.zoom, source: source),
@@ -652,7 +660,9 @@ abstract class MapGestureMixin extends State<FlutterMap>
zoom: mapState.zoom,
source: MapEventSource.doubleTapZoomAnimationController),
);
_startListeningForAnimationInterruptions();
} else if (status == AnimationStatus.completed) {
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventDoubleTapZoomEnd(
center: mapState.center,
@@ -711,6 +721,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
void _flingAnimationStatusListener(AnimationStatus status) {
if (status == AnimationStatus.completed) {
_flingAnimationStarted = false;
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventFlingAnimationEnd(
center: mapState.center,
@@ -729,6 +740,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
zoom: mapState.zoom,
source: MapEventSource.flingAnimationController),
);
_startListeningForAnimationInterruptions();
}

var newCenterPoint = mapState.project(_mapCenterStart) +
@@ -743,6 +755,25 @@ abstract class MapGestureMixin extends State<FlutterMap>
);
}

void _startListeningForAnimationInterruptions() {
if (_mapControllerAnimationInterruption != null) return;
// cancel map animation controllers on map controller move events
_mapControllerAnimationInterruption = mapController.mapEventStream.where(
(event) => event.source == MapEventSource.mapController &&
event is MapEventMove
).listen(_handleAnimationInterruptions);
}

void _stopListeningForAnimationInterruptions() {
_mapControllerAnimationInterruption?.cancel();
_mapControllerAnimationInterruption = null;
}

void _handleAnimationInterruptions(MapEvent event) {
closeDoubleTapController(event.source);
closeFlingAnimationController(event.source);
}

CustomPoint _offsetToPoint(Offset offset) {
return CustomPoint(offset.dx, offset.dy);
}