I am trying to toggle the color of a raised button. Initially the button should be blue and when it is pressed it turns to grey. Right now I have a bool value called pressAttention and it is set to false. I am using this to initially set this the false. When the button is pressed it toggles the pressAttention bool, but it seems that the widget is never updated again.
new RaisedButton(
child: new Text("Attention"),
textColor: Colors.white,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
color: pressAttention?Colors.grey:Colors.blue,
onPressed: () => doSomething("Attention"),
)
void doSomething(String buttonName){
if(buttonName == "Attention"){
if(pressAttention = false){
pressAttention = true;
} else {
pressAttention = false;
}
}
}
Go to your main. Inside the MaterialApp, find the ThemeData widget. Add the elevatedButtonTheme property inside and assign the ElevatedButtonThemeData(). Add the style property and change the color as you would change it for normal ElevatedButton. Place the ElevatedButton widget anywhere in your Flutter app and see.
You can custom the shape of an elevated button by using the shape parameter of the styleFrom method. Example: ElevatedButton( onPressed: () {}, child: const Text('Kindacode.com'), style: ElevatedButton. styleFrom( primary: Colors.
It is not possible to change default elevation color right now in Flutter. And in my opinion, it won't be, because of Material Design principles. Create a wrapper Container then wrap your Button Widget (that has no elevation) with the Container. You can tweak the BoxShadow however you want.
This button will need to be created in the build
of a State
of a StatefulWidget
, and the State must have a member variable bool pressAttention = false;
. As Edman suggests, you need to make state changes in a setState
callback for the Widget to redraw.
new RaisedButton(
child: new Text('Attention'),
textColor: Colors.white,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
color: pressAttention ? Colors.grey : Colors.blue,
onPressed: () => setState(() => pressAttention = !pressAttention),
);
If you want the button to change color for the pressed state you just need to use the "highlightColor" property in RaisedButton
RaisedButton(
onPressed: () {},
child: Text("Test"),
highlightColor: YOUR_PRESSED_COLOR, //Replace with actual colors
color: IDLE_STATE_COLOR,
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With