image_cache_pro 1.0.0 copy "image_cache_pro: ^1.0.0" to clipboard
image_cache_pro: ^1.0.0 copied to clipboard

High-performance image caching with 3-tier system (Memory, Disk, Network). Auto-cleanup, LRU eviction, and precaching support.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Cache Pro Example',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  Map<String, dynamic>? _cacheInfo;

  @override
  void initState() {
    super.initState();
    _loadCacheInfo();
  }

  Future<void> _loadCacheInfo() async {
    final info = await ImageCacheManager.instance.getCacheInfo();
    setState(() => _cacheInfo = info);
  }

  Future<void> _clearCache() async {
    await ImageCacheManager.instance.clearCache();
    await _loadCacheInfo();
    if (mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Cache cleared successfully!')),
      );
    }
  }

  Future<void> _precacheImages() async {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('Precaching images...')),
    );

    final urls = List.generate(
      20,
      (i) => 'https://picsum.photos/400/300?random=$i',
    );

    await ImageCacheManager.instance.precacheImages(urls);
    await _loadCacheInfo();

    if (mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Precaching complete!')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Image Cache Pro'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
        actions: [
          IconButton(
            icon: const Icon(Icons.info_outline),
            onPressed: () => _showCacheInfo(),
          ),
          IconButton(
            icon: const Icon(Icons.delete_outline),
            onPressed: _clearCache,
          ),
        ],
      ),
      body: Column(
        children: [
          // Cache info banner
          if (_cacheInfo != null)
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(16),
              color: Colors.blue.shade50,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  _buildStat(
                    'Memory',
                    '${_cacheInfo!['memory_cache_count']}',
                    Icons.memory,
                  ),
                  _buildStat(
                    'Disk',
                    '${_cacheInfo!['disk_cache_count']}',
                    Icons.storage,
                  ),
                  _buildStat(
                    'Size',
                    '${(_cacheInfo!['disk_cache_size'] / 1024 / 1024).toStringAsFixed(1)} MB',
                    Icons.folder,
                  ),
                ],
              ),
            ),

          // Image grid
          Expanded(
            child: GridView.builder(
              padding: const EdgeInsets.all(8),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 8,
                mainAxisSpacing: 8,
                childAspectRatio: 1,
              ),
              itemCount: 20,
              itemBuilder: (context, i) {
                return CachedImagePro(
                  imageUrl: 'https://picsum.photos/400/300?random=$i',
                  width: double.infinity,
                  height: double.infinity,
                  borderRadius: BorderRadius.circular(12),
                  fit: BoxFit.cover,
                  placeholder: Container(
                    decoration: BoxDecoration(
                      color: Colors.grey.shade200,
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: const Center(
                      child: CircularProgressIndicator(strokeWidth: 2),
                    ),
                  ),
                  errorWidget: Container(
                    decoration: BoxDecoration(
                      color: Colors.red.shade100,
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: const Center(
                      child: Icon(Icons.error, color: Colors.red, size: 40),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _precacheImages,
        icon: const Icon(Icons.download),
        label: const Text('Precache All'),
      ),
    );
  }

  Widget _buildStat(String label, String value, IconData icon) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, color: Colors.blue, size: 24),
        const SizedBox(height: 4),
        Text(
          value,
          style: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: Colors.grey.shade600,
          ),
        ),
      ],
    );
  }

  void _showCacheInfo() {
    if (_cacheInfo == null) return;

    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text('Cache Statistics'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Memory Cache: ${_cacheInfo!['memory_cache_count']} images'),
            const SizedBox(height: 8),
            Text('Disk Cache: ${_cacheInfo!['disk_cache_count']} files'),
            const SizedBox(height: 8),
            Text(
              'Disk Size: ${(_cacheInfo!['disk_cache_size'] / 1024 / 1024).toStringAsFixed(2)} MB',
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Close'),
          ),
        ],
      ),
    );
  }
}
1
likes
160
points
23
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

High-performance image caching with 3-tier system (Memory, Disk, Network). Auto-cleanup, LRU eviction, and precaching support.

Repository (GitHub)
View/report issues

Topics

#image #cache #network #performance #optimization

License

MIT (license)

Dependencies

crypto, flutter, http, path, path_provider

More

Packages that depend on image_cache_pro