A code generator for Flutter
Project description
The Flutter code generator for your assets, fonts, colors,… to increase your productivity.
Inspired by FlutterGen.
Installation
Python
Window
$ https://www.python.org/downloads/
Mac OS
$ brew install python3
FlutterGen
Works with MacOS and Windows.
Window
$ pip install flutter_gen
Mac OS
$ pip3 install flutter_gen
Update via command line:
$ pip3 install -U flutter_gen
Usage
Run flutter_gen after the configuration pubspec.yaml.
$ flutter_gen -h
$ flutter_gen watch
Android Studio Plugin
Configuration file
FlutterGen generates dart files based on the key flutter of pubspec.yaml.
Default configuration can be found here.
# pubspec.yaml
# ...
flutter:
uses-material-design: true
assets:
- assets/images/
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Italic.ttf
style: italic
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.
# pubspec.yaml
flutter:
assets:
- assets/images
These configurations will generate images.g.dart under the lib/generated/ directory by default.
Usage Example
FlutterGen generates Image class if the asset is Flutter supported image format.
Example results of assets/images/chip.jpg:
Images.chipis an implementation ofAssetImage class.Images.chip.image(...)returnsImage class.Images.chip.pathjust returns the path string.
Widget build(BuildContext context) {
return Image(image: R.images.chip);
}
Widget build(BuildContext context) {
return R.images.chip.image(
width: 120,
height: 120,
fit: BoxFit.scaleDown,
);
Widget build(BuildContext context) {
// Images.chip.path = 'assets/images/chip3/chip3.jpg'
return Image.asset(R.images.chip.path);
}
If you are using SVG images with flutter_svg you can use as:
Widget build(BuildContext context) {
return R.images.paint.svg(
width: 120,
height: 120
);
}
Example of code generated by FlutterGen
/// DO NOT EDIT. This is code generated via flutter_gen
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
const _assetsImagePath = 'assets/images';
class Images {
static AssetGenImage get chip => const AssetGenImage('$_assetsImagePath/chip.png');
static SvgGenImage get paint => const SvgGenImage('$_assetsImagePath/paint.svg');
}
class AssetGenImage extends AssetImage {
const AssetGenImage(String assetName) : super(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;
}
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.
# pubspec.yaml
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: assets/fonts/RobotoMono-Regular.ttf
- asset: assets/fonts/RobotoMono-Bold.ttf
weight: 700
These configurations will generate fonts.g.dart under the lib/generated/ directory by default.
Usage Example
Text(
'Hi there, I\'m FlutterGen',
style: TextStyle(
fontFamily: R.fonts.robotoMono,
fontFamilyFallback: const [R.fonts.raleway],
),
Example of code generated by FlutterGen
/// DO NOT EDIT. This is code generated via flutter_gen
class FontFamily {
FontFamily._();
static const String raleway = 'Raleway';
static const String robotoMono = 'RobotoMono';
}
Colors
FlutterGen supports generating colors from TXT format files.
Ignore duplicated.
FlutterGen can generate a Color class based on the color hex value.
#F5CB84
#955E1C
These configurations will generate colors.g.dart under the lib/generated/ directory by default.
Usage Example
Text(
'Hi there, I\'m FlutterGen',
style: TextStyle(
color: R.colors.hexF5CB84,
),
Example of code generated by FlutterGen
import 'package:flutter/material.dart';
class ColorName {
ColorName._();
static const Color hexF5CB84 = Color(0xFFF5CB84);
static const Color hex955E1C = Color(0xFF955E1C);
}
Issues
Please file FlutterGen specific issues, bugs, or feature requests in our issue tracker.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flutter_gen-2.2.0.tar.gz.
File metadata
- Download URL: flutter_gen-2.2.0.tar.gz
- Upload date:
- Size: 32.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62738e95c7cbfcc9bcea0eebac1058bda53031219b3c600990feff7f0cc09cd0
|
|
| MD5 |
0de16ad319da6cbcf212fc43683d7250
|
|
| BLAKE2b-256 |
cb22fd2ec6231972918fe9976993b10e212bf0f651acc72b9943450cdc039159
|
File details
Details for the file flutter_gen-2.2.0-py3-none-any.whl.
File metadata
- Download URL: flutter_gen-2.2.0-py3-none-any.whl
- Upload date:
- Size: 46.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
498920e8a3d36b31aa2e74c6edc467e428282688e0274721c54d2e395a9ca7b3
|
|
| MD5 |
bb9ac639d2d0d5e420763854362389d8
|
|
| BLAKE2b-256 |
c7c1abaa9d887b48364f69a4487550f4b7212920d03940b43fb75a83f0d34562
|