disk_cached_image 0.1.1
disk_cached_image: ^0.1.1 copied to clipboard
A Flutter widget and cache service that downloads network images to disk once, serves them from disk, and supports TTL, eviction, and clearing.
import 'package:disk_cached_image/disk_cached_image.dart';
import 'package:flutter/material.dart';
void main() => runApp(const ExampleApp());
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'disk_cached_image example',
home: ExamplePage(),
);
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
static final DiskImageCache _cache = DiskImageCache();
Future<void> _clearCache(BuildContext context) async {
final messenger = ScaffoldMessenger.of(context);
final before = await _cache.size();
await _cache.clear();
final after = await _cache.size();
messenger.showSnackBar(
SnackBar(content: Text('Cache size: $before -> $after bytes')),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('disk_cached_image example')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Wrap(
spacing: 16,
children: [
DiskCachedImage(
url: 'https://picsum.photos/seed/one/200',
cacheKey: 'picsum-one',
cache: _cache,
width: 120,
height: 120,
),
DiskCachedImage(
url: 'https://picsum.photos/seed/two/200',
cacheKey: 'picsum-two',
cache: _cache,
width: 120,
height: 120,
placeholder: const Center(child: CircularProgressIndicator()),
errorBuilder: _buildError,
),
],
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () => _clearCache(context),
child: const Text('Clear cache'),
),
],
),
),
);
}
}
Widget _buildError(BuildContext context, Object error, StackTrace? stackTrace) {
return const Icon(Icons.broken_image, size: 120);
}