我试图弄清楚我们如何使用剪辑器来剪辑容器内部以消除阴影颜色。
这是我的解决方案:
首先,我创建了一个InvertedClipper,其任务是剪辑小部件的内部:
class InvertedClipper extends CustomClipper<Path> {
InvertedClipper();
@override
Path getClip(Size size) {
return Path.combine(
PathOperation.difference,
Path()..addRect(Rect.fromLTWH(0, 0, size.width, size.height)),
Path()..addRect(Rect.fromLTWH(1, 1, size.width - 5, size.height - 5)),
);
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}
为此,我关注了this interesting article。
也就是说,我马上尝试了剪辑器,但注意到如果你用阴影剪辑 Container,阴影会丢失 spread 和 blur。
谷歌搜索我偶然发现了 clip_shadow 包,它允许为剪辑的小部件添加阴影(这是你需要的)。
按照自述文件我这样做了:
class DummyHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
body: Center(
child: ClipShadow(
boxShadow: [
BoxShadow(
color: Colors.black,
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
clipper: InvertedClipper(),
child: Container(
height: 250,
width: 250,
color: Colors.transparent,
),
),
),
);
}
}
这导致了这个结果:
抱歉,图片太大了,我不知道如何在 Markdown 中调整它的大小。除此之外,我希望这会有所帮助。
编码愉快!