Is there an existing issue for this?
Describe the problem
The generator for colors currently just has the option of writing every color as a static const Color on a holder class like this:
abstract final class ColorName {
static const Color platinum = Color(0xFFE5E5E5);
}
So each generated color is just a plain Color. There is no dedicated, project-specific color type.
This matters now that Dart has dot shorthands. A dot shorthand like .black resolves a static member against the context type. Because the generated colors are exposed as Color (not as members of their own type), there is no way to design an API that opts into them.
I'd like to declare a parameter whose type is my generated color set, then call it with a dot shorthand:
void setBackground(ColorName color);
setBackground(.platinum); // not possible today since ColorName isn't a Color subtype
To benefit from dot shorthands with the generated palette, the generated type needs to be a Color by inheritance, so that a value of the generated type can be passed anywhere a Color is expected, and, when an API purposely declares a parameter/return type as the generated type, callers can use .colorName dot shorthands.
There's currently no option to produce that shape.
Describe the solution
Add an opt-in style option under colors/outputs (mirroring the existing assets/outputs/style):
plain (default) — current behavior, unchanged.
wrapper-class — generate a class that extends Color with a forwarding const constructor,
exposing each normal color as a static const of the generated type.
flutter_gen:
colors:
outputs:
style: wrapper-class
class_name: AppColor
inputs:
- assets/color/colors.xml
Example generated output:
class AppColor extends Color {
const AppColor(super.value);
static const platinum = AppColor(0xFFE5E5E5);
}
Because AppColor is now a Color, it works seamlessly with Dart dot shorthands wherever a
parameter/return type is declared as AppColor, while still being assignable to Color:
final Color c = AppColor.platinum; // still a Color ✅
setBackground(.platinum); // dot shorthand ✅
Notes:
- Only normal colors change shape;
MaterialColor / MaterialAccentColor output is identical to the plain style.
- Fully backwards compatible since plain stays the default, so existing users are unaffected.
Additional context
No response
Code of Conduct
Is there an existing issue for this?
Describe the problem
The generator for colors currently just has the option of writing every color as a
static const Coloron a holder class like this:So each generated color is just a plain
Color. There is no dedicated, project-specific color type.This matters now that Dart has dot shorthands. A dot shorthand like .black resolves a static member against the context type. Because the generated colors are exposed as
Color(not as members of their own type), there is no way to design an API that opts into them.I'd like to declare a parameter whose type is my generated color set, then call it with a dot shorthand:
To benefit from dot shorthands with the generated palette, the generated type needs to be a
Colorby inheritance, so that a value of the generated type can be passed anywhere aColoris expected, and, when an API purposely declares a parameter/return type as the generated type, callers can use.colorNamedot shorthands.There's currently no option to produce that shape.
Describe the solution
Add an opt-in
styleoption undercolors/outputs(mirroring the existingassets/outputs/style):plain(default) — current behavior, unchanged.wrapper-class— generate a class thatextends Colorwith a forwarding const constructor,exposing each normal color as a
static constof the generated type.Example generated output:
Because
AppColoris now aColor, it works seamlessly with Dart dot shorthands wherever aparameter/return type is declared as
AppColor, while still being assignable toColor:Notes:
MaterialColor/MaterialAccentColoroutput is identical to the plain style.Additional context
No response
Code of Conduct