38

I've implemented cards in a Flutter app. I need a custom BoxShadow for every card. How can this be done?

What I've tried so far is to add the BoxShadow property to the Card() constructor, which is not working...

Edric
  • 24,639
  • 13
  • 81
  • 91
Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37

5 Answers5

63

The Card Widget doesn't have decoration property so you need to make a card inside a Container and then apply the BoxShadow to the Container,

Sample :

class MyCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Card(
        child: new Center(
          child: new Icon(
            Icons.refresh,
            size: 150.0,
          ),
        ),
      ),
      decoration: new BoxDecoration(
        boxShadow: [
          new BoxShadow(
            color: Colors.black,
            blurRadius: 20.0,
          ),
        ],
      ),
    );
  }
}

enter image description here

Boken
  • 4,825
  • 10
  • 32
  • 42
Raouf Rahiche
  • 28,948
  • 10
  • 85
  • 77
19

Simply wrap the card in a container for applying shadow to card widget by obtaining boxShadow property.

new Container(
  width: 100,
  height: 100,
  decoration: new BoxDecoration(
    boxShadow: [
      BoxShadow(
        color: Colors.grey.withOpacity(.5),
        blurRadius: 20.0, // soften the shadow
        spreadRadius: 0.0, //extend the shadow
        offset: Offset(
          5.0, // Move to right 10  horizontally
          5.0, // Move to bottom 10 Vertically
        ),
      )
    ],
  ),
),
Boken
  • 4,825
  • 10
  • 32
  • 42
Malisha De Silva
  • 393
  • 2
  • 10
17

See the Card widget

      @override
      Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Color(0xFFdde0e3),
            body: SingleChildScrollView(
              child: Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Card(
                      elevation:5,
                      margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 16.0),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(0.0),
                      ),
                      child: Container(
                        height: 200,
                      ),
                    )
                  ],
                ),
              ),
            ));
      }

enter image description here

Christer
  • 2,746
  • 3
  • 31
  • 50
14

When talk about shadows mainly the appearance of the shadow can be changed by tweaking blurness and color.

So you can do something like this without writing extra lines of code.

Card(
  elevation: 4,  // Change this 
  shadowColor: Colors.black12,  // Change this 
  child: Center(child: Text('Hey, dude.')),
),
Nipun Ravisara
  • 3,629
  • 3
  • 20
  • 35
  • 1
    The `elevation` and `shadowColor` only work when you want the shadow effect applied without offset and blur effect. `offset` and `blurRadius` can be defined in the `BoxShadow` object, which can't be achieved by tweaking these two parameters. – tanghao Aug 06 '21 at 01:42
-2

Use elevation

Card(elevation: 10, child: theWidget),
lepsch
  • 8,927
  • 5
  • 24
  • 44
J School
  • 17
  • 3