Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color shades are not constant values in Flutter

Tags:

flutter

When you use Colors.blue, for example this returns a constant Color object, but if you choose to use a shade instead, i.e. Colors.blue[300], then this object is NOT constant. This is important, for example, when you have a method that takes an optional Color parameter, whose default value must be constant. So how do we make a Color shade constant?

static const Color mainColor = Colors.blue \\All good!
static const Color shade = Colors.blue[400] \\ERROR: Const variables must be initialized with a constant value
like image 779
Arkay Avatar asked Jun 07 '19 12:06

Arkay


1 Answers

So how do we make a Color shade constant?

You can't. To choose a specific shade, you should use the [] operator, which is just like calling a method, and since the value a method returns varies on runtime, the value returned from a method call cannot be use as constant.

This is important, for example, when you have a method that takes an optional Color parameter, whose default value must be constant.

If your situation is as simple as this one, just use the real value of Colors.blue[400], which is Color(0xFF42A5F5).

like image 87
Hugo Passos Avatar answered Nov 09 '22 11:11

Hugo Passos