Adding Shadows at the bottom of a container in flutter?
Asked Answered
S

8

36

I have a simple screen with a container about 100 in height and with blue color. I want to add a shadow or elevation at the bottom of the container.

This is my code below

import 'package:flutter/material.dart';
import 'package:finsec/utils/strings.dart';
import 'package:finsec/utils/dimens.dart';
import 'package:finsec/utils/colors.dart';


void main() {
  runApp(new IncomeFragment());
}

class IncomeFragment extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Stack(
        children: <Widget>[
          new Container(
            height: margin_100dp,
            color: colorPrimary,

          ),
          new Container(    //container to  overlay on top of blue container
            alignment: Alignment.topCenter,


            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[

                Text(
                    zero_amount,
                    style: TextStyle(color: white, fontSize: 40.0, fontWeight: FontWeight.bold)
                ),
              ],
            ),
          )
        ],
    );
  }
}

can someone help me to add a shadow or elevation at the bottom of my blue container?

see image below. shawdow should be in place in the red circle enter image description here

thanks in advance

Savannasavannah answered 24/6, 2019 at 20:37 Comment(0)
C
47

You can reuse the first container that you have in your Stack, the container has a property called decoration and it accept a widget of kind BoxDecoration, as you can see in this link: BoxDecoration Inside this widget you can use the boxShadow property to give to your container a custom shadow, try the next code:

new Container(
      height: margin_100dp,
      decoration: BoxDecoration(
          boxShadow: <BoxShadow>[
            BoxShadow(
                color: Colors.black54,
                blurRadius: 15.0,
                offset: Offset(0.0, 0.75)
            )
          ],
        color: colorPrimary
      ),
    ),
Chromosphere answered 24/6, 2019 at 21:14 Comment(2)
also you use card widgetBrakpan
It gives me very weird output. Thanks.Farewell
C
34

Or you can wrap your Container widget with a Material widget which contains an elevation property to give the shadowy effects.

Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Material(
                elevation: 15.0,
                child: Container(
                  height: 100,
                  width: 100,
                  color: Colors.blue,
                  child: Center(child: Text("Material",style: TextStyle(color: Colors.white),)),
                ),
              ),
              SizedBox(width: 100,),
              Container(
                height: 100,
                width: 100,
                decoration: BoxDecoration(
                    boxShadow: <BoxShadow>[
                      BoxShadow(
                          color: Colors.black54,
                          blurRadius: 15.0,
                          offset: Offset(0.0, 0.75)
                      )
                    ],
                    color: Colors.blue
                ),
                child: Center(child: Text('Box Shadow',style: TextStyle(color: Colors.white))),
              ),
            ],
          ),
        ),

Image:

enter image description here

Difference between two widgets is shown above. Hope this helps!!!

Chaisson answered 26/6, 2019 at 6:21 Comment(0)
J
27

Yes, BoxShadow can solve the problem but instead of manually adding the list of BoxShadow, there is a handy map in flutter call kElevationToShadow which map elevation keys to pre-defined list of BoxShadow. It is also defined based on the material design elevation.

Container(
  height: 60.0,
  decoration: BoxDecoration(
    boxShadow: kElevationToShadow[4],
    color: Theme.of(context).bottomAppBarColor,
  ),
  child: ...
);
Jozef answered 15/1, 2020 at 10:19 Comment(0)
H
6
Container(
   margin: EdgeInsets.only(bottom: 7),
   decoration: BoxDecoration(
     color: Colors.white,
     borderRadius: BorderRadius.circular(10),
     boxShadow: kElevationToShadow[2], // Use This kElevationToShadow ***********
   ),
   child: Center(child: Text('With BoxShadow')),
),

enter image description here

Material( // Using Material Widget ***********************
  elevation: 5,
  borderRadius: BorderRadius.circular(10),
  child: Container(
    margin: EdgeInsets.only(bottom: 7),
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(10),
    ),
    child: Center(child: Text('With BoxShadow')),
 ),
),

enter image description here

Holytide answered 15/3, 2022 at 16:55 Comment(0)
C
3

use the Container Shadow as below:

decoration: BoxDecoration(
    boxShadow: [
      BoxShadow(
        color: Colors.black,
        offset: Offset(20.0, 10.0),
        blurRadius: 20.0,
        spreadRadius: 40.0,
      ),
    ], 
  ),

Controll the blurRadius and the SpreadRadius depending on your needs

Consuela answered 24/6, 2019 at 20:52 Comment(0)
T
3

if you want a container to have shadow only top

 boxShadow: [
      BoxShadow(
        color: Color.fromARGB(255, 218, 218, 218),
        blurRadius: 10.0, // soften the shadow
        spreadRadius: 0.0, //extend the shadow
        offset: Offset(
          0.0, // Move to right 10  horizontally
          -15.0, // Move to bottom 10 Vertically
        ),
      )
    ],
Tying answered 5/3, 2022 at 8:13 Comment(0)
C
3

You can add several BoxShadow to show the shadow you need, with OffSet propertie you can move the shadow.

boxShadow: [
             BoxShadow(
                 color: Colors.blue,
                 offset: Offset(0, 8), // hide shadow top
                 blurRadius: 5),
             BoxShadow(
               color: Colors.white,  // replace with color of container
               offset: Offset(-8, 0), // hide shadow right
             ),
             BoxShadow(
               color: Colors.white, // replace with color of container
               offset: Offset(8, 0), // hide shadow left
             ),
           ],
Clairvoyance answered 28/12, 2022 at 20:27 Comment(0)
J
1

Try this one

boxShadow: <BoxShadow>[
                  BoxShadow(
                      color: Colors.black.withOpacity(.6),
                      blurRadius: 12.0,
                      spreadRadius: 6.0,
                      offset: Offset(
                        0,
                        10,
                      )),
Jeromyjerreed answered 3/3, 2023 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.