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 国际」许可协议进行许可。
标签:
Flutter
如果这篇文章帮助到您,请我喝个肥宅快乐水可好
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示