Home > Enterprise >  How can i call theme colour in constructor
How can i call theme colour in constructor

Time:02-08

var boxDecoration = BoxDecoration(
  borderRadius: BorderRadius.circular(10),
  color: Colors.grey.shade300,
  
  boxShadow: [
    BoxShadow(
        blurRadius: 15,
        offset: Offset(4, 4),
        color: Colors.grey.shade500,
        spreadRadius: 1),
    BoxShadow(
        blurRadius: 15,
        offset: Offset(-4, -4),
        color: Colors.white,
        spreadRadius: 1)
  ],
);

This is my constructor for the container, I want to change its color according to the system theme. Like different colors for the dark and light themes. How can I do that?

CodePudding user response:

I will prefer using method in this case,

BoxDecoration boxDecoration(BuildContext context) => BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Theme.of(context).colorScheme.primary,
        boxShadow: [...],
      );

And use it like decoration: boxDecoration(context),

  •  Tags:  
  • Related