Home > front end >  Flutter - Colors and Variable - Can we use a variable when using the [xxx] after the color?
Flutter - Colors and Variable - Can we use a variable when using the [xxx] after the color?

Time:03-28

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 ?

CodePudding user response:

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))
    );
  • Related