How To Change Background Color Of Elevated Button In Flutter From Function?
Solution 1:
You can style ElevatedButton by using the styleFrom static method or the ButtonStyle class. The first one is more convenience than the second one.
Using styleFrom to style an ElevatedButton:
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom({
Color primary, // set the backgroundcolorColor onPrimary,
Color onSurface,
Color shadowColor,
double elevation,
TextStyle textStyle,
EdgeInsetsGeometry padding,
Size minimumSize,
BorderSide side,
OutlinedBorder shape,
MouseCursor enabledMouseCursor,
MouseCursor disabledMouseCursor,
VisualDensity visualDensity,
MaterialTapTargetSize tapTargetSize,
Duration animationDuration,
bool enableFeedback
}),
),
Example:
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.purple,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
textStyle: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold)),
),
Using ButtonStyle to style an ElevatedButton:
style: ButtonStyle({
MaterialStateProperty<TextStyle> textStyle,
MaterialStateProperty<Color> backgroundColor,
MaterialStateProperty<Color> foregroundColor,
MaterialStateProperty<Color> overlayColor,
MaterialStateProperty<Color> shadowColor,
MaterialStateProperty<double> elevation,
MaterialStateProperty<EdgeInsetsGeometry> padding,
MaterialStateProperty<Size> minimumSize,
MaterialStateProperty<BorderSide> side,
MaterialStateProperty<OutlinedBorder> shape,
MaterialStateProperty<MouseCursor> mouseCursor,
VisualDensity visualDensity,
MaterialTapTargetSize tapTargetSize,
Duration animationDuration,
bool enableFeedback
})
Example
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
padding: MaterialStateProperty.all(EdgeInsets.all(50)),
textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30))),
),
Solution 2:
Pass color as parameter and use MaterialStateProperty.all<Color>(color) to specify the color.
buildPlayButton(color: Colors.red, soundNumber: 1)
Expanded buildPlayButton({Color color, int soundNumber}){
return Expanded(
child: ElevatedButton(
onPressed: () {
playSound(soundNumber);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(color),
),
),
);}
Sample button
In general
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red, // backgroundonPrimary: Colors.yellow, // foreground
),
onPressed: () {},
child: Text('ElevatedButton with custom foreground/background'),
)
Sample button
Reference:
Solution 3:
ElevatedButton(onPressed: resetHandler,
child: Text("button"),
style: ElevatedButton.styleFrom(primary: Colors.amber),),
Solution 4:
You have 3 Options to change the background color :
ElevatedButton.styleFrom : If you just want to change the background color and foreground color irrespective of the states then you can do as given below.
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red, // backgroundonPrimary: Colors.white, // foreground
),
onPressed: () { },
child: Text('custom foreground/background'),
)
MaterialStateProperty.all : to override a ElevatedButtons default background(text/icon) color for all states.
ElevatedButton(style:
ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
onPressed: () { },
child: Text('custom foreground/background'),
));
MaterialStateProperty.resolveWith : By default, the elevated button inherits a blue color. We can tweak the default style using the style parameter and ButtonStyle class. Button has different states such as pressed, disabled, hovered etc. You can change the style for each state. In the below snippet, the default color of the button changes to green when it is pressed.
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Colors.green;
returnnull; // Use the component's default.
},
),
),
)
Solution 5:
Screenshot:
Code:
class _MyState extends State<MyPage> {
bool _flag = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => setState(() => _flag = !_flag),
child: Text(_flag ? 'Red' : 'Green'),
style: ElevatedButton.styleFrom(
primary: _flag ? Colors.red : Colors.teal, // This is what you need!
),
),
),
);
}
}
Post a Comment for "How To Change Background Color Of Elevated Button In Flutter From Function?"