Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add custom boxshadow to Flutter card

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...

like image 596
Nirav Madariya Avatar asked Apr 01 '18 16:04

Nirav Madariya


People also ask

How do you make a BoxShadow in flutter?

BoxShadow is a built-in widget in flutter, whose functionality is to cast shadow to a box. The BoxShadow widget is usually used with BoxDecoration. In BoxDecoration widget one of its parameters is boxShadow which takes a list of BoxShadow to cast a shadow around a box.

How do you add color to your card in flutter?

Use color property to set the color of the Card . You need to pass a Color , either by using predefined color or creating a custom color by defining the RGB values. color: Colors.


3 Answers

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

like image 101
Raouf Rahiche Avatar answered Sep 22 '22 05:09

Raouf Rahiche


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

like image 43
Christer Avatar answered Sep 24 '22 05:09

Christer


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
        ),
      )
    ],
  ),
),
like image 33
Malisha De Silva Avatar answered Sep 25 '22 05:09

Malisha De Silva