auto_gen_assets 1.0.0 copy "auto_gen_assets: ^1.0.0" to clipboard
auto_gen_assets: ^1.0.0 copied to clipboard

A Flutter package for automatically generating strongly-typed asset references from your assets directory.

auto_gen_assets #

A Flutter package for automatically generating strongly-typed asset references from your assets directory.

Features #

  • 🔄 Automatic Generation: Scans your assets directory and generates Dart code
  • 📁 Folder Organization: Groups assets by folder structure
  • 🎯 Type Safety: Provides strongly-typed asset references
  • ⚙️ Configurable: Customize output location and file filtering
  • 🚀 Easy to Use: Simple API with sensible defaults

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  auto_gen_assets: ^1.0.0

Usage #

Basic Usage #

  1. Create an assets directory structure in your Flutter project:
assets/
├── images/
│   ├── logo.png
│   ├── background.jpg
│   └── icons/
│       ├── home.png
│       └── settings.png
├── animations/
│   ├── loading.json
│   └── success.json
└── fonts/
    ├── roboto.ttf
    └── opensans.ttf
  1. Run the generator:
dart run auto_gen_assets
  1. Use the generated assets in your Flutter code:
import 'generated/assets.dart';

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Image.asset(Assets.images.logo),
        Image.asset(Assets.images.icons.home),
        Lottie.asset(Assets.animations.loading),
      ],
    );
  }
}

Programmatic Usage #

You can also use the generator programmatically:

import 'package:auto_gen_assets/auto_gen_assets.dart';

void main() {
  const generator = AssetGenerator(
    assetsDirectory: 'assets',
    outputFile: 'lib/generated/assets.dart',
    ignoreHiddenFiles: true,
    ignoreEnvFiles: true,
  );
  
  final success = generator.generate();
  if (success) {
    print('Assets generated successfully!');
  }
}

Configuration Options #

The AssetGenerator class accepts the following configuration options:

Parameter Type Default Description
assetsDirectory String 'assets' The directory containing assets to scan
outputFile String 'lib/generated/assets.dart' The output file path
ignoreHiddenFiles bool true Whether to ignore hidden files (starting with '.')
ignoreEnvFiles bool true Whether to ignore environment files (containing '.env')

Generated Code Structure #

The generator creates a structured Dart file with the following organization:

/// This file is automatically generated. DO NOT EDIT.
/// Generated by auto_gen_assets package.

class Assets {
  Assets._();

  static const Images images = Images();
  static const Animations animations = Animations();
  static const Fonts fonts = Fonts();
}

class Images {
  const Images();
  final String logo = 'assets/images/logo.png';
  final String background = 'assets/images/background.jpg';
}

class Icons {
  const Icons();
  final String home = 'assets/images/icons/home.png';
  final String settings = 'assets/images/icons/settings.png';
}

class Animations {
  const Animations();
  final String loading = 'assets/animations/loading.json';
  final String success = 'assets/animations/success.json';
}

class Fonts {
  const Fonts();
  final String roboto = 'assets/fonts/roboto.ttf';
  final String opensans = 'assets/fonts/opensans.ttf';
}

Naming Conventions #

  • Folder names are converted to PascalCase for class names
  • File names are converted to camelCase for property names
  • Underscores and hyphens in names are properly handled

Examples:

  • my_folderMyFolder (class name)
  • my_file_name.pngmyFileName (property name)
  • user-avatar.jpguserAvatar (property name)

Integration with Build Process #

Using build_runner #

Add to your pubspec.yaml:

dev_dependencies:
  build_runner: ^2.4.7
  build: ^2.4.1

Create a build script:

// build.yaml
targets:
  $default:
    builders:
      auto_gen_assets|asset_builder:
        enabled: true
        options:
          assets_directory: assets
          output_file: lib/generated/assets.dart

Using Makefile #

Create a Makefile in your project root:

.PHONY: generate-assets
generate-assets:
	dart run auto_gen_assets

.PHONY: watch-assets
watch-assets:
	fswatch -o assets | xargs -n1 -I{} dart run auto_gen_assets

Examples #

Complete Example Project #

// lib/main.dart
import 'package:flutter/material.dart';
import 'generated/assets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Auto Gen Assets Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Auto Gen Assets Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              Assets.images.logo,
              width: 200,
              height: 200,
            ),
            SizedBox(height: 20),
            Text(
              'Assets loaded successfully!',
              style: TextStyle(fontSize: 18),
            ),
          ],
        ),
      ),
    );
  }
}

Contributing #

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog #

See CHANGELOG.md for a list of changes and version history.

1
likes
0
points
27
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for automatically generating strongly-typed asset references from your assets directory.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on auto_gen_assets