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

Flutter – Colors and Variable – Can we use a variable when using the [xxx] after the color?

I have a simple question :

Here is the code :

BoxDecoration(
                          gradient: LinearGradient(
                              colors: [
                                Colors.blue[100]!,
                                Colors.blue[50]!,
                                Colors.blue[50]!,
                                Colors.blue[100]!
                              ],
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              stops: [0, 0.2, 0.5, 0.8]),
                          border:
                              Border.all(color: Colors.blue[100]!, width: 1.0),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),

I am creating a special container and I would like to be able to set the color as a parameter, so that I can have a blue, or green or purple container…
But since I use [xxx] after the color, it seems I can’t write something like myColor[100] where "myColor" would be the parameter. When using I could replace "myColor" with "Colors.blue", or "Colors.green" etc.

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

Is there a way to do this ?

>Solution :

Simply make a function that accepts List<Color>

  BoxDecoration _simpleBox(List<Color> color, Color borderColor) =>
    BoxDecoration(
      gradient: LinearGradient(colors: color),
      border: Border.all(color: borderColor, width: 1.0),
      borderRadius: const BorderRadius.all(Radius.circular(15))
    );
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