how to add custom color to flutter?

Why there’s no underlying technique that changes over a hex an incentive into a MaterialColor variable, we’ll never know

Image for post
A screenshot of Material Design Colors

Despite the fact that Flutter is unquestionably an incredible decision of a system with regards to cross-stage portable and web advancements, its ability for shading control is in reality a long way from significance.

When we make a fresh out of the plastic new Flutter venture by running $ futter make myapp on Terminal, the absolute first and the most straightforward customization we can give a shot should adjust the shading subject of the application… isn’t that so? No!

Image for post
Color theming of the application in the new project

The primarySwatch boundary — which is answerable for painting significant pieces of the application (top route bar as the most regular model) which follows Material Design worldview — never acknowledges factors other than MaterialColor type. That is very wrecked on the grounds that we as a whole understand what sort of shading assortment Google’s Material Concept can give us: the normal, worn out lethargic, dull, pre-characterized colors like golden, indigo, limeBlue, deepPurple, and straightforward!

Appointing my number one shading esteems (for example #174378) into this named boundary utilizing a couple of lines of Flutter’s inherent strategies is close to incomprehensible on the off chance that it is conceivable by any means. We totally need a utility technique which creates custom MaterialColor object from the hex whole number like above. Thus this stunt.

As an earlier notification: what I will do is straightforwardly credited from the constructor of MaterialColor. We can’t utilize it with no guarantees except for we can’t dodge it either 🙂

MaterialColor(int primary, Map<int, Color> swatch)

More or less, given the correct shading esteem (which we as of now have) and a pattern of various shades, we can get it going, much the same as that

MaterialColor createMaterialColor(Color color) {
  List strengths = <double>[.05];
  Map swatch = <int, Color>{};
  final int r =, g =, b =;

  for (int i = 1; i < 10; i++) {
    strengths.add(0.1 * i);
  strengths.forEach((strength) {
    final double ds = 0.5 - strength;
    swatch[(strength * 1000).round()] = Color.fromRGBO(
      r + ((ds < 0 ? r : (255 - r)) * ds).round(),
      g + ((ds < 0 ? g : (255 - g)) * ds).round(),
      b + ((ds < 0 ? b : (255 - b)) * ds).round(),
  return MaterialColor(color.value, swatch);

This code is essentially obvious. The distinctive shade estimations of pattern ought to continuously spread out. The limit of 0.5 in Line #10 is significant on the grounds that the real shading spoke to by another MaterialColor item should be indistinguishable from its own shade500.

Presently you should have the option to utilize it as here.

primarySwatch: createMaterialColor(Color(0xFF174378))

Also, presto! We made it work. Yet, how can this return esteem precisely resemble? It’s fundamentally something very similar as underneath, yet is a gathered thing with no manual estimation which shows up so irritating to our large personalities 🙂

Image for post

You might don’t want to do this.


It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment