Reputation: 469
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
Upvotes: 15
Views: 5234
Reputation: 8427
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)
.
Upvotes: 18