simple dart code generator that can help you to generate flutter extension from JSON code with different formats
you should install it to pub global as command line tool
dart pub global activate theme_generator_x
-
input
- path to your input JSON file -
output
- path to your output.dart
file -
class_name
- name of generated extension class, e.g. AppThemeDataColorsX -
use_dark
- flag if you need to generate dark colors too. false by default -
keys_rename
- how to rename JSON keysAvailable options
:camel_case
- camelCaseoriginal
- original namesnake_case
- snake_case
you can use different JSON key-value structures
{
"primary": "#f6f4da",
"secondary": "0xff9e9e9e",
"some_color": "#ff000011"
}
array of colors
0 item
- light color1 item
- dark color
{
"composite": [
"#f6f4da",
"0xff9e9e9e"
]
}
{
"test_color_simple": {
"light": "#000011"
},
"test_color": {
"dark": "#656213",
"light": "#000011"
}
}
also you can generate array of colors
{
"simple_map_of_array": {
"light": [
"#656213",
"#000011"
]
},
"simple_map_of_array_dark": {
"light": [
"#995577",
"#000011"
],
"dark": [
"#110022",
"#000000"
]
}
}
theme_generator_x colors --input example/test_schema.json --output example/output.dart --class_name AppThemeDataColorsX
of course you can use absolute or relative paths to the files
theme_generator_x colors --input /Users/jWinterDay/Documents/app_theme_asset.json --output app_theme_color.dart --class_name AppThemeDataColorsX
now you have the extensions and can use it in the flutter app
MaterialApp(
...
theme: ThemeData(
useMaterial3: true,
).copyWith(
extensions: <ThemeExtension<dynamic>>[
lightAppThemeDataColorsXData(), // generated extension function
],
),
...
)