Diverse Image
A Flutter package to easily display images from multiple sources (assets, network, cache) and support various formats like JPG, PNG, SVG, and more.
Features
- Display images from assets, network, and cached files using a unified API.
- Support for multiple formats: JPG, PNG, SVG, and more.
- Placeholder and error widgets for improved user experience.
- Automatic detection of image source type based on the
path.
Installation
Add the following line to your pubspec.yaml:
dependencies:
diverse_image: ^1.0.0
Then run:
flutter pub get
Usage
Import the package:
import 'package:diverse_image/diverse_image.dart';
Display an Image
DiverseImage(
path: 'image-path', // Path can be an asset, network URL, or cache key.
width: 100,
height: 100,
placeholder: CircularProgressIndicator(),
errorWidget: Icon(Icons.error),
)
Examples:
- Load an Asset Image:
DiverseImage(
path: 'assets/images/sample.png',
width: 100,
height: 100,
placeholder: CircularProgressIndicator(),
errorWidget: Icon(Icons.error),
)
- Load a Network Image:
DiverseImage(
path: 'https://example.com/image.jpg',
width: 150,
height: 150,
placeholder: CircularProgressIndicator(),
errorWidget: Icon(Icons.error),
)
- Load a Cached Image:
DiverseImage(
path: 'cached_key_for_image',
width: 200,
height: 200,
placeholder: CircularProgressIndicator(),
errorWidget: Icon(Icons.error),
)
Common Properties:
path: The image source (can be an asset path, network URL, or cache key).widthandheight: Define the dimensions of the image.placeholder: Widget to show while the image is loading.errorWidget: Widget to display when the image fails to load.fit: Adjust how the image should fit its bounding box (e.g.,BoxFit.cover).
Example
Here’s a complete example of using DiverseImage:
import 'package:flutter/material.dart';
import 'package:diverse_image/diverse_image.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Diverse Image Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DiverseImage(
path: 'assets/images/sample.png',
width: 100,
height: 100,
placeholder: CircularProgressIndicator(),
),
SizedBox(height: 20),
DiverseImage(
path: 'https://example.com/image.jpg',
width: 150,
height: 150,
errorWidget: Icon(Icons.error),
),
],
),
),
),
);
}
}
Contributing
We welcome contributions! Please check the CONTRIBUTING.md for guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Developed with ❤️ by Your Name/Team.