效果图

image.png

一般实现方式

  • ClipPath
  • CustomClipper
  • 贝塞尔曲线

    1. /// 一般的方式
    2. class OldMyWidget extends StatelessWidget {
    3. @override
    4. Widget build(BuildContext context) {
    5. return Scaffold(
    6. appBar: AppBar(title: Text("ClipRRect Demo")),
    7. body: Center(
    8. // 这里使用 ClipPath
    9. child: ClipPath(
    10. clipper:ClipperPath(),
    11. child: Container(
    12. width: 400,
    13. height: 200,
    14. color: Colors.blue,
    15. alignment: Alignment.center,
    16. child: Text(
    17. 'Flutter\n\nClipRRect',
    18. textAlign: TextAlign.center,
    19. ),
    20. ),
    21. ),
    22. ),
    23. );
    24. }
    25. }
    26. /// 创建剪裁路径
    27. class ClipperPath extends CustomClipper<Path> {
    28. @override
    29. Path getClip(Size size) {
    30. var path = Path();
    31. path.lineTo(0.0, size.height - 50);
    32. var firstControlPoint = Offset(size.width / 4, size.height);
    33. var firstPoint = Offset(size.width / 2, size.height);
    34. path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
    35. firstPoint.dx, firstPoint.dy);
    36. var secondControlPoint = Offset(size.width - (size.width / 4), size.height);
    37. var secondPoint = Offset(size.width, size.height - 50);
    38. path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy,
    39. secondPoint.dx, secondPoint.dy);
    40. path.lineTo(size.width, 0.0);
    41. path.close();
    42. return path;
    43. }
    44. @override
    45. bool shouldReclip(CustomClipper<Path> oldClipper) => false;
    46. }

    效果

    image.png

    简单的实现方式

  • ClipRRect

  • BorderRadius ```dart

class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(“ClipRRect Demo”)), body: Center( /// 这里使用 ClipRRect child: ClipRRect( /// 设置剪裁半径 /// 顶部是 10 圆形 /// 底部是 x:200 和 y:50 半径的椭圆 borderRadius: BorderRadius.vertical( top: Radius.circular(10), bottom: Radius.elliptical(200, 50), ), child: Container( width: 400, height: 200, color: Colors.blue, alignment: Alignment.center, child: Text( ‘Flutter\n\nClipRRect’, textAlign: TextAlign.center, ), ), ), ), ); } } ```

效果

image.png

在线查看效果