Flutter在Column容器上使用ClipPath

huangapple go评论104阅读模式
英文:

Flutter use ClipPath on Column Container

问题

我正在尝试使用Flutter做一些个人项目。我正在学习一些关于复杂UI的知识,这确实很难。我查阅了文档,一些博客,但仍然不知道如何在Flutter ClipPath中正确定义x、y坐标点。

我的想法是,不是让这个三角形有一个光滑的曲线形状。我尝试了不同的组合,仍然不知道如何剪切和曲线这个三角形。

我的当前自定义剪切器大致如下:

  1. @override
  2. Path getClip(Size size) {
  3. Path path = Path();
  4. path.lineTo(size.width, size.height * 0.5);
  5. path.lineTo(0, size.height);
  6. return path;
  7. }
  8. @override
  9. bool shouldReclip(CustomClipper<Path> oldClipper) => false;
  10. }

有关如何正确剪切这个三角形的x、y坐标的建议将不胜感激。我知道我必须在Flutter中使用二次贝塞尔曲线,但在这一点上我基本上陷入了困境。

谢谢!

英文:

I'm playing around with flutter doing some personal projects. I'm learning a bit about complex UIs and it's been quite hard. I've looked into documentation, some blogs, and still have no idea how to properly define x, y points using Flutter ClipPath.

Flutter在Column容器上使用ClipPath

My idea is to instead of having this triangle have a smooth curve shape. I've tried with different combinations, still no idea how to clip and curve this triangle.

My current custom clipper it's something like this.

  1. @override
  2. Path getClip(Size size) {
  3. Path path = Path();
  4. path.lineTo(size.width, size.height * 0.5);
  5. path.lineTo(0, size.height);
  6. return path;
  7. }
  8. @override
  9. bool shouldReclip(CustomClipper&lt;Path&gt; oldClipper) =&gt; false;
  10. }

Any advice on how to get the x,y coordinates to clip properly this triangle is much appreciated. I know I have to use the quadratic bezier in flutter, but at this point I'm pretty much stuck.

Thanks!

答案1

得分: 0

以下是基于@pskink的建议的示例。只需将您的CustomClipper类中的.lineTo()调用替换为.cubicTo()调用。

您可以更改Container的高度和宽度以及cubicTo()调用中的x/y值,以改变曲线的形状。

如果您想绘制特定的曲线/形状,您需要首先进行数学计算,以计算出确切的坐标。

  1. class CurveClipper extends StatelessWidget {
  2. const CurveClipper({super.key});
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: const Text('Curve Clipper'),
  8. ),
  9. body: Center(
  10. child: ClipPath(
  11. clipper: CustomBezierClipper(),
  12. child: Container(
  13. color: Colors.blueGrey,
  14. height: 200,
  15. width: 50,
  16. ),
  17. ),
  18. ),
  19. );
  20. }
  21. }
  22. class CustomBezierClipper extends CustomClipper<Path> {
  23. @override
  24. Path getClip(Size size) {
  25. Path path = Path();
  26. path.cubicTo(0, size.height / 4, size.width, size.height / 4, size.width,
  27. size.height / 2);
  28. path.cubicTo(size.width, size.height * 3 / 4, 0, size.height * 3 / 4, 0,
  29. size.height);
  30. path.close();
  31. return path;
  32. }
  33. @override
  34. bool shouldReclip(covariant CustomClipper oldClipper) => false;
  35. }

注意:上述代码是一个Flutter应用程序中用于创建自定义剪切区域的示例。

英文:

Below is an example based on @pskink's suggestion. Just replace the .lineTo() calls in your CustomClipper class with .cubicTo() calls.

You can change the height and width of the Container and the x/y values in the cubicTo() calls to alter the shape of the curve.

If you want to draw specific curves/shapes, you will have to do the Maths first to calculate the exact coordinates.

  1. class CurveClipper extends StatelessWidget {
  2. const CurveClipper({super.key});
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: const Text(&#39;Curve Clipper&#39;),
  8. ),
  9. body: Center(
  10. child: ClipPath(
  11. clipper: CustomBezierClipper(),
  12. child: Container(
  13. color: Colors.blueGrey,
  14. height: 200,
  15. width: 50,
  16. ),
  17. ),
  18. ),
  19. );
  20. }
  21. }
  22. class CustomBezierClipper extends CustomClipper&lt;Path&gt; {
  23. @override
  24. getClip(Size size) {
  25. Path path = Path();
  26. path.cubicTo(0, size.height / 4, size.width, size.height / 4, size.width,
  27. size.height / 2);
  28. path.cubicTo(size.width, size.height * 3 / 4, 0, size.height * 3 / 4, 0,
  29. size.height);
  30. path.close();
  31. return path;
  32. }
  33. @override
  34. bool shouldReclip(covariant CustomClipper oldClipper) =&gt; false;
  35. }

huangapple
  • 本文由 发表于 2023年7月3日 10:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定