Asset Image Generator for Flutter
A Flutter plugin that automatically generates type-safe Dart references for all your asset images โ now with folder-based class generation in v2.0.0.
๐ What's New in v3.1.0
- ๐ Generates separate Dart files for each asset folder
- ๐ Class names based on folder structure (e.g.,
IconsImages,BackgroundsImages) - ๐ Variables for both full paths and image names:
homeandhomeName
โ ๏ธ Breaking Changes: If you're upgrading from
v1.x, see Migration Guide โ
โจ Features
- ๐ Scans
pubspec.yamlfor asset paths automatically - ๐ Generates clean, type-safe Dart constants
- ๐ผ Supports PNG, JPG, JPEG, GIF, BMP, WEBP, and SVG formats
- ๐ Converts filenames to consistent camelCase variables
- ๐ Works across Windows, macOS, and Linux
- ๐ Lists all assets and names for dynamic use
- โ๏ธ Customizable output location
- ๐งฉ Folder-based organization with tree-shakable structure
๐ Installation
Add to your project's pubspec.yaml under dev_dependencies:
dev_dependencies:
asset_image_generator: ^2.0.0
Then run:
flutter pub get
โ๏ธ Usage
๐ง Command Line
From your project root:
flutter pub run asset_image_generator:generate_images
๐งโ๐ป Programmatic Usage
import 'package:asset_image_generator/asset_image_generator.dart';
void main() async {
await AssetImageGenerator().generate();
// OR with custom output path
// await AssetImageGenerator().generate(
// outputPath: 'lib/generated/images/'
// );
}
๐ Example Output Structure
lib/generated/images/
โโโ images.dart # Main export file
โโโ app_images.dart # Root level images
โโโ icons_images.dart # From assets/icons/
โโโ backgrounds_images.dart # From assets/backgrounds/
๐ Accessing Assets
Option 1: Through Images main class
import 'package:your_app/generated/images/images.dart';
Image.asset(Images.icons.home); // Full path
print(Images.icons.homeName); // Just the name
Option 2: Direct import from specific class
import 'package:your_app/generated/images/icons_images.dart';
Image.asset(IconsImages.home);
print(IconsImages.getPathByName('home'));
โ Configuration
Ensure your pubspec.yaml has proper asset declarations:
flutter:
assets:
- assets/icons/
- assets/images/
- assets/backgrounds/
๐ Best Practices
- โ Add this to your CI build process
- โ Commit generated files to version control
- ๐ Regenerate assets after adding new files
- ๐งน Keep assets organized in folders
๐ Supported Extensions
.png,.jpg,.jpeg,.gif,.bmp,.webp,.svg
๐ Troubleshooting
No assets found?
- Check that folders exist and are listed in
pubspec.yaml - Only supported image extensions are picked up
Paths not working on Windows?
- Generator normalizes paths with forward slashes (
/)
๐ค Contributing
Contributions are welcome! Please:
- Fork the repo
- Create a feature branch
- Open a pull request
๐ License
MIT ยฉ Abhilash Puthukkudi
๐ฆ Pub.dev
๐ View on pub.dev โ