我已在Flutter应用程序中实现了卡片。我需要为每个卡片设置自定义BoxShadow
。如何做到这一点?
目前我尝试的是将BoxShadow
属性添加到Card()
构造函数中,但这并不起作用...
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,
),
],
),
);
}
}
只需将卡片放入一个容器中,并获取 boxShadow 属性,即可为卡片小部件应用阴影效果。
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
),
)
],
),
),
查看卡片小部件
@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,
),
)
],
),
),
));
}
当谈论阴影时,主要可以通过调整模糊度
和颜色
来改变阴影的外观。
因此,您可以在不编写额外代码的情况下执行此操作。
Card(
elevation: 4, // Change this
shadowColor: Colors.black12, // Change this
child: Center(child: Text('Hey, dude.')),
),
elevation
和 shadowColor
才能起作用。offset
和 blurRadius
可以在 BoxShadow
对象中定义,这两个参数无法通过调整来实现。 - tanghao使用elevation
Card(elevation: 10, child: theWidget),
margin: const EdgeInsets.all(10.0)
- Raouf Rahiche