Aus öffentlichen Paketdaten erstellte Orientierung.
Flutter Gen Runner ist ein Codegenerator für Flutter, der automatisch typensicheren Dart-Code aus Assets, Schriftarten, Farben und anderen Ressourcen erstellt. Er beseitigt die Notwendigkeit von stringbasierten Referenzen, reduziert Laufzeitfehler und beschleunigt die Entwicklung. Durch die Generierung von Konstanten und Aufzählungen gewährleistet er Typensicherheit und verbessert die Wartbarkeit des Codes. Das Werkzeug lässt sich nahtlos in Flutter-Projekte integrieren und unterstützt mehrere Plattformen wie Android, iOS, macOS, Windows und Linux.
Anwendungsfälle
Generieren sicherer Typ-Referenzen für Ressourcen
Automatisierung der Erstellung von Schriftart- und Farbkonstanten
Verringern der Verwendung von zeichenkettenbasierten APIs
Verbesserung der Codezuverlässigkeit durch Compile-time-Prüfungen
UI-Entwicklungswerkzeuge optimieren
Hauptmerkmale
Typsichere Ressourcenerzeugung
Unterstützt Ressourcen, Schriftarten, Farben und mehr
Kompilierzeitfehlervermeidung
Nahtlose Integration mit Flutter
Plattformübergreifende Kompatibilität
Geeignet für
Entwickler, die Typsicherheit in Benutzeroberflächenressourcen suchen
Teams, die darauf abzielen, string-basierte Fehler zu reduzieren
Projekte, die skalierbare Vermögensverwaltung erfordern
Flutter-Apps mit umfangreichen Design-Systemen
CI/CD-Pipelines, die automatisierte Codegenerierung benötigen
Hinweise
Erfordert Einrichtung in der pubspec.yaml
Benötigt Build-Runner-Ausführung
Kann die anfängliche Projektkonfiguration erhöhen
Nicht geeignet für dynamisches Ressourcenladen
Abhängig von korrekten Asset-Pfaden in der Konfiguration
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
Widgetbuild(BuildContext context) {
returnImage.asset('assets/images/profile.jpeg');
}
// The following assertion was thrown resolving an image codec:// Unable to load asset: assets/images/profile.jpeg
FlutterGen also supports dart pub workspaces.
Run build_runner from the workspace root, and make sure each workspace member
that should be built has resolution: workspace in its pubspec.yaml.
FlutterGen will resolve each package from the active build target instead of the
process working directory, so package-local pubspec.yaml configuration and
output paths continue to work in workspace builds.
If generated source files were removed manually while .dart_tool/build is
still present, run dart run build_runner clean from the workspace root before
running build --workspace again. The current post-process builder flow can
re-materialize files reliably after a clean build, but a warm incremental build
may skip unchanged manifests.
For workspace builds, use Dart >=3.7.0 together with build_runner >=2.12.0.
Use --workspace to treat the config file as a workspace root pubspec and run
generation for each listed workspace member. Without --workspace, the command
generates for exactly one package.
When --workspace is enabled, the root pubspec.yaml is used only to discover
workspace members from its workspace: section. FlutterGen then switches to
each member package and loads that package's own pubspec.yaml and, if
present, its local build.yaml.
That means workspace-wide command execution behaves like this:
Root pubspec.yaml: selects which packages to visit.
Member pubspec.yaml: provides flutter and flutter_gen configuration.
Member build.yaml: overrides generator options for that member only.
The --build option is only available in single-package mode. In workspace
mode, put any overrides in each package's local build.yaml instead.
Configuration file
FlutterGen generates dart files based on the key flutter and flutter_gen of pubspec.yaml.
Default configuration can be found here.
When using build_runner, you can also configure generator options in the
target build.yaml. These builder options are applied on top of the
pubspec.yaml configuration for the current target.
# build.yaml# ...targets:$default:builders:flutter_gen_runner:# or flutter_genoptions:output:lib/build_gen/# Optional (default: lib/gen/)line_length:120# Optional
# pubspec.yamlflutter:assets:-assets/images/-assets/images/chip3/chip.jpg-assets/images/chip4/chip.jpg-path:assets/images/icons/paint.svg-path:assets/images/icons/transformed.svgtransformers:-package:vector_graphics_compiler-assets/images/icons/dart@test.svg-assets/json/fruits.json-assets/flare/Penguin.flr-assets/rive/vehicles.riv-pictures/ocean_view.jpg# Also include assets from deferred components# https://docs.flutter.dev/perf/deferred-componentsdeferred-components:-name:myDeferredComponentassets:-assets/images/another_image.jps-assets/videos/a_large_video.mp4
These configurations will generate assets.gen.dart under the lib/gen/ directory by default.
Flavored assets
Flutter supports
Conditionally bundling assets based on flavor.
Assets are only available with flavors if specified.
flutter_gen will generate the specified flavors for assets regardless the current flavor.
The flavors field accessible though .flavors, for example:
You can use parse_animation to generate more animation details.
It will automatically parse all animation information for GIF and WebP files,
including frames, duration, etc. As this option significantly increases generation time,
The option is disabled by default; enabling it will significantly increase the generation elapse.
flutter_gen:images:parse_animation:true# <- Add this line (default: false)# This option implies parse_metadata: true when parsing images.
For GIF and WebP animation, several new nullable field is added to the
generated class. For example:
If you are using SVG images with flutter_svg you can use the integration feature. This feature also supports using vector_graphics_compiler transformer and the produced code will use the AssetBytesLoader for such transformed assets.
Note: For lottie integration with .lottie and .tgs files, you must add a custom decoder via decoder parameter, see lottie's document for more information.
In other cases, the asset is generated as String class.
// If don't use the Integrations.final svg = SvgPicture.asset(Assets.images.icons.paint);
final json = await rootBundle.loadString(Assets.json.fruits);
FlutterGen also support generating other style of Assets class:
Just follow the doc Use a custom font to specify fonts, then FlutterGen will generate related dart files.
No other specific configuration is required. Ignore duplicated.
FlutterGen can generate a Color class based on the name attribute and the color hex value.
If the element has the attribute type, then a specially color will be generated.
[SEVERE] flutter_gen_runner:flutter_gen_runner on $package$:
Bad state: No element
[SEVERE] Failed after 16.0s
The 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
targets:$default:sources:include:-pubspec.yaml# add this line-...