asset_image_generator 1.0.0
asset_image_generator: ^1.0.0 copied to clipboard
A Dart plugin to generate app_images.dart file with static constants for all asset images
Asset Image Generator for Flutter #
A Flutter plugin that automatically generates type-safe Dart references for all your asset images, eliminating hardcoded strings and improving maintainability.
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
- 📊 Includes complete list of all assets
- ⚙️ Customizable output location
Installation #
Add to your project's pubspec.yaml:
dev_dependencies:
asset_image_generator: ^1.0.0
Then run:
flutter pub get
Usage #
Command Line #
Run 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();
// With custom output path:
// await AssetImageGenerator().generate(
// outputPath: 'lib/constants/images.dart'
// );
}
Example Output #
lib/generated/app_images.dart:
// GENERATED CODE - DO NOT MODIFY BY HAND
// Generated by asset_image_generator
class AppImages {
AppImages._();
/// assets/images/logo.png
static const String logo = 'assets/images/logo.png';
/// assets/icons/home.svg
static const String homeIcon = 'assets/icons/home.svg';
static const List<String> all = [
logo,
homeIcon,
];
}
Using Generated Assets #
import 'package:flutter/material.dart';
import 'package:my_app/generated/app_images.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Image.asset(AppImages.logo);
}
}
Configuration #
Ensure your pubspec.yaml has proper asset declarations:
flutter:
assets:
- assets/images/
- assets/icons/
The generator will automatically:
- Create the output directory if needed
- Convert Windows paths to forward slashes
- Sort assets alphabetically
Best Practices #
- Add to CI: Include generation in your build process
- Version Control: Commit the generated file
- Regenerate: Run after adding new images
- Organize Assets: Use clear directory structure
- Use Constants: Always reference via AppImages
Troubleshooting #
No assets found
- Verify asset paths in pubspec.yaml
- Ensure directories exist
- Check for supported file extensions
Path issues on Windows
- The plugin automatically converts backslashes to forward slashes.
Contributing #
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
License #
MIT © Abhilash Puthukkudi