flex_theme_gen 1.0.5
flex_theme_gen: ^1.0.5 copied to clipboard
Generate Flutter theme code from design tokens (YAML/JSON).
π Flutter Theme Generator #
Language: English | δΈζζζ‘£ π¨π³
A powerful Flutter theme generation tool based on YAML design tokens,
capable of automatically generating typography, colors, tokens, and Tailwind-style widget extensions.
It is designed for design systems that need to maintain consistency between design and code.
β¨ Features #
- π§± Generate color, font, spacing, radius, shadow, and border tokens
- π¨ Support light / dark theme switching
- π§© Tailwind-like chainable widget extensions (
.twBg(),.twRadius4(),.twBorder2()) - π‘ Automatically read from YAML configuration
- βοΈ Fully integrated with
melosand CI environments - π Support for custom fonts (via Flutter assets)
π Installation #
Add to your dev_dependencies:
dev_dependencies:
flex_theme_gen: ^1.0.4
Then activate globally (optional):
dart pub global activate flex_theme_gen
You can then run it globally with:
gen_flex_theme build
or from project scripts:
dart run flex_theme_gen:gen_flex_theme build
π Configuration (in pubspec.yaml) #
Add the following under the root of your design system / resource module (e.g., res/):
flex_theme_gen:
input: design_tokens.yaml
output: lib/gen
This means:
- input β path to your token definition file (YAML or JSON)
- output β where generated Dart files will be written
π§© design_tokens.yaml Example #
tokens:
prefix: tw # prefix for generated chain methods
spacing:
4: 4
8: 8
12: 12
16: 16
radius:
4: 4
8: 8
12: 12
shadow:
1: 2
2: 4
misc:
opacityDisabled: 0.5
border:
default:
colorToken: colorFF33A9FF
width: 1
2: 2
4: 4
6: 6
colors:
light:
colorFFFFFF: "#FFFFFF"
color000000: "#000000"
colorFF33A9FF: "#33A9FF"
dark:
colorFFFFFF: "#000000"
color000000: "#FFFFFF"
colorFF33A9FF: "#33A9FF"
typography:
fonts:
default:
family: InterRes
size: 16
weight: 400
lineHeight: 1.5
P2_B:
family: InterRes
size: 14
weight: 700
π€ Load Custom Fonts Example #
In pubspec.yaml:
flutter:
fonts:
- family: InterRes
fonts:
- asset: assets/fonts/Inter_24pt-Regular.ttf
weight: 400
- asset: assets/fonts/Inter_24pt-Bold.ttf
weight: 700
You can then use the generated text styles directly:
Text(
'Sign Up',
style: TextStyles.normal_P2_B.copyWith(
color: colors.colorFFFFFFFF,
),
)
.twPHx8()
.twPVy6()
.twBg(colors.colorFF33A9FF)
.twRounded4()
.onTap(() {
print('Sign up tapped');
}),
π§ Generated Files #
After running gen_flex_theme build, the tool will generate files under your output folder:
lib/gen/
βββ app_colors.g.dart
βββ tokens.g.dart
βββ tw_alias.g.dart
βββ fonts.g.dart
βββ tw_font.g.dart
βββ typography.g.dart
βββ typography_resolved.g.dart
π§± Integration with Melos (Optional) #
You can automate theme generation during bootstrap:
melos:
command:
bootstrap:
hooks:
pre: |
flutter clean
fvm dart run scripts/clean_generate_dart_file.dart || true
melos exec --scope="res" -- gen_flex_theme build
This ensures your res module automatically generates theme files every time you run:
This ensures your res module automatically generates theme files every time you run:
βοΈ CLI Commands #
| Command | Description |
|---|---|
| gen_flex_theme build | One-time generation |
| gen_flex_theme watch | Watch mode (auto re-generate on file change) |
π§© Example Project Structure #
project_root/
βββ modules/
β βββ res/
β β βββ design_tokens.yaml
β β βββ lib/gen/ # Generated theme files
β β βββ pubspec.yaml
β βββ login/
β βββ sport/
β βββ ...
βββ melos.yaml
π License #
This project is released under the MIT License.
π¬ Feedback & Contribution #
Contributions, bug reports, and feature requests are welcome!
Feel free to open an issue or pull request on GitHub.