res_generator 1.2.0
res_generator: ^1.2.0 copied to clipboard
For Assets generator and translator (localizations, images, icons)
ResGenerator #
A Flutter code generator for images, icons, and localization with built-in translation support.
---

Features #
- ✅ Auto-generate Dart classes from SVG icons and PNG/JPG images
- ✅ Generate and translate localization files automatically
- ✅ Seamless integration with
easy_localizationand similar packages - ✅ Type-safe resource access
- ✅ Convenient
copyWithmethods for customization
Installation #
Add to your pubspec.yaml:
dev_dependencies:
res_generator: ^version
Configuration #
Create res_generator.yaml in your project root (next to pubspec.yaml):
words:
assets_directory: assets/tr/
class_directory: lib/core/common/words/
class_file: words.dart
class_name: Words
supported_locales: ['uz', 'en', 'ru'] # or ['uz-UZ', 'en-EN', 'ru-RU']
translated_locales: ['en', 'ru'] # locales to translate
target_locale: 'uz' # source locale
inline: false # true → embed translations as Dart const maps (WordsLoader)
icons:
assets_directory: assets/icons/
class_directory: lib/widgets/
class_file: app_icons.dart
class_name: AppIcons
inline: false # true → embed SVGs as raw strings
height: 24 # optional default height for every icon
images:
assets_directory: assets/images/
class_directory: lib/widgets/
class_file: app_images.dart
class_name: AppImages
Only .svg files are taken from icons.assets_directory, and only .png, .jpg, .jpeg, .webp, .gif, .bmp, .wbmp files from images.assets_directory; both are sorted by file name.
With inline: true every SVG is minified and embedded into app_icons.res.dart as a raw string (SvgPicture.string), so icon changes can be shipped with a Shorebird patch. The assets/icons/ entry in pubspec.yaml is then no longer needed (Shorebird users: remove it only in the next store release, not in a patch).
With words.inline: true the translations are embedded into words.res.dart as const Dart maps together with a generated WordsLoader (an easy_localization AssetLoader), so translation changes can be shipped with a Shorebird patch. Inline mode requires easy_localization: pass assetLoader: const WordsLoader() to EasyLocalization; path is still required by easy_localization but is ignored. An existing host words.dart is not regenerated, so add import 'dart:ui'; to it yourself. Keep assets/tr/ in pubspec.yaml until the next store release (Shorebird).
Usage #
Generate Resources #
dart run res_generator:generate
Translate Localization #
dart run res_generator:translate
After translation the Dart files are regenerated, so the words class (and the inline maps) reflect the translated JSON.
Code Examples #
Icons #
// Simple usage
Scaffold(body: AppIcons.logo)
// With customization
AppIcons.logo.copyWith(
width: 24,
height: 24,
fit: BoxFit.fill,
color: Colors.blue, // same as colorFilter: ColorFilter.mode(Colors.blue, BlendMode.srcIn)
)
// Using path directly (asset mode only)
SvgPicture.asset(AppIcons.logo.path)
// width/height are a pair: passing one of them resets the other to null
AppIcons.logo.copyWith(width: 32) // width: 32, height: null (scales by width)
AppIcons.logo.copyWith(color: Colors.red) // size is kept
// All icons: AppIcons.list (asset mode, paths) or AppIcons.values (inline mode, name → SvgPicture)
AppIcons.values.forEach((name, icon) => print(name));
Images #
// Simple usage
Scaffold(body: AppImages.splash)
// With customization
AppImages.splash.copyWith(
width: double.infinity,
height: double.infinity,
fit: BoxFit.cover,
color: Colors.black26,
colorBlendMode: BlendMode.darken,
filterQuality: FilterQuality.high,
)
// Using path directly
Image.asset(AppImages.splash.path)
Localization #
// Method 1 # Recommended for static words
Text(Words.documents.str)
// Method 2 # Recommended for dynamic words
Text("documents".str)
// Method 3
Text(str(Words.documents))
// Method 4
Text(str("documents"))
// Inline mode (words.inline: true)
EasyLocalization(
supportedLocales: const [Locale('uz'), Locale('en'), Locale('ru')],
path: 'assets/tr', // required by easy_localization, ignored by WordsLoader
assetLoader: const WordsLoader(),
child: const App(),
)
Generated Files #
The generator creates two types of files:
*.dart- Safe to modify for customization*.res.dart- Auto-generated, do not modify (will be overwritten)
Generated files are formatted with dart_style using your project's language version and the formatter section (page_width, trailing_commas) of analysis_options.yaml, so dart format and the IDE leave them unchanged.