Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to apply shade of color as default container color with custom widget in flutter?

I am learning custom widget for reusable..

here i want to make a container with default light color of grey but while applying Colors.grey.shade200 its showing error

looks like Colors.grey is constant but .shade200 is not constant value

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so how can we use shade of color as constant …

class RoundedContainer extends StatelessWidget {
  
  final Color backgroundColor;
 

  const RoundedContainer({
    super.key,
    
    this.backgroundColor= Colors.grey.shade200,//showing error
   
  });

>Solution :

You cannot do above because: Colors.grey.shade200 value is not a compile-time constant. You can do it like this you can make the backgroundColor nullable and later assign the shade if the color is not available.

class RoundedContainer extends StatelessWidget {
  final Color? backgroundColor;

  
  const RoundedContainer({
    Key? key,
    required this.backgroundColor,
  }) : super(key: key);

 

  @override
  Widget build(BuildContext context) {
    // Rest of your build method
    return Container(
      color: backgroundColor ?? Colors.grey.shade200
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading