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.
- First Of all make boolean variable _flag and set default value as true.
- Then make Elivated Button.
- Now when anyone press this elevated button. We are going to use Setstate to change _flag default value.
- and as _flag value change we will change our primary color value in style property.
- 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
- 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.
- 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
- Flutter Error: MissingPluginException No implementation found for method
- How To Download File From URL And Save in Local Storage In Flutter
- Error: Could not find included file ‘Generated.xcconfig’ in search paths (in target ‘Runner’)
- Exception thrown while unbinding, java.lang.IllegalArgumentException: Service not registered in Flutter
- type ‘List‘ is not a subtype of type ‘List‘ Flutter
Leave a Reply