Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the ElevatedButton color or shadow Flutter

I was using the RaisedButton untill the Flutter deprecated it and we couldn't use it anymore. There was a suggestion saying "Use ElevatedButton instead". So I tried using it but I couldn't see the properties like color, elevation, focusColor etc.

So how can I personalize an ElevatedButton?

like image 506
Taba Avatar asked Mar 09 '21 14:03

Taba


People also ask

How do you customize an ElevatedButton in Flutter?

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.

How do you change the TextButton color when pressed in Flutter?

To change the Text Button color in Flutter, simply add the style parameter inside the Text Button and assign the TextButton. styleFrom() with the primary property set to any color of your choice.

How to change the color of elevated Button in flutter?

Now, let’s check how to change the color of the elevated button in flutter. By default, the elevated button inherits a blue color. We can tweak the default style using the style parameter and ButtonStyle class. See the following snippet.

How to change the shadow color of an elevated Button?

The styling of the Elevated Button is done with the help of ButtonStyle class. The shadowColor property of the ButtonStyle class can be used to change the shadow color.

What happened to flatbutton and raisedbutton in flutter?

Flutter team has recently deprecated t he FlatButton , RaisedButton and OutlineButton widgets. Although we don’t use those buttons, yet in their place we can use TextButton , ElevatedButton , and OutlinedButton. However, the ElevatedButton comes with default blue color.

Why do we use elevatedbutton instead of raisedbutton in HTML?

So, you have to use ElevatedButton instead of RaisedButton. On ElevatedButton, there is the provision of style property that is used to style the button. See the example below to know more about it. To change Background Color, Broder Radius, Border, Elevation, and Padding of Button.


2 Answers

To change the properties of ElevatedButton you should be using the style: property like so:

ElevatedButton(
  style: ElevatedButton.styleFrom(
    primary: Colors.blue, //button's fill color
    onPrimary: Colors.red, //specify the color of the button's text and icons as well as the overlay colors used to indicate the hover, focus, and pressed states
    onSurface: Colors.orange, //specify the button's disabled text, icon, and fill color
    shadowColor: Colors.black, //specify the button's elevation color
    elevation: 4.0, //buttons Material shadow
    textStyle: TextStyle(fontFamily: 'roboto'), //specify the button's text TextStyle
    padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 8.0, left: 8.0), //specify the button's Padding
    minimumSize: Size(20, 40), //specify the button's first: width and second: height
    side: BorderSide(color: Colors.yellow, width: 2.0, style: BorderStyle.solid), //set border for the button
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(35.0)), // set the buttons shape. Make its birders rounded etc
    enabledMouseCursor: MouseCursor.defer, //used to construct ButtonStyle.mouseCursor
    disabledMouseCursor: MouseCursor.uncontrolled, //used to construct ButtonStyle.mouseCursor
    visualDensity: VisualDensity(horizontal: 0.0, vertical: 0.0), //set the button's visual density
    tapTargetSize: MaterialTapTargetSize.padded, // set the MaterialTapTarget size. can set to: values, padded and shrinkWrap properties
    animationDuration: Duration(milliseconds: 100), //the buttons animations duration
    enableFeedback: true, //to set the feedback to true or false
    alignment: Alignment.bottomCenter, //set the button's child Alignment
  ),
    onPressed: () => {} , //set both onPressed and onLongPressed to null to see the disabled properties
    onLongPress: () => {}, //set both onPressed and onLongPressed to null to see the disabled properties
    child: Text('ElevatedButton')
),
like image 103
Taba Avatar answered Oct 12 '22 18:10

Taba


  • Change individual button color. Use style property.

    ElevatedButton(
      onPressed: () {},
      child: Text('Button'),
      style: ElevatedButton.styleFrom(
        primary: Colors.red,
      ),
    )
    
  • Change all buttons color down the widget tree. Use ElevatedButtonTheme:

    ElevatedButtonTheme(
      data: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          primary: Colors.red, // Sets color for all the descendent ElevatedButtons
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {},
            child: Text('Button 1'),
          ),
          SizedBox(height: 12),
          ElevatedButton(
            onPressed: () {},
            child: Text('Button 2'),
          ),
        ],
      ),
    )
    

    enter image description here

like image 43
CopsOnRoad Avatar answered Oct 12 '22 16:10

CopsOnRoad