Home > Enterprise >  apply shade to flutter color variable
apply shade to flutter color variable

Time:07-31

I have defined a variable that holds a color value and when I setstate the screen, I always change this color. I want to use this color both as normal and shade100 in my application. However, my code is giving an error below.

late Color objeColor;
objeColor = Colors.orange;

Container(
   decoration: BoxDecoration(
   shape: BoxShape.circle,
   color: objeColor, // there is no error *******
   ),),


Container(
   decoration: BoxDecoration(
   shape: BoxShape.circle,
   color: objeColor.shade100, // there is error *******
),),

error

The getter 'shade100' isn't defined for the type 'Color'. Try importing the library that defines 'shade100', correcting the name to the name of an existing getter, or defining a getter or field named 'shade100'

CodePudding user response:

shade100 is a getter for MaterialColor. You can do

 late MaterialColor objeColor;
 ...
 color: objeColor.shade100 

CodePudding user response:

You can't change the shade of the color again in the widget tree. Though you've got plenty of other methods to manipulate the color with. For example, objeColor.withAlpha(75) looks exactly like Colors.orange.shade100 so it would perfectly fit your needs. In other usecases you can try to experiment with other methods, like withOpacity().

  • Related