Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can flutter extend colors?

Tags:

flutter

dart

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

like image 715
Zhuoyuan.Li Avatar asked Jul 02 '26 00:07

Zhuoyuan.Li


1 Answers

You can use like this,

extension colorExt on Color {
    Color get newColor => Color(0xDD000000);
}
   
class MyWidget extends StatelessWidget {
    Color color;
    @override
    Widget build(BuildContext context) {
      return Container(
        color: color.newColor,
        width: 50,
        height: 50
      );
    }
}
like image 133
Alperen Ekin Avatar answered Jul 05 '26 00:07

Alperen Ekin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!