How to set color in flutter from slider?

Issue

I have a list of colors
const bgcolor = ['Colors.red','Colors.green','Colors.blue','Colors.purple'];
I want to change the color as I slide forward. I can print the color but I want to set it in a container as color:…
The value means double _value = 0;

 child: Slider(
                      value: _value,
                      label: _emojis[_value.toInt()],
                      min: 0.0,
                      max: 3.0,
                      divisions: 3,
                      onChangeStart: (double value) {},
                      onChangeEnd: (double value) {
                        print('${bgcolor[_value.toInt()]}');
                      },
                      onChanged: (double value) {
                        setState(() {
                          _value = value;
                        });

Solution

You will need to create another variable that holds the selected color or set the color property of your container to bgcolor[_value].

Container(
                  color: bgcolor[_value],
                  child: Slider(
                    ...
                  ),
                ),

Answered By – Joe Muller

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *