Skip to content

feat: add autoDisposeImage param to automatically evict image from cache when object dispose #289

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions lib/src/composition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ class LottieComposition {
return roundedProgress;
}

void dispose() {
for (var image in images.values) {
image.imageProvider?.evict();
}
}

@override
String toString() {
final sb = StringBuffer('LottieComposition:\n');
Expand Down
19 changes: 18 additions & 1 deletion lib/src/lottie.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class Lottie extends StatefulWidget {
this.delegates,
this.options,
bool? addRepaintBoundary,
bool? autoDisposeImage,
this.filterQuality,
}) : animate = animate ?? true,
reverse = reverse ?? false,
repeat = repeat ?? true,
addRepaintBoundary = addRepaintBoundary ?? true;
addRepaintBoundary = addRepaintBoundary ?? true,
autoDisposeImage = autoDisposeImage ?? true;

/// Creates a widget that displays an [LottieComposition] obtained from an [AssetBundle].
static LottieBuilder asset(
Expand All @@ -58,6 +60,7 @@ class Lottie extends StatefulWidget {
AlignmentGeometry? alignment,
String? package,
bool? addRepaintBoundary,
bool? autoDisposeImage,
FilterQuality? filterQuality,
WarningCallback? onWarning,
}) =>
Expand All @@ -82,6 +85,7 @@ class Lottie extends StatefulWidget {
alignment: alignment,
package: package,
addRepaintBoundary: addRepaintBoundary,
autoDisposeImage: autoDisposeImage,
filterQuality: filterQuality,
onWarning: onWarning,
);
Expand All @@ -106,6 +110,7 @@ class Lottie extends StatefulWidget {
BoxFit? fit,
AlignmentGeometry? alignment,
bool? addRepaintBoundary,
bool? autoDisposeImage,
FilterQuality? filterQuality,
WarningCallback? onWarning,
}) =>
Expand All @@ -128,6 +133,7 @@ class Lottie extends StatefulWidget {
fit: fit,
alignment: alignment,
addRepaintBoundary: addRepaintBoundary,
autoDisposeImage: autoDisposeImage,
filterQuality: filterQuality,
onWarning: onWarning,
);
Expand All @@ -152,6 +158,7 @@ class Lottie extends StatefulWidget {
BoxFit? fit,
AlignmentGeometry? alignment,
bool? addRepaintBoundary,
bool? autoDisposeImage,
FilterQuality? filterQuality,
WarningCallback? onWarning,
}) =>
Expand All @@ -174,6 +181,7 @@ class Lottie extends StatefulWidget {
fit: fit,
alignment: alignment,
addRepaintBoundary: addRepaintBoundary,
autoDisposeImage: autoDisposeImage,
filterQuality: filterQuality,
onWarning: onWarning,
);
Expand All @@ -198,6 +206,7 @@ class Lottie extends StatefulWidget {
BoxFit? fit,
AlignmentGeometry? alignment,
bool? addRepaintBoundary,
bool? autoDisposeImage,
FilterQuality? filterQuality,
WarningCallback? onWarning,
}) =>
Expand All @@ -220,6 +229,7 @@ class Lottie extends StatefulWidget {
fit: fit,
alignment: alignment,
addRepaintBoundary: addRepaintBoundary,
autoDisposeImage: autoDisposeImage,
filterQuality: filterQuality,
onWarning: onWarning,
);
Expand Down Expand Up @@ -312,6 +322,10 @@ class Lottie extends StatefulWidget {
/// This property is `true` by default.
final bool addRepaintBoundary;

/// Indicate to automatically evict image from cache when object dispose
/// This property is `true` by default.
final bool autoDisposeImage;

/// The quality of the image layer. See [FilterQuality]
/// [FilterQuality.high] is highest quality but slowest.
///
Expand Down Expand Up @@ -364,6 +378,9 @@ class _LottieState extends State<Lottie> with TickerProviderStateMixin {
@override
void dispose() {
_autoAnimation.dispose();
if (widget.autoDisposeImage && widget.composition != null) {
widget.composition!.dispose();
}
super.dispose();
}

Expand Down
18 changes: 18 additions & 0 deletions lib/src/lottie_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class LottieBuilder extends StatefulWidget {
this.fit,
this.alignment,
this.addRepaintBoundary,
this.autoDisposeImage,
this.filterQuality,
this.onWarning,
});
Expand All @@ -83,6 +84,7 @@ class LottieBuilder extends StatefulWidget {
this.fit,
this.alignment,
this.addRepaintBoundary,
this.autoDisposeImage,
this.filterQuality,
this.onWarning,
}) : lottie = NetworkLottie(src,
Expand Down Expand Up @@ -117,6 +119,7 @@ class LottieBuilder extends StatefulWidget {
this.fit,
this.alignment,
this.addRepaintBoundary,
this.autoDisposeImage,
this.filterQuality,
this.onWarning,
}) : lottie = FileLottie(file, imageProviderFactory: imageProviderFactory);
Expand All @@ -143,6 +146,7 @@ class LottieBuilder extends StatefulWidget {
this.alignment,
String? package,
this.addRepaintBoundary,
this.autoDisposeImage,
this.filterQuality,
this.onWarning,
}) : lottie = AssetLottie(name,
Expand Down Expand Up @@ -170,6 +174,7 @@ class LottieBuilder extends StatefulWidget {
this.fit,
this.alignment,
this.addRepaintBoundary,
this.autoDisposeImage,
this.filterQuality,
this.onWarning,
}) : lottie = MemoryLottie(bytes, imageProviderFactory: imageProviderFactory);
Expand Down Expand Up @@ -361,6 +366,10 @@ class LottieBuilder extends StatefulWidget {
/// This property is `true` by default.
final bool? addRepaintBoundary;

/// Indicate to automatically evict image from cache when object dispose
/// This property is `true` by default.
final bool? autoDisposeImage;

/// The quality of the image layer. See [FilterQuality]
/// [FilterQuality.high] is highest quality but slowest.
///
Expand Down Expand Up @@ -439,6 +448,14 @@ class _LottieBuilderState extends State<LottieBuilder> {
}
}

@override
void dispose() {
if (widget.autoDisposeImage != null && widget.autoDisposeImage!) {
sharedLottieCache.evict(widget.lottie);
}
super.dispose();
}

void _load() {
var provider = widget.lottie;
_loadingFuture = widget.lottie.load().then((composition) {
Expand Down Expand Up @@ -495,6 +512,7 @@ class _LottieBuilderState extends State<LottieBuilder> {
fit: widget.fit,
alignment: widget.alignment,
addRepaintBoundary: widget.addRepaintBoundary,
autoDisposeImage: widget.autoDisposeImage,
filterQuality: widget.filterQuality,
);

Expand Down
6 changes: 5 additions & 1 deletion lib/src/lottie_image_asset.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';

class LottieImageAsset {
final int width;
final int height;
final String id;
final String fileName;
final String dirName;
ui.Image? loadedImage;
ImageProvider? imageProvider;

LottieImageAsset(
{required this.width,
required this.height,
required this.id,
required this.fileName,
required this.dirName});
required this.dirName,
this.imageProvider});

@override
String toString() =>
Expand Down
2 changes: 2 additions & 0 deletions lib/src/providers/load_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ typedef LottieImageProviderFactory = ImageProvider? Function(LottieImageAsset);

Future<ui.Image?> loadImage(LottieComposition composition,
LottieImageAsset lottieImage, ImageProvider provider) {
lottieImage.imageProvider = provider;

var completer = Completer<ui.Image?>();
var imageStream = provider.resolve(ImageConfiguration.empty);
late ImageStreamListener listener;
Expand Down