Skip to content

[FR]: add wrapper-class output style so generated colors are a Color subtype (usable with dot shorthands) #765

@ugoostanleyibe

Description

@ugoostanleyibe

Is there an existing issue for this?

  • I have searched the existing issues

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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions