【Flutter 专题】116 图解 PhysicalModel & PhysicalShape 裁切小组件

return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Container(width: 80.0, height: 80.0, child: Image.asset(‘images/icon_hzw01.jpg’)));
return Container(
width: 80.0, height: 80.0,
child: ClipOval(child: Image.asset(‘images/icon_hzw01.jpg’, fit: BoxFit.fill)));

// PhysicalModel
return PhysicalModel(
color: Colors.transparent,
shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Container(width: 80.0, height: 80.0, child: Image.asset(‘images/icon_hzw01.jpg’)));

【Flutter 专题】116 图解 PhysicalModel & PhysicalShape 裁切小组件

2. color & shadowColor

color 对应 Widget 裁切的背景色,阴影效果是根据当前背景色展示,shadowColor 可以设置阴影颜色;

return PhysicalModel(
color: Colors.teal.withOpacity(0.6),
shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
clipBehavior: Clip.antiAlias,
elevation: 6.0,
shadowColor: isCircle ? Colors.yellow : Colors.deepOrange,
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Container(width: 80.0, height: 80.0, color: Colors.pink.withOpacity(0.2)));

【Flutter 专题】116 图解 PhysicalModel & PhysicalShape 裁切小组件

PhysicalShape

源码分析

const PhysicalShape({
Key key,
@required this.clipper,
this.clipBehavior = Clip.none,
this.elevation = 0.0,
@require

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

d this.color,
this.shadowColor = const Color(0xFF000000),
Widget child,
})

分析源码可得,PhysicalShapeClipPath 类似,均是通过 clipper 方式自定义裁切方式;

案例尝试

小菜尝试裁切一个多边形图片,通过自定义 CustomClipper 来设置裁切样式;

return ClipPath(
clipBehavior: Clip.antiAlias,
clipper: PolygonClipper(10),
child: Container(width: 80.0, height: 80.0, child: Image.asset(‘images/icon_hzw01.jpg’)));

return PhysicalShape(
color: Colors.transparent,
clipBehavior: Clip.antiAlias,
clipper: PolygonClipper(10),
shadowColor: Colors.deepOrange,
elevation: 6.0,
child: Container(width: 80.0, height: 80.0, child: Image.asset(‘images/icon_hzw01.jpg’)));

class PolygonClipper extends CustomClipper {
final int num;
PolygonClipper(this.num);

@override
Path getClip(Size size) {
double radius = size.width * 0.5;
Path _path = Path();
var startX = size.width * 0.5 + radius * cos(2 * pi * 0 / num);
var startY = size.height * 0.5 + radius * sin(2 * pi * 0 / num);
_path.moveTo(startX, startY);
for (var i = 1; i <= num; i++) {
var newX = size.width * 0.5 + radius * cos(2 * pi * i / num);
var newY = size.height * 0.5 + radius * sin(2 * pi * i / num);
_path.lineTo(newX, newY);
}
_path.close();
return _path;
}
@override
bool shouldReclip(CustomClipper oldClipper) => true;
}
se();
return _path;
}
@override
bool shouldReclip(CustomClipper oldClipper) => true;
}

上一篇:分享一个简单好看的背景图


下一篇:C# WPF 查找窗体上的子元素、得到窗体上的一类元素