Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Shadows at the bottom of a container in flutter?

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

like image 320
yoohoo Avatar asked Jun 24 '19 20:06

yoohoo


People also ask

How do you give shadow to container bottom in flutter?

Steps to Add Shadow to Container Widget in Flutter The Container widget has a property called decoration. You can use the BoxDecoration class to add shadow to Container widget in Flutter using the BoxShadow widget. The BoxShadow is a widget that creates a shadow. By default, it comes in black color.

How do you give shadow to one side of a container in flutter?

new Container( decoration: BoxDecoration( color: Colors. grey. withOpacity(0.5), boxShadow: [ BoxShadow( blurRadius: 5.0 ), ], ), ), This code works but adds a shadow to every possible side of the container.

How do you add a shadow in TextField flutter?

For changing the shadow color you use the shadowColor property of the material. Which takes the Color object as an input. For adding a drop shadow to flutter TextField or TextFormField you can use the Material widget. Just wrap TextField or TextFormField with Material widget and set elevation and shadow color.


4 Answers

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
      ),
    ),
like image 59
Luis Miguel Mantilla Avatar answered Sep 28 '22 19:09

Luis Miguel Mantilla


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

like image 39
Syangden DT Avatar answered Sep 28 '22 19:09

Syangden DT


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: ...
);
like image 21
akp Avatar answered Sep 28 '22 20:09

akp


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

like image 2
Sumit Avatar answered Sep 28 '22 18:09

Sumit