disk_cached_image 0.1.1 copy "disk_cached_image: ^0.1.1" to clipboard
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.

example/lib/main.dart

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);
}
0
likes
160
points
233
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter widget and cache service that downloads network images to disk once, serves them from disk, and supports TTL, eviction, and clearing.

Repository (GitHub)
View/report issues

Topics

#widget #image #cache #disk #network

License

MIT (license)

Dependencies

flutter, http, path_provider

More

Packages that depend on disk_cached_image