close
How to change background color of Elevated Button in Flutter from function?, change background color of Elevated Button in Flutter from function, change background color of elevated button, background color of elevated button, background color of elevated button

How to change background color of Elevated Button in Flutter from function?

Hello guys how are you all? Hope you all are fine. As we know RaisedButton is deprecated and the Elevated button is replacing Raised Button widget with its new features. Here in this tutorial, we are going to learn How to change background color of Elevated Button in Flutter from function?

How to change background color of Elevated Button in Flutter from function?

to change the background color of the Elevated Button in Flutter from the function all you need to Pass color as a parameter and use MaterialStateProperty.all<Color>(color) to specify the color.

  1. First Of all make boolean variable _flag and set default value as true.
  2. Then make Elivated Button.
  3. Now when anyone press this elevated button. We are going to use Setstate to change _flag default value.
  4. and as _flag value change we will change our primary color value in style property.
  5. Here is my full source code.

Source Code

           bool _flag = true;

           ElevatedButton.icon(
              onPressed: () => setState(() => _flag = !_flag),
              icon: Icon(
                _flag ? Icons.play_arrow : Icons.pause,
              ),
              label: Text(
                _flag ? 'Play' : 'Pause',
              ),
              style: ElevatedButton.styleFrom(
                primary: _flag ? Colors.lightGreen : Colors.blue,
              ),
            ),

OutPut

Faq

  1. How to change background color of Elevated Button in Flutter from function?

    to change the background color of the Elevated Button in Flutter from the function all you need to Pass color as a parameter and use MaterialStateProperty.all<Color>(color) to specify the color.

  2. change background color of Elevated Button in Flutter from function?

    to change the background color of the Elevated Button in Flutter from the function all you need to Pass color as a parameter and use MaterialStateProperty.all<Color>(color) to specify the color.

Summery

So it’s all About this tutorial. Hope this above-all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article


Posted

in

by

Comments

Leave a Reply

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