Flutter: Why Colors.blue is a const value but Colors.blue.shade50 is not?

ยท

1 min read

While giving color values to a couple of circleavatars I realized that flutter gives error to the color shades but not to the colors?

children: [
           ๐Ÿคข             const CircleAvatar(backgroundColor: Colors.blue.shade50,),
           ๐Ÿ˜€             const CircleAvatar(backgroundColor: Colors.yellow,),
           ๐Ÿ˜€             CircleAvatar(backgroundColor: Colors.blueGrey.shade100,),
           ๐Ÿ˜€             const CircleAvatar(backgroundColor: Colors.grey,),
                      ],

const CircleAvatar(backgroundColor: Colors.blue.shade50,) gives error invalid constant error.

I found that Colors.blue.shade50 interprets as Colors.blue[50].

Operator [] returns list. Hence, do not meet the definition of const.

The other alternative is to use the actual color value.

image.png

As per the screenshot Colors.blue.shade50 = Colors.blue[50] = Color(0xFFE3F2FD).

So if we use const CircleAvatar(backgroundColor: Color(0xFFE3F2FD)), this will be a valid solution.

ย