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

Issue

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.

Is there a way to do this ?

Solution

Simply make a function that accepts List<Color>

  BoxDecoration _simpleBox(List<Color> color, Color borderColor) {
    return BoxDecoration(
      gradient: LinearGradient(colors: color),
      border: Border.all(color: borderColor, width: 1.0),
      borderRadius: const BorderRadius.all(Radius.circular(15))
    );
  }

Answered By – jjkbm

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.