Asset Class Generator
A Flutter package that automatically generates Dart classes for assets by watching the assets folder.
Features
- Watches all subfolders under
assets/(e.g.,images,svgs,fonts, etc.). - Generates classes like
AppImages,AppSvgs, etc., with camelCase asset names. - Updates
lib/assets.dartwhenever new assets are added.
Installation
Add the package to your pubspec.yaml:
dependencies:
asset_class_generator: ^0.0.1
Then, run:
flutter pub get
Usage
To generate asset classes for your Flutter project, follow these steps:
1️⃣ Create a Script
- Inside your project, create a folder named
tools/(if it doesn’t exist). - Create a file named
generate_assets.dartinsidetools/.
2️⃣ Add the Following Code
import 'package:asset_class_generator/asset_class_generator.dart';
void main() {
generateAssetClasses();
}
3️⃣ Run the Script
In your terminal, execute:
dart run tools/generate_assets.dart
This will scan your assets/ folder and update lib/assets.dart automatically.
Example Output (lib/assets.dart)
class AppImages {
static const String logo = 'assets/images/logo.png';
static const String banner = 'assets/images/banner.png';
}
class AppSvgs {
static const String icon = 'assets/svgs/icon.svg';
}
Now, you can use it in your Flutter app like this:
Image.asset(AppImages.logo);
SvgPicture.asset(AppSvgs.icon);
Development & Contribution
Feel free to contribute to this package! Fork the repo, submit a PR, or report issues.
Future Improvements
- Add support for additional file types.
- Improve CLI automation.
Links
- 📖 Documentation: pub.dev
- 🛠 Source Code: GitHub
- 🐞 Report Issues: GitHub Issues