flutter_gen
The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
Motivation
Using asset path string directly is not safe.
❌ Bad
What would happen if you made a typo?
⭕️ Good
We want to use it safely.
Installation
Homebrew
Works with MacOS and Linux.
Pub Global
Works with MacOS, Linux and Windows.
You might need to set up your path.
As a part of build_runner
- Add [build_runner] and [FlutterGen] to your package's pubspec.yaml file:
dev_dependencies:
build_runner:
flutter_gen_runner:
- Install [FlutterGen]
- Use [FlutterGen]
$ flutter packages pub run build_runner build
Usage
Run fluttergen
after the configuration pubspec.yaml
.
Configuration file
[FlutterGen] generates dart files based on the key flutter
and flutter_gen
of pubspec.yaml
.
Default configuration can be found here.
Available Parsers
Assets
Just follow the doc Adding assets and images#Specifying assets to specify assets, then [FlutterGen] will generate related dart files.
No other specific configuration is required.
Ignore duplicated.
These configurations will generate assets.gen.dart
under the lib/gen/
directory by default.
Usage Example
[FlutterGen] generates Image class if the asset is Flutter supported image format.
Example results of assets/images/chip.jpg
:
Assets.images.chip
is an implementation of AssetImage class
.
Assets.images.chip.image(...)
returns Image class
.
Assets.images.chip.path
just returns the path string.
If you are using SVG images with flutter_svg you can use the integration feature.
Available Integrations
Packages |
File extension |
Setting |
Usage |
flutter_svg |
.svg |
flutter_svg: true |
Assets.images.icons.paint.svg() |
flare_flutter |
.flr |
flare_flutter: true |
Assets.flare.penguin.flare() |
In other cases, the asset is generated as String class.
[FlutterGen] also support generating other style of Assets
class:
The root directory will be omitted if it is either assets
or asset
.
assets/images/chip3/chip.jpg => Assets.images.chip3.chip
assets/images/chip4/chip.jpg => Assets.images.chip4.chip
assets/images/icons/paint.svg => Assets.images.icons.paint
assets/json/fruits.json => Assets.json.fruits
pictures/ocean_view.jpg => Assets.pictures.oceanView
Example of code generated by FlutterGen
/// GENERATED CODE - DO NOT MODIFY BY HAND
/// *****************************************************
/// FlutterGen
/// *****************************************************
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/services.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
class $PicturesGen {
const $PicturesGen();
AssetGenImage get chip5 => const AssetGenImage('pictures/chip5.jpg');
}
class $AssetsFlareGen {
const $AssetsFlareGen();
FlareGenImage get penguin => const FlareGenImage('assets/flare/Penguin.flr');
}
class $AssetsImagesGen {
const $AssetsImagesGen();
AssetGenImage get chip1 => const AssetGenImage('assets/images/chip1.jpg');
AssetGenImage get chip2 => const AssetGenImage('assets/images/chip2.jpg');
$AssetsImagesChip3Gen get chip3 => const $AssetsImagesChip3Gen();
$AssetsImagesChip4Gen get chip4 => const $AssetsImagesChip4Gen();
$AssetsImagesIconsGen get icons => const $AssetsImagesIconsGen();
AssetGenImage get logo => const AssetGenImage('assets/images/logo.png');
AssetGenImage get profile => const AssetGenImage('assets/images/profile.jpg');
}
class $AssetsJsonGen {
const $AssetsJsonGen();
String get fruits => 'assets/json/fruits.json';
}
class $AssetsMovieGen {
const $AssetsMovieGen();
String get theEarth => 'assets/movie/the_earth.mp4';
}
class $AssetsUnknownGen {
const $AssetsUnknownGen();
String get unknownMimeType => 'assets/unknown/unknown_mime_type.bk';
}
class $AssetsImagesChip3Gen {
const $AssetsImagesChip3Gen();
AssetGenImage get chip3 =>
const AssetGenImage('assets/images/chip3/chip3.jpg');
}
class $AssetsImagesChip4Gen {
const $AssetsImagesChip4Gen();
AssetGenImage get chip4 =>
const AssetGenImage('assets/images/chip4/chip4.jpg');
}
class $AssetsImagesIconsGen {
const $AssetsImagesIconsGen();
SvgGenImage get fuchsia =>
const SvgGenImage('assets/images/icons/fuchsia.svg');
SvgGenImage get kmm => const SvgGenImage('assets/images/icons/kmm.svg');
SvgGenImage get paint => const SvgGenImage('assets/images/icons/paint.svg');
}
class Assets {
Assets._();
static const $AssetsFlareGen flare = $AssetsFlareGen();
static const $AssetsImagesGen images = $AssetsImagesGen();
static const $AssetsJsonGen json = $AssetsJsonGen();
static const $AssetsMovieGen movie = $AssetsMovieGen();
static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
static const $PicturesGen pictures = $PicturesGen();
}
class AssetGenImage extends AssetImage {
const AssetGenImage(String assetName)
: _assetName = assetName,
super(assetName);
final String _assetName;
Image image({
Key key,
ImageFrameBuilder frameBuilder,
ImageLoadingBuilder loadingBuilder,
ImageErrorWidgetBuilder errorBuilder,
String semanticLabel,
bool excludeFromSemantics = false,
double width,
double height,
Color color,
BlendMode colorBlendMode,
BoxFit fit,
AlignmentGeometry alignment = Alignment.center,
ImageRepeat repeat = ImageRepeat.noRepeat,
Rect centerSlice,
bool matchTextDirection = false,
bool gaplessPlayback = false,
bool isAntiAlias = false,
FilterQuality filterQuality = FilterQuality.low,
}) {
return Image(
key: key,
image: this,
frameBuilder: frameBuilder,
loadingBuilder: loadingBuilder,
errorBuilder: errorBuilder,
semanticLabel: semanticLabel,
excludeFromSemantics: excludeFromSemantics,
width: width,
height: height,
color: color,
colorBlendMode: colorBlendMode,
fit: fit,
alignment: alignment,
repeat: repeat,
centerSlice: centerSlice,
matchTextDirection: matchTextDirection,
gaplessPlayback: gaplessPlayback,
isAntiAlias: isAntiAlias,
filterQuality: filterQuality,
);
}
String get path => _assetName;
}
class SvgGenImage {
const SvgGenImage(this._assetName);
final String _assetName;
SvgPicture svg({
Key key,
bool matchTextDirection = false,
AssetBundle bundle,
String package,
double width,
double height,
BoxFit fit = BoxFit.contain,
AlignmentGeometry alignment = Alignment.center,
bool allowDrawingOutsideViewBox = false,
WidgetBuilder placeholderBuilder,
Color color,
BlendMode colorBlendMode = BlendMode.srcIn,
String semanticsLabel,
bool excludeFromSemantics = false,
Clip clipBehavior = Clip.hardEdge,
}) {
return SvgPicture.asset(
_assetName,
key: key,
matchTextDirection: matchTextDirection,
bundle: bundle,
package: package,
width: width,
height: height,
fit: fit,
alignment: alignment,
allowDrawingOutsideViewBox: allowDrawingOutsideViewBox,
placeholderBuilder: placeholderBuilder,
color: color,
colorBlendMode: colorBlendMode,
semanticsLabel: semanticsLabel,
excludeFromSemantics: excludeFromSemantics,
clipBehavior: clipBehavior,
);
}
String get path => _assetName;
}
class FlareGenImage {
const FlareGenImage(this._assetName);
final String _assetName;
FlareActor flare({
String boundsNode,
String animation,
BoxFit fit = BoxFit.contain,
Alignment alignment = Alignment.center,
bool isPaused = false,
bool snapToEnd = false,
FlareController controller,
FlareCompletedCallback callback,
Color color,
bool shouldClip = true,
bool sizeFromArtboard = false,
String artboard,
bool antialias = true,
}) {
return FlareActor(
_assetName,
boundsNode: boundsNode,
animation: animation,
fit: fit,
alignment: alignment,
isPaused: isPaused,
snapToEnd: snapToEnd,
controller: controller,
callback: callback,
color: color,
shouldClip: shouldClip,
sizeFromArtboard: sizeFromArtboard,
artboard: artboard,
antialias: antialias,
);
}
String get path => _assetName;
}
Fonts
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.
These configurations will generate fonts.gen.dart
under the lib/gen/
directory by default.
Usage Example
Example of code generated by FlutterGen
Colors
[FlutterGen] supports generating colors from XML format files.
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.
Currently supported special color types:
Noticed that there is no official material color generation algorithm. The implementation is based on the mcg project.
These configurations will generate colors.gen.dart
under the lib/gen/
directory by default.
Usage Example
Example of code generated by FlutterGen
Default Configuration
The following are the default settings.
The options you set in pubspec.yaml
will override the corresponding default options.
GitHub
https://github.com/FlutterGen/flutter_gen