Can flutter extend colors?

Viewed 1773

I'm tired of having to build a separate class to store color values. I hope to extend Colors and use Colors.aaa directly.

I wrote the following code:

extension colorExt on Colors {
  static const Color cementTwo = const Color(0xff999990);
  static Color aaa = Color(0xDD000000);
 
}

But it does not work. Can someone tell me why and how to correctly implement my needs

1 Answers

You can use like this,

extension colorExt on Color {
    Color get aaa => Color(0xDD000000);
}
   
class MyWidget extends StatelessWidget {
    Color color;
    @override
    Widget build(BuildContext context) {
      return Container(
        color: color.aaa,
        width: 50,
        height: 50
      );
    }
}
Related