Skip to content

Commit

Permalink
Merge pull request #45 from TesteurManiak/fix/dispose-running-animations
Browse files Browse the repository at this point in the history
Fix: Stop and dispose all running animations
  • Loading branch information
TesteurManiak authored Nov 25, 2024
2 parents c0d00c4 + 5068c8e commit 1689e4d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 22 deletions.
26 changes: 13 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.7.0"
version: "0.7.1"
flutter_map_cancellable_tile_provider:
dependency: "direct main"
description:
Expand Down Expand Up @@ -142,18 +142,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -198,18 +198,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
mgrs_dart:
dependency: transitive
description:
Expand Down Expand Up @@ -291,10 +291,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -323,10 +323,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.5"
web:
dependency: transitive
description:
Expand Down
80 changes: 71 additions & 9 deletions lib/src/animated_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AnimatedMapController {
MapController? mapController,
this.duration = const Duration(milliseconds: 500),
this.curve = Curves.fastOutSlowIn,
this.cancelPreviousAnimations = false,
}) : mapController = mapController ?? MapController(),
_internal = mapController == null;

Expand All @@ -44,11 +45,21 @@ class AnimatedMapController {
final bool _internal;

/// The duration of the animation.
///
/// Defaults to 500 milliseconds.
final Duration duration;

/// The curve of the animation.
///
/// Defaults to [Curves.fastOutSlowIn].
final Curve curve;

/// Global option to define if previous animations should be canceled when a
/// new one is triggered.
///
/// Defaults to `false`.
final bool cancelPreviousAnimations;

/// Current rotation of the map.
double get rotation {
double effectiveRotation = mapController.camera.rotation;
Expand All @@ -60,15 +71,11 @@ class AnimatedMapController {
return effectiveRotation;
}

/// Controller of the current animation.
AnimationController? _animationController;
final _runningAnimations = <AnimationController>{};

void dispose() {
final isAnimating = _animationController?.isAnimating ?? false;
if (isAnimating) {
_animationController?.stop();
}
_animationController?.dispose();
// Stop running animations and dispose their controllers.
stopAnimations();

// Dispose the map controller if it was created internally.
if (_internal) {
Expand All @@ -87,6 +94,15 @@ class AnimatedMapController {
/// {@template animated_map_controller.animate_to.curve}
/// If [curve] is not specified, the one specified in the constructor will be
/// used.
///
/// Defaults to the value specified in the constructor.
/// {@endtemplate}
///
/// {@template animated_map_controller.animate_to_cancel_previous_animations}
/// If [cancelPreviousAnimations] is `true`, all ongoing animations will be
/// stopped before starting the new one.
///
/// Defaults to the value specified in the constructor.
/// {@endtemplate}
Future<void> animateTo({
LatLng? dest,
Expand All @@ -96,6 +112,7 @@ class AnimatedMapController {
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
if (zoom != null && zoom < 0) {
throw ArgumentError.value(
Expand Down Expand Up @@ -140,19 +157,24 @@ class AnimatedMapController {
_movementCallback(hasMovement: hasMovement, hasRotation: hasRotation);
if (movementCallback == null) return Future.value();

// If cancelPreviousAnimations is true, stop all ongoing animations.
if (cancelPreviousAnimations ?? this.cancelPreviousAnimations) {
stopAnimations();
}

// This controller will be disposed when the animation is completed.
final animationController = AnimationController(
vsync: vsync,
duration: duration ?? this.duration,
);
_animationController = animationController;
_runningAnimations.add(animationController);

final animation = CurvedAnimation(
parent: animationController,
curve: curve ?? this.curve,
)..onEnd(() {
animationController.dispose();
_animationController = null;
_runningAnimations.remove(animationController);
});

AnimationId animationId = AnimationId(
Expand Down Expand Up @@ -253,47 +275,58 @@ class AnimatedMapController {
/// Center the map on [point] with an optional [zoom] level.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> centerOnPoint(
LatLng point, {
double? zoom,
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
dest: point,
zoom: zoom,
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

/// Apply a rotation of [degree] to the current rotation.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedRotateFrom(
double degree, {
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
rotation: rotation + degree,
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

/// Set the rotation to [degree].
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedRotateTo(
double degree, {
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
rotation: degree,
Expand All @@ -306,32 +339,40 @@ class AnimatedMapController {
/// Reset the rotation to 0.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedRotateReset({
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
rotation: 0,
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

/// Add one level to the current zoom level.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedZoomIn({
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
zoom: mapController.camera.zoom + 1,
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

Expand All @@ -340,10 +381,13 @@ class AnimatedMapController {
/// If the current zoom level is 0, nothing will happen.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedZoomOut({
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) async {
final newZoom = mapController.camera.zoom - 1;
if (newZoom < 0) return;
Expand All @@ -353,6 +397,7 @@ class AnimatedMapController {
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

Expand All @@ -361,30 +406,37 @@ class AnimatedMapController {
/// [newZoom] must be greater or equal to 0.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedZoomTo(
double newZoom, {
Curve? curve,
String? customId,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
return animateTo(
zoom: newZoom,
curve: curve,
customId: customId,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

/// Will use the [cameraFit] to calculate the center and zoom level and then
/// animate to that position.
///
/// {@macro animated_map_controller.animate_to.curve}
///
/// {@macro animated_map_controller.animate_to_cancel_previous_animations}
Future<void> animatedFitCamera({
required CameraFit cameraFit,
Curve? curve,
String? customId,
double? rotation,
Duration? duration,
bool? cancelPreviousAnimations,
}) {
MapCamera camera = mapController.camera;
if (rotation != null) {
Expand All @@ -400,6 +452,7 @@ class AnimatedMapController {
customId: customId,
rotation: rotation,
duration: duration,
cancelPreviousAnimations: cancelPreviousAnimations,
);
}

Expand Down Expand Up @@ -429,6 +482,15 @@ class AnimatedMapController {
customId: customId,
);
}

/// Stop all ongoing animations.
void stopAnimations() {
for (final animation in _runningAnimations) {
if (animation.isAnimating) animation.stop();
animation.dispose();
}
_runningAnimations.clear();
}
}

class _AngleTween extends Tween<double> {
Expand Down

0 comments on commit 1689e4d

Please sign in to comment.