{"id":15038823,"url":"https://github.com/fluttergen/flutter_gen","last_synced_at":"2025-05-13T22:12:20.400Z","repository":{"id":37919876,"uuid":"289226906","full_name":"FlutterGen/flutter_gen","owner":"FlutterGen","description":"The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.","archived":false,"fork":false,"pushed_at":"2025-05-09T04:36:00.000Z","size":5379,"stargazers_count":1534,"open_issues_count":26,"forks_count":165,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-05-09T05:28:02.168Z","etag":null,"topics":["assets","code-generator","dart","flutter","fluttergen","fonts","swiftgen","templates"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/flutter_gen","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FlutterGen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["wasabeef","AlexV525"]}},"created_at":"2020-08-21T09:09:54.000Z","updated_at":"2025-05-09T04:35:27.000Z","dependencies_parsed_at":"2023-02-05T03:46:37.149Z","dependency_job_id":"4228249d-6008-4492-af62-216e36269452","html_url":"https://github.com/FlutterGen/flutter_gen","commit_stats":{"total_commits":614,"total_committers":29,"mean_commits":21.17241379310345,"dds":0.3420195439739414,"last_synced_commit":"1314586b5c3c99fabc6e6d9ebeb0f063a977d112"},"previous_names":[],"tags_count":52,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlutterGen%2Fflutter_gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlutterGen%2Fflutter_gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlutterGen%2Fflutter_gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlutterGen%2Fflutter_gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FlutterGen","download_url":"https://codeload.github.com/FlutterGen/flutter_gen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036843,"owners_count":22003654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assets","code-generator","dart","flutter","fluttergen","fonts","swiftgen","templates"],"created_at":"2024-09-24T20:40:21.716Z","updated_at":"2025-05-13T22:12:15.379Z","avatar_url":"https://github.com/FlutterGen.png","language":"Dart","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://pub.dev/packages/flutter_gen\"\u003e\n    \u003cimg src=\"https://github.com/FlutterGen/flutter_gen/raw/main/art/logo.png\" width=\"480px\" alt=\"Logo\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://pub.dartlang.org/packages/flutter_gen\"\u003e\n    \u003cimg src=\"https://img.shields.io/pub/v/flutter_gen.svg\" alt=\"Pub\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/FlutterGen/flutter_gen/actions?query=workflow%3A%22Dart+CI%22\"\u003e\n    \u003cimg src=\"https://github.com/FlutterGen/flutter_gen/workflows/Build/badge.svg\" alt=\"Build Status\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/FlutterGen/flutter_gen\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/FlutterGen/flutter_gen/branch/main/graph/badge.svg\" alt=\"Coverage\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nThe Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.\n\nInspired by [SwiftGen](https://github.com/SwiftGen/SwiftGen).\n\n## Motivation\n\nUsing asset path string directly is not safe.\n\n```yaml\n# pubspec.yaml\nflutter:\n  assets:\n    - assets/images/profile.jpg\n```\n\n❌ **Bad**  \nWhat would happen if you made a typo?\n\n```dart\nWidget build(BuildContext context) {\n  return Image.asset('assets/images/profile.jpeg');\n}\n\n// The following assertion was thrown resolving an image codec:\n// Unable to load asset: assets/images/profile.jpeg\n```\n\n⭕️ **Good**  \nWe want to use it safely.\n\n```dart\nWidget build(BuildContext context) {\n  return Assets.images.profile.image();\n}\n```\n\n## Installation\n\n### As a part of build_runner\n\n1. Add [build_runner] and [FlutterGen] to your package's pubspec.yaml file:\n\n   ```yaml\n   dev_dependencies:\n     build_runner:\n     flutter_gen_runner:\n   ```\n\n2. Install [FlutterGen]\n\n   ```sh\n   flutter pub get\n   ```\n\n3. Use [FlutterGen]\n\n   ```sh\n   dart run build_runner build\n   ```\n\n### Pub Global\n\nWorks with macOS, Linux and Windows.\n\n```sh\ndart pub global activate flutter_gen\n```\n\nYou might need to [set up your path](https://dart.dev/tools/pub/cmd/pub-global#running-a-script-from-your-path).\n\n### Homebrew\n\nWorks with macOS and Linux.\n\n```sh\nbrew install FlutterGen/tap/fluttergen\n```\n\n### asdf\n\nWorks with macOS and Linux.\nasdf-fluttergen is compatible with [mise](https://mise.jdx.dev/).\n\n```sh\n# add plugin\nasdf plugin add fluttergen\n# or\nasdf plugin add fluttergen https://github.com/FlutterGen/asdf-fluttergen.git\n\n# install fluttergen\nasdf install fluttergen latest\n```\n\nSee also: [FlutterGen/asdf-fluttergen](https://github.com/FlutterGen/asdf-fluttergen)\n\n### GitHub Actions\n\nWorks with macOS and Linux.\n\n```yaml\n- uses: FlutterGen/setup-fluttergen@v1\n  with:\n    version: ${{ fluttergen_version }}\n```\n\nSee also: [FlutterGen/setup-fluttergen](https://github.com/FlutterGen/setup-fluttergen)\n\n## Usage\n\nRun `fluttergen` after the configuration [`pubspec.yaml`](https://dart.dev/tools/pub/pubspec).\n\n```sh\nfluttergen -h\n\nfluttergen -c example/pubspec.yaml\n```\n\n## Configuration file\n\n[FlutterGen] generates dart files based on the key **`flutter`** and **`flutter_gen`** of [`pubspec.yaml`](https://dart.dev/tools/pub/pubspec).  \nDefault configuration can be found [here](https://github.com/FlutterGen/flutter_gen/tree/main/packages/core/lib/settings/config_default.dart). \n\n```yaml\n# pubspec.yaml\n# ...\n\nflutter_gen:\n  output: lib/gen/ # Optional (default: lib/gen/)\n  line_length: 80 # Optional (default: 80)\n\n  # Optional\n  integrations:\n    image: true\n    flutter_svg: true\n    rive: true\n    lottie: true\n\n  colors:\n    inputs:\n      - assets/color/colors.xml\n\nflutter:\n  uses-material-design: true\n  assets:\n    - assets/images/\n\n  fonts:\n    - family: Raleway\n      fonts:\n        - asset: assets/fonts/Raleway-Regular.ttf\n        - asset: assets/fonts/Raleway-Italic.ttf\n          style: italic\n```\n\n### build.yaml\n\nYou can also configure generate options in the `build.yaml`, it will be read before the `pubspec.yaml` if it exists.\n\n\n```yaml\n# build.yaml\n# ...\n\ntargets:\n  $default:\n    builders:\n      flutter_gen_runner: # or flutter_gen\n        options: \n          output: lib/build_gen/ # Optional (default: lib/gen/)\n          line_length: 120 # Optional (default: 80)\n```\n\n## Available Parsers\n\n### Assets\n\nFollowing the doc\n[Adding assets and images#Specifying assets](https://flutter.dev/docs/development/ui/assets-and-images#specifying-assets)\nto specify assets, then [FlutterGen] will generate related dart files.  \nNo other specific configuration is required.  \n_Ignore duplicated._\n\n```yaml\n# pubspec.yaml\nflutter:\n  assets:\n    - assets/images/\n    - assets/images/chip3/chip.jpg\n    - assets/images/chip4/chip.jpg\n    - assets/images/icons/paint.svg\n    - assets/images/icons/dart@test.svg\n    - assets/json/fruits.json\n    - assets/flare/Penguin.flr\n    - assets/rive/vehicles.riv\n    - pictures/ocean_view.jpg\n```\n\nThese configurations will generate **`assets.gen.dart`** under the **`lib/gen/`** directory by default.\n\n#### Flavored assets\n\nFlutter supports\n[Conditionally bundling assets based on flavor](https://docs.flutter.dev/deployment/flavors#conditionally-bundling-assets-based-on-flavor).\nAssets are only available with flavors if specified.\n`flutter_gen` will generate the specified `flavors` for assets regardless the current flavor.\nThe `flavors` field accessible though `.flavors`, for example:\n\n```dart\nprint(MyAssets.images.chip4.flavors); // -\u003e {'extern'}\n```\n\n#### Excluding generating for assets\n\nYou can specify `flutter_gen \u003e assets \u003e exclude` using `Glob` patterns to exclude particular assets.\n\n```yaml\nflutter_gen:\n  assets:\n    exclude:\n      - folder-your-want-to-exclude/**\n      - specified-asset.jpg\n```\n\nSee more patterns with the `package:glob`.\n\n#### Generate for packages\n\nIf you want to generate assets for a package,\nuse `package_parameter_enabled` under `flutter_gen \u003e assets \u003e outputs`.\n\n```yaml\nflutter_gen:\n  assets:\n    outputs:\n      package_parameter_enabled: true # \u003c- Add this line.\n```\n\nThis would add the package constant to the generated class. For example:\n\n```dart\nclass Assets {\n  const Assets._();\n\n  static const String package = 'test';\n\n  static const $AssetsImagesGen images = $AssetsImagesGen();\n  static const $AssetsUnknownGen unknown = $AssetsUnknownGen();\n}\n```\n\nThen you can use assets with the package implicitly or explicitly:\n\n```dart\n// Implicit usage for `Image`/`SvgPicture`/`Lottie`.\nWidget build(BuildContext context) {\n  return Assets.images.icons.paint.svg(\n    width: 120,\n    height: 120,\n  );\n}\n```\nor\n```dart\n// Explicit usage for `Image`/`SvgPicture`/`Lottie`.\nWidget build(BuildContext context) {\n  return SvgPicture.asset(\n    Assets.images.icons.paint.path,\n    package: Assets.package,\n    width: 120,\n    height: 120,\n  );\n}\n```\n\n#### Generate directories path\n\nIf you want to generate the path of directories,\nuse `directory_path_enabled` under `flutter_gen \u003e assets \u003e outputs`.\n\n```yaml\nflutter_gen:\n  assets:\n    outputs:\n      directory_path_enabled: true # \u003c- Add this line.\n```\n\nThis would add the `path` getter to the generated directory class. For example:\n\n```dart\nclass $AssetsImagesGen {\n  const $AssetsImagesGen();\n\n  ///******///\n\n  /// Directory path: assets/images\n  String get path =\u003e 'assets/images';\n}\n```\n\n#### Including additional metadata\n\nAt build time, additional metadata may be included in the generated class, by using the\n`parse_metadata` option.\n\n```yaml\nflutter_gen:\n  parse_metadata: true # \u003c- Add this line (default: false)\n```\n\nFor image based assets, a new nullable `size` field is added to the\ngenerated class. For example:\n\n```dart\nAssetGenImage get logo =\u003e \n  const AssetGenImage('assets/images/logo.png', size: Size(209.0, 49.0));\n```\n\nWhich can now be used at runtime without parsing the information from the actual asset.\n\n```dart\nWidget build(BuildContext context) {\n  return Assets.images.logo.size!.width;\n}\n```\n\n#### Usage Example\n\n[FlutterGen] generates [Image](https://api.flutter.dev/flutter/widgets/Image-class.html) class if the asset is Flutter supported image format.\n\nExample results of `assets/images/chip.jpg`:\n\n- **`Assets.images.chip`** is an implementation of [`AssetImage class`](https://api.flutter.dev/flutter/painting/AssetImage-class.html).\n- **`Assets.images.chip.image(...)`** returns [`Image class`](https://api.flutter.dev/flutter/widgets/Image-class.html).\n- **`Assets.images.chip.provider(...)`** returns [`ImageProvider class`](https://api.flutter.dev/flutter/painting/ImageProvider-class.html).\n- **`Assets.images.chip.path`** just returns the path string.\n- **`Assets.images.chip.values`** just returns the values list.\n\n```dart\nWidget build(BuildContext context) {\n  return Assets.images.chip.image();\n}\n\nWidget build(BuildContext context) {\n  return Assets.images.chip.image(\n    width: 120,\n    height: 120,\n    fit: BoxFit.scaleDown,\n  );\n\nWidget build(BuildContext context) {\n  // Assets.images.chip.path = 'assets/images/chip3/chip3.jpg'\n  return Image.asset(Assets.images.chip.path);\n}\n\n```\n\nIf you do not want to generate `AssetGenImage`, set `flutter_gen \u003e integrations \u003e image` to `false`.\n\n```yaml\n# pubspec.yaml\nflutter_gen:\n  integrations:\n    image: false\n```\n\nIf you are using SVG images with [flutter_svg](https://pub.dev/packages/flutter_svg) you can use the integration feature.\n\n```yaml\n# pubspec.yaml\nflutter_gen:\n  integrations:\n    flutter_svg: true\n\nflutter:\n  assets:\n    - assets/images/icons/paint.svg\n```\n\n```dart\nWidget build(BuildContext context) {\n  return Assets.images.icons.paint.svg(\n    width: 120,\n    height: 120\n  );\n}\n```\n\n**Available Integrations**\n\n| Packages                                            | File extension             | Setting             | Usage                                     |\n|-----------------------------------------------------|----------------------------|---------------------|-------------------------------------------|\n| [flutter_svg](https://pub.dev/packages/flutter_svg) | .svg                       | `flutter_svg: true` | Assets.images.icons.paint.**svg()**       |\n| [rive](https://pub.dev/packages/rive)               | .riv                       | `rive: true`        | Assets.rive.vehicles.**rive()**           |\n| [lottie](https://pub.dev/packages/lottie)           | .json, .zip, .lottie, .tgs | `lottie: true`      | Assets.lottie.hamburgerArrow.**lottie()** |\n\n**Note:** For [lottie](https://pub.dev/packages/lottie) integration with `.lottie` and `.tgs` files, you must add a custom decoder via `decoder` parameter, see [lottie's document](https://pub.dev/packages/lottie#telegram-stickers-tgs-and-dotlottie-lottie) for more information.\n\nIn other cases, the asset is generated as String class.\n\n```dart\n// If don't use the Integrations.\nfinal svg = SvgPicture.asset(Assets.images.icons.paint);\n\nfinal json = await rootBundle.loadString(Assets.json.fruits);\n```\n\n[FlutterGen] also support generating other style of `Assets` class:\n\n```yaml\n# pubspec.yaml\nflutter_gen:\n  assets:\n    outputs: \n      # Assets.imagesChip\n      # style: camel-case\n\n      # Assets.images_chip\n      # style: snake-case\n\n      # Assets.images.chip (default style)\n      # style: dot-delimiter\n\nflutter:\n  assets:\n    - assets/images/chip.png\n```\n\nThe root directory will be omitted if it is either **`assets`** or **`asset`**.\n\n```\nassets/images/chip3/chip.jpg      =\u003e Assets.images.chip3.chip\nassets/images/chip4/chip.jpg      =\u003e Assets.images.chip4.chip\nassets/images/icons/paint.svg     =\u003e Assets.images.icons.paint\nassets/images/icons/dart@test.svg =\u003e Assets.images.icons.dartTest\nassets/json/fruits.json           =\u003e Assets.json.fruits\npictures/ocean_view.jpg           =\u003e Assets.pictures.oceanView\n```\n\n[Example of code generated by FlutterGen](https://github.com/FlutterGen/flutter_gen/blob/main/examples/example/lib/gen/assets.gen.dart)\n\n### Fonts\n\nJust follow the doc [Use a custom font](https://flutter.dev/docs/cookbook/design/fonts) to specify fonts, then [FlutterGen] will generate related dart files.  \nNo other specific configuration is required.  \n_Ignore duplicated._\n\n```yaml\n# pubspec.yaml\nflutter:\n  fonts:\n    - family: Raleway\n      fonts:\n        - asset: assets/fonts/Raleway-Regular.ttf\n        - asset: assets/fonts/Raleway-Italic.ttf\n          style: italic\n    - family: RobotoMono\n      fonts:\n        - asset: assets/fonts/RobotoMono-Regular.ttf\n        - asset: assets/fonts/RobotoMono-Bold.ttf\n          weight: 700\n```\n\nThese configurations will generate **`fonts.gen.dart`** under the **`lib/gen/`** directory by default.\n\n#### Generate for packages\n\nIf you want to generate fonts for a package,\nuse `package_parameter_enabled` under `flutter_gen \u003e fonts \u003e outputs`.\n\n```yaml\nflutter_gen:\n  fonts:\n    outputs:\n      package_parameter_enabled: true # \u003c- Add this line.\n```\n\nThis would add the package constant to the generated class. For example:\n\n```dart\nclass Fonts {\n  Fonts._();\n\n  static const String package = 'test';\n\n  static const String raleway = 'packages/$package/Raleway';\n  static const String robotoMono = 'packages/$package/RobotoMono';\n}\n```\n\n#### Usage Example\n\n```dart\nText(\n  'Hi there, I\\'m FlutterGen',\n  style: TextStyle(\n    fontFamily: FontFamily.robotoMono,\n    fontFamilyFallback: const [FontFamily.raleway],\n  ),\n)\n```\n\n[Example of code generated by FlutterGen](https://github.com/FlutterGen/flutter_gen/tree/main/examples/example/lib/gen/fonts.gen.dart)\n\n### Colors\n\n[FlutterGen] supports generating colors from [XML](examples/example/assets/color/colors.xml) format files.  \n_Ignore duplicated._\n\n```yaml\n# pubspec.yaml\nflutter_gen:\n  colors:\n    inputs:\n      - assets/color/colors.xml\n      - assets/color/colors2.xml\n```\n\n[FlutterGen] can generate a [Color](https://api.flutter.dev/flutter/material/Colors-class.html) class based on the `name` attribute and the color hex value.\nIf the element has the attribute `type`, then a specially color will be generated.\n\nCurrently supported special color types:\n\n- [MaterialColor](https://api.flutter.dev/flutter/material/MaterialColor-class.html)\n- [MaterialAccentColor](https://api.flutter.dev/flutter/material/MaterialAccentColor-class.html)\n\n\u003e Noticed that there is no official material color generation algorithm. The implementation is based on the [mcg](https://github.com/mbitson/mcg) project.\n\n```xml\n\u003ccolor name=\"milk_tea\"\u003e#F5CB84\u003c/color\u003e\n\u003ccolor name=\"cinnamon\" type=\"material\"\u003e#955E1C\u003c/color\u003e\n\u003ccolor name=\"yellow_ocher\" type=\"material material-accent\"\u003e#DF9527\u003c/color\u003e\n```\n\nThese configurations will generate **`colors.gen.dart`** under the **`lib/gen/`** directory by default.\n\n#### Usage Example\n\n```dart\nText(\n  'Hi there, I\\'m FlutterGen',\n  style: TextStyle(\n    color: ColorName.denim,\n  ),\n```\n\n[Example of code generated by FlutterGen](https://github.com/FlutterGen/flutter_gen/tree/main/examples/example/lib/gen/colors.gen.dart)\n\n## Credits\n\nThe material color generation implementation is based on [mcg](https://github.com/mbitson/mcg) and [TinyColor](https://github.com/bgrins/TinyColor).\n\n## Issues\n\nPlease file [FlutterGen] specific issues, bugs, or feature requests in our [issue tracker](https://github.com/FlutterGen/flutter_gen/issues/new).\n\nPlugin issues that are not specific to [FlutterGen] can be filed in the [Flutter issue tracker](https://github.com/flutter/flutter/issues/new).\n\n### Known Issues\n\n#### Bad State: No Element when using build_runner\nIf you get an error message like this:\n```\n[SEVERE] flutter_gen_runner:flutter_gen_runner on $package$:\n\nBad state: No element\n[SEVERE] Failed after 16.0s\n```\n\nThe you most likely have a customized `build.yaml` to configure the build runner. In that case, all you have to do is to add the `pubspec.yaml` as build source to your `build.yaml`\n\n```yaml\ntargets:\n  $default:\n    sources:\n      include:\n        - pubspec.yaml  # add this line\n        - ...\n```\n\nSee #268 for the corresponding issue discussion.\n\n### Error with [internationalization](https://docs.flutter.dev/development/accessibility-and-localization/internationalization)\n\nPlease try to remove `generate: true` in your `pubspec.yaml` and disable `synthetic-package` in your `l10n.yaml` like:\n\n```yaml\n# pubspec.yaml\nflutter:\n  generate: true \u003c--- ⚠️Remove this line⚠️\n```\n\n```yaml\n# l10n.yaml\narb-dir: lib/l10n\ntemplate-arb-file: app_en.arb\noutput-localization-file: app_localizations.dart\nsynthetic-package: false \u003c--- ⚠️Add this line⚠️\n```\n\nIf you get \n\n## Contributing\n\n**We are looking for co-developers.**\n\nIf you wish to contribute a change to any of the existing plugins in this repo,\nplease review our [contribution guide](https://github.com/FlutterGen/flutter_gen/blob/master/CONTRIBUTING.md)\nand open a [pull request](https://github.com/FlutterGen/flutter_gen/pulls).\n\n[build_runner]: https://pub.dev/packages/build_runner\n[fluttergen]: https://pub.dev/packages/flutter_gen\n","funding_links":["https://github.com/sponsors/wasabeef","https://github.com/sponsors/AlexV525"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttergen%2Fflutter_gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluttergen%2Fflutter_gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttergen%2Fflutter_gen/lists"}