Image Cache Pro 🚀

pub package GitHub Stars License: MIT

A high-performance Flutter image caching package with automatic memory and disk management.

✨ Features

  • 3-Tier Caching: Memory → Disk → Network
  • LRU Memory Cache: Automatically removes oldest images (max 50)
  • Disk Cache Management: Auto-cleanup when exceeds 100MB
  • Expiration: Removes cached images after 30 days
  • Fade Animation: Smooth image loading with customizable duration
  • Custom Placeholders & Error Widgets
  • Precaching Support: Load images in background
  • Border Radius Support: Rounded corners
  • Color Overlay: Apply color filters
  • Custom Headers: Add authentication headers

📦 Installation

Add to pubspec.yaml:

dependencies:
  image_cache_pro: ^1.0.0

Then run:

flutter pub get

🎯 Usage

Basic Example

import 'package:image_cache_pro/image_cache_pro.dart';

CachedImagePro(
  imageUrl: 'https://picsum.photos/400/300',
  width: 200,
  height: 200,
  fit: BoxFit.cover,
)

With Placeholder & Error Widget

CachedImagePro(
  imageUrl: 'https://example.com/image.jpg',
  width: double.infinity,
  height: 200,
  placeholder: Container(
    color: Colors.grey.shade200,
    child: Center(child: CircularProgressIndicator()),
  ),
  errorWidget: Container(
    color: Colors.red.shade100,
    child: Icon(Icons.error, color: Colors.red),
  ),
  fit: BoxFit.cover,
)

With Border Radius

CachedImagePro(
  imageUrl: 'https://example.com/image.jpg',
  width: 200,
  height: 200,
  borderRadius: BorderRadius.circular(16),
  fit: BoxFit.cover,
)

With Color Overlay

CachedImagePro(
  imageUrl: 'https://example.com/image.jpg',
  width: 200,
  height: 200,
  color: Colors.blue.withOpacity(0.5),
  colorBlendMode: BlendMode.multiply,
)

With Custom Headers (Authentication)

CachedImagePro(
  imageUrl: 'https://api.example.com/private/image.jpg',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
    'Custom-Header': 'value',
  },
)

🛠️ Cache Management

Clear All Cache

await ImageCacheManager.instance.clearCache();

Clear Memory Only

ImageCacheManager.instance.clearMemoryCache();

Clear Disk Only

await ImageCacheManager.instance.clearDiskCache();

Remove Specific Image

await ImageCacheManager.instance.removeImage('https://example.com/image.jpg');

Get Cache Statistics

final info = await ImageCacheManager.instance.getCacheInfo();
print('Disk cache size: ${info['disk_cache_size']} bytes');
print('Disk cache count: ${info['disk_cache_count']} files');
print('Memory cache count: ${info['memory_cache_count']} images');

🔄 Precaching Images

Precache Single Image

await ImageCacheManager.instance.precacheImage('https://example.com/image.jpg');

Precache Multiple Images

await ImageCacheManager.instance.precacheImages([
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  'https://example.com/image3.jpg',
]);

📋 All Parameters

Parameter Type Default Description
imageUrl String required URL of the image to load
width double? null Image width
height double? null Image height
fit BoxFit BoxFit.cover How image should fit
placeholder Widget? Loading spinner Shown while loading
errorWidget Widget? Broken image icon Shown on error
fadeInDuration Duration 300ms Fade-in animation duration
fadeInCurve Curve Curves.easeIn Fade-in animation curve
borderRadius BorderRadius? null Rounded corners
color Color? null Color overlay
colorBlendMode BlendMode? null Blend mode for overlay
headers Map<String, String>? null Custom HTTP headers

🎨 Complete Example

import 'package:flutter/material.dart';
import 'package:image_cache_pro/image_cache_pro.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Image Cache Pro'),
          actions: [
            IconButton(
              icon: Icon(Icons.delete),
              onPressed: () async {
                await ImageCacheManager.instance.clearCache();
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Cache cleared!')),
                );
              },
            ),
          ],
        ),
        body: ListView.builder(
          itemCount: 20,
          itemBuilder: (context, i) {
            return Padding(
              padding: EdgeInsets.all(8),
              child: CachedImagePro(
                imageUrl: 'https://picsum.photos/400/300?random=$i',
                width: double.infinity,
                height: 200,
                borderRadius: BorderRadius.circular(12),
                placeholder: Container(
                  color: Colors.grey.shade200,
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                ),
                errorWidget: Container(
                  color: Colors.red.shade100,
                  child: Center(
                    child: Icon(Icons.error, color: Colors.red),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

⚙️ Configuration

Default settings (can be modified in cache_storage.dart):

  • Max memory cache: 50 images
  • Max disk cache size: 100 MB
  • Cache expiration: 30 days
  • Network timeout: 30 seconds

📄 License

MIT License

🤝 Contributing

Contributions are welcome! Please open an issue or submit a PR.

👨‍💻 Author

Rasel2510

Libraries

image_cache_pro