Skip to content

Instantly share code, notes, and snippets.

@emiliodallatorre
Last active August 10, 2021 16:00
Show Gist options
  • Save emiliodallatorre/3372a05ac3305fc48604bbccfec5d019 to your computer and use it in GitHub Desktop.
Save emiliodallatorre/3372a05ac3305fc48604bbccfec5d019 to your computer and use it in GitHub Desktop.
A simple wrapper for CachedNetworkImage that lets you specify caching parameters globally.
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:resmedia_manita_flutter/references.dart';
class OptimizedNetworkImage extends StatelessWidget {
final String imageUrl;
final Widget Function(BuildContext, String) placeholder;
final BoxFit fit;
const OptimizedNetworkImage({
Key key,
@required this.imageUrl,
this.placeholder,
this.fit = BoxFit.cover,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CachedNetworkImage(
imageUrl: this.imageUrl,
placeholder: this.placeholder ?? (BuildContext context, String imageUrl) => Center(child: CircularProgressIndicator()),
fit: BoxFit.cover,
maxWidthDiskCache: References.optimizedImageMaxSize,
maxHeightDiskCache: References.optimizedImageMaxSize,
cacheManager: CustomCacheManager(),
);
}
}
class CustomCacheManager extends CacheManager with ImageCacheManager {
static const String key = 'customCacheKey';
static Config config = Config(
key,
stalePeriod: const Duration(days: 5),
maxNrOfCacheObjects: 384,
repo: JsonCacheInfoRepository(databaseName: key),
// fileSystem: IOFileSystem(key),
fileService: HttpFileService(),
);
CustomCacheManager() : super(config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment