Flutter控件---超简单的模糊阴影实现

如果Android原生开发让搞个阴影,那就把UI拉出去暴打一顿吧。当然,搞个带阴影的背景切图也是能勉强接受的。
用了Flutter之后发现写带阴影控件简直不要太简单,妈妈再也不用担心我画不出来好看的阴影了。
先上图: 在这里插入图片描述

这次分享的例子是用Container的decoration属性实现的,当然你也可以使用Material家族的MaterialButton等,但是隐形的位置不可控。如上图第五个用的就是官方MaterialButton,阴影主要就是在下方,如果想改上下左右的阴影大小颜色等, 就没法实现了。不废话了,直接上代码:
第一个圆形button:

Widget _circleButton() {
return Container(
margin: EdgeInsets.only(left: 20),
decoration: BoxDecoration(shape: BoxShape.circle, boxShadow: [
///阴影颜色/位置/大小等
BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),
///模糊阴影半径
blurRadius: 5,
),
BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 5),
BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 5),
BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 5)
]),
child: CircleAvatar(
radius: 20,
backgroundColor: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.launch,
color: Colors.black87,
size: 20,
),
Text(
"分享",
style: TextStyle(
color: Colors.grey[400],
fontSize: 10,
),
)
],
),
),
);
}
Widget _rectangleButton() {
return Container(
margin: EdgeInsets.only(top: 20, left: 20),
decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),blurRadius: 5,),
BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 5),
BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 5),
BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 5)
]),
child: FlatButton(
onPressed: () {},
color: Colors.white,
padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.launch,
color: Colors.black87,
size: 20,
),
Text(
"分享",
style: TextStyle(
color: Colors.grey[400],
fontSize: 10,
),
)
],
)),
);
}
Widget _flatButton1() {
return Container(
margin: EdgeInsets.only(top: 20, left: 20),
decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
//BoxShadow中Offset (1,1)右下,(-1,-1)左上,(1,-1)右上,(-1,1)左下
BoxShadow(
color: Colors.red,
offset: Offset(1, 1),
blurRadius: 5,
),
BoxShadow(
color: Colors.blueAccent, offset: Offset(-1, -1), blurRadius: 5),
]),
child: FlatButton(
onPressed: () {},
color: Colors.white,
padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.launch,
color: Colors.black87,
size: 20,
),
Text(
"分享",
style: TextStyle(
color: Colors.grey[400],
fontSize: 10,
),
)
],
)),
);
}
Widget _flatButton2() {
return Container(
margin: EdgeInsets.only(top: 20, left: 20),
decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
//BoxShadow中Offset (1,1)右下,(-1,-1)左上,(1,-1)右上,(-1,1)左下
BoxShadow(
color: Colors.red,
offset: Offset(-1, 1),
blurRadius: 5,
),
BoxShadow(color: Colors.blueAccent, offset: Offset(1, -1), blurRadius: 5),
]),
child: FlatButton(
onPressed: () {},
color: Colors.white,
padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.launch,
color: Colors.black87,
size: 20,
),
Text(
"分享",
style: TextStyle(
color: Colors.grey[400],
fontSize: 10,
),
)
],
)),
);
}
Widget _materialButton() {
return Padding(
padding: EdgeInsets.only(top: 20, left: 20),
child: MaterialButton(
padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
color: Colors.white,
onPressed: () {},
elevation: 5,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.launch,
color: Colors.black87,
size: 20,
),
Text(
"分享",
style: TextStyle(
color: Colors.grey[400],
fontSize: 10,
),
)
],
),
),
);
}

decoration的boxShadow就是一系列的阴影组件,根据坐标位置和大小,颜色以及阴影模糊半径,想怎么实现就怎么实现,阴影实现就是这么简单。

作者:冲锋的麦克

出处:https://www.cnblogs.com/zhangwenju/p/14242845.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   冲锋的麦克  阅读(1356)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示