使用那个习惯ShapeBorder
class WaveShapeBorder extends ShapeBorder {
@override EdgeInsetsGeometry get dimensions => EdgeInsets.zero;
@override ui.Path getInnerPath(ui.Rect rect, {ui.TextDirection textDirection}) => null;
@override
ui.Path getOuterPath(ui.Rect rect, {ui.TextDirection textDirection}) {
var ctrl1 = FractionalOffset(0.35, 0.75).withinRect(rect);
var end1 = FractionalOffset(0.65, 0.85).withinRect(rect);
var ctrl2 = FractionalOffset(0.85, 0.90).withinRect(rect);
var end2 = FractionalOffset(1.0, 0.75).withinRect(rect);
return Path()
..moveTo(rect.topLeft.dx, rect.topLeft.dy)
..lineTo(rect.bottomLeft.dx, rect.bottomLeft.dy)
..quadraticBezierTo(ctrl1.dx, ctrl1.dy, end1.dx, end1.dy)
..quadraticBezierTo(ctrl2.dx, ctrl2.dy, end2.dx, end2.dy)
..lineTo(rect.topRight.dx, rect.topRight.dy)
..close();
}
@override void paint(ui.Canvas canvas, ui.Rect rect, {ui.TextDirection textDirection}) {}
@override ShapeBorder scale(double t) => this;
}
以及用于测试的示例代码:
class WaveShapeBorderTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.teal, Colors.green],
begin: Alignment.topRight,
end: Alignment.centerLeft,
),
),
child: Stack(
children: [
Material(
elevation: 6,
shape: WaveShapeBorder(),
clipBehavior: Clip.antiAlias,
child: Image.asset('images/someImage.png'),
),
],
),
);
}
}
当然,你可以自由选择如何使用它,但主要思想是使用Material.shape
and Material.clipBehavior
特性
这是最终结果:
EDIT如果您认为您的阴影太“亮”,您可以添加一个额外的阴影Container
使用自定义阴影:
class WaveShapeBorderTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.teal, Colors.green],
begin: Alignment.topRight,
end: Alignment.centerLeft,
),
),
child: Stack(
children: [
Container(
decoration: ShapeDecoration(
shape: WaveShapeBorder(),
shadows: [
BoxShadow(color: Colors.black, blurRadius: 16, offset: Offset(2, 2), spreadRadius: 8),
]
),
child: Material(
clipBehavior: Clip.antiAlias,
shape: WaveShapeBorder(),
child: Image.asset('images/someImage.png',),
),
),
Icon(Icons.photo, size: 64,),
],
),
);
}
}
你会看到这个: