在颤动的颜色容器底部添加阴影

时间:2019-06-24 20:37:54

标签: flutter

我有一个简单的屏幕,其中装有一个高度约100的容器,颜色为蓝色。我想在容器底部添加阴影或高程。

这是我的下面的代码

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)
                ),
              ],
            ),
          )
        ],
    );
  }
}

有人可以帮助我在蓝色容器的底部添加阴影或高程吗?

请参见下图。阴影应该在红色圆圈内 enter image description here

预先感谢

4 个答案:

答案 0 :(得分:9)

是的,BoxShadow可以解决此问题,但取代手动添加BoxShadow的列表,在flutter调用中有一个方便的地图 kElevationToShadow 定义的BoxShadow列表。它也是基于material design elevation定义的。

Container(
  height: 60.0,
  decoration: BoxDecoration(
    boxShadow: kElevationToShadow[4],
    color: Theme.of(context).bottomAppBarColor,
  ),
  child: ...
);

答案 1 :(得分:0)

使用容器阴影,如下所示:

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

根据需要控制blurRadius和SpreadRadius

答案 2 :(得分:0)

您可以重用堆栈中的第一个容器,该容器具有一个称为装饰的属性,并且可以接受BoxDecoration类型的小部件,如您在此链接中所见:BoxDecoration 在此小部件内,您可以使用boxShadow属性为您的容器提供自定义阴影,请尝试以下代码:

new Container(
      height: margin_100dp,
      decoration: BoxDecoration(
          boxShadow: <BoxShadow>[
            BoxShadow(
                color: Colors.black54,
                blurRadius: 15.0,
                offset: Offset(0.0, 0.75)
            )
          ],
        color: colorPrimary
      ),
    ),

答案 3 :(得分:0)

或者,您也可以将“容器”窗口小部件包裹在“材质”窗口小部件上,该窗口小部件包含一个高程属性以提供阴影效果。

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))),
              ),
            ],
          ),
        ),

图片:

enter image description here

两个小部件之间的差异如上所示。希望这会有所帮助!