Skip to main content

Cached Image

A widget that displays an image loaded from a URL with caching support. The C2CCachedImage widget is used to display an image loaded from a URL.

It supports caching of the image using the flutter_cache_manager package. The image is fetched from the URL and stored in the cache for future use.

The C2CCachedImage widget provides various options for customizing the image display, such as size, fit, alignment, placeholder image, error image, and fade animation.

Installation

cpub add c2c_cached_image

or

flutter pub add c2c_cached_image --hosted-url=https://pub.csp.scrum-dev.com

Example usage:

// Normal image
const C2CCachedImage(
size: Size(double.infinity, 250),
imageUrl: 'https://example.com/your_image.jpg',
errorImage: AssetImage('assets/images/noAvatar.png'),
placeholderImage: AssetImage('assets/images/loading.png'),
)

// Circle image
const C2CCachedImage(
size: Size.square(200),
imageUrl: 'https://example.com/your_image.jpg',
errorImage: AssetImage('assets/images/noAvatar.png'),
placeholderImage: AssetImage('assets/images/loading.png'),
shape: BoxShape.circle,
)

// Show image with min/max size
const C2CCachedImage(
constraints: BoxConstraints(
minWidth: 150,
maxWidth: 250,
minHeight: 150,
maxHeight: 250,
),
imageUrl: 'https://example.com/your_image.jpg',
placeholderImage: AssetImage('assets/images/noAvatar.png'),
isShowPlaceholderWhenError: true,
)

Properties

PropertyDescriptionType
imageUrlThe URL of the target image.String
scaleThe scale of the image.double
headersHTTP headers for the image request.Map<String, String>
cacheManagerCustom cache manager for handling image caching.BaseCacheManager
cacheKeyThe cache key for the image.String?
sizeThe size of the image. Size(width, height). Default is: Size(40,40)Size?
constraintsMinimum and maximum size constraints for the image.BoxConstraints?
shapeThe shape of the image container. (rectangle or circle). Default: BoxShape.rectangleBoxShape
fitHow to inscribe the image into the allocated space. Default: BoxFit.coverBoxFit
filterQualityThe rendering quality of the image.FilterQuality
alignmentHow to align the image within its bounds.AlignmentGeometry
repeatHow to repeat the image if it doesn't cover the entire space.ImageRepeat
backgroundColorThe background color of the image container.Color?
placeholderImageImage to display while the target image is loading.ImageProvider?
placeholderImageFitHow to fit the placeholder image.BoxFit?
placeholderImageFilterQualityRendering quality of the placeholder image.FilterQuality?
placeholderImageBuilderBuilder function for the placeholder.Widget Function(BuildContext)?
isShowPlaceholderWhenErrorWhether to show the placeholder when an error occurs.bool?
errorImageImage to display if an error occurs during loading.ImageProvider?
errorImageFitHow to fit the error image.BoxFit?
errorImageFilterQualityRendering quality of the error image.FilterQuality?
errorImageBuilderBuilder function for the error image.Widget Function(BuildContext)?
isFadeEnabledEnable or disable fade-in/out animations.bool?
fadeOutDurationDuration of the fade-out animation for the placeholder image.Duration
fadeOutCurveCurve of the fade-out animation for the placeholder image.Curve
fadeInDurationDuration of the fade-in animation for the target image.Duration
fadeInCurveCurve of the fade-in animation for the target image.Curve
matchTextDirectionWhether to paint the image based on text direction.bool
excludeFromSemanticsWhether to exclude the image from semantics.bool
imageSemanticLabelA semantic description of the image.String?

Preloading Images

You can use the preload method to preload a list of images before they are displayed in your Flutter app. This can help improve the user experience by loading images in the background. Here's an example of how to use the preload method:

List<SourceItem> sources = [
SourceItem(url: 'https://example.com/image1.jpg'),
SourceItem(url: 'https://example.com/image2.jpg'),
SourceItem(url: 'https://example.com/image3.jpg'),
];

Future<void> preloadImages() async {
List<File> images = await C2CCachedImage.preload(sources);
// You can now use the preloaded images as needed
// For example, you can display them using the Image widget.
// images[0] corresponds to the preloaded image for source 1, and so on.
}

Removing Cached Images

You can use the removeItem method to remove a cached image by its cache key. This can be useful if you want to clear specific images from the cache. Here's an example of how to use the removeItem method:

String cacheKeyToRemove = 'custom_cache_key';

void removeCachedImage() {
C2CCachedImage.removeItem(cacheKeyToRemove);
}