Reuse custom color with custom name

I would suggest you create your own custom colors in another file:

class Colors {
  static const Color myCustomBlack = const Color(0x8A000000);
  static const Color white = const Color(0xFFFFFFFF);
}

but if you want to use them in your project, with CupertinoColors there is no conflict but with material Colors, you should either change the class name to something like MyColors which I think is better or you can hide the Colors class from the Material Library. if you want to use them just import the dart file and you are good to go.


You can use

color: MyColors.myCustomBlue

there is no way to extend the static members of Colors with your custom entries. You can include all Colors entries in your MyColors so you don't have to use multiple names.

You can even name your own set Colors, you just have to be careful to not import both at the same time (the IDE will warn about conflicts)

import 'package:my_package/colors.dart';
import 'package:flutter/material.dart' hide Colors;

Tags:

Dart

Flutter