dart 如何画三角形的圆角?

t1qtbnec  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(137)

我试着从这个图像复制光标,但是我在创建圆角时遇到了一个小问题。我似乎不能正确地完成它。


的数据
我与你分享我的代码(如果你愿意,你可以在DartPad中运行它)。我被困在我需要圆角的地方。我已经尝试了几种方法,但它似乎不起作用。从我大致了解的情况来看,它应该使用drawRRect完成。

  1. import 'package:flutter/material.dart';
  2. import 'dart:math';
  3. void main() {
  4. runApp(
  5. MaterialApp(
  6. home: Scaffold(
  7. appBar: AppBar(
  8. title: const Text('Cursor'),
  9. ),
  10. body: Center(
  11. child: Transform.rotate(
  12. angle: pi,
  13. child: CustomPaint(
  14. size: const Size(60, 70),
  15. painter: CursorPainter()
  16. )
  17. )
  18. ),
  19. ),
  20. )
  21. );
  22. }
  23. class CursorPainter extends CustomPainter {
  24. @override
  25. void paint(Canvas canvas, Size size) {
  26. final paint = Paint()
  27. ..color = Colors.red
  28. ..style = PaintingStyle.fill;
  29. canvas.drawPath(drawPath(size.width, size.height), paint);
  30. }
  31. Path drawPath(double x, double y) => Path()
  32. ..moveTo(x / 2, 0)
  33. ..lineTo(0, y)
  34. ..quadraticBezierTo(x / 2, y + 15, x, y)
  35. ..close();
  36. @override
  37. bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
  38. }

字符串
这是它目前的样子



如果有人有想法

rpppsulh

rpppsulh1#

这将是代码,我会实现做这样的事情,玩一点的值,这样你就可以自定义它更符合你的喜好,我认为transform.rotate是没有必要的,如果你有时间,阅读下面的文章,它会帮助你很多,祝你好运!
https://medium.com/flutter-community/paths-in-flutter-a-visual-guide-6c906464dcd0

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MaterialApp(
  4. home: Scaffold(
  5. appBar: AppBar(
  6. title: const Text('Cursor'),
  7. ),
  8. body: Center(
  9. child:
  10. CustomPaint(size: const Size(50, 70), painter: CursorPainter()),
  11. )),
  12. ));
  13. }
  14. class CursorPainter extends CustomPainter {
  15. @override
  16. void paint(Canvas canvas, Size size) {
  17. final paint = Paint()
  18. ..color = Colors.red
  19. ..style = PaintingStyle.fill;
  20. canvas.drawPath(drawPath(size.width, size.height), paint);
  21. }
  22. Path drawPath(double x, double y) => Path()
  23. ..moveTo(x * 0.05, y * 0.1)
  24. ..quadraticBezierTo(x / 2, -x * 0.10, x * 0.95, y * 0.1)
  25. //borther right
  26. ..quadraticBezierTo(x, y * 0.12, x * 0.95, y * 0.2)
  27. ..lineTo(x * 0.5 + (x * 0.1), y * 0.9)
  28. //borther bottom
  29. ..quadraticBezierTo(x * 0.5, y * 1.08, x * 0.40, y * 0.9)
  30. ..lineTo(x * 0.05, y * 0.2)
  31. //borther left
  32. ..quadraticBezierTo(0, y * 0.12, x * 0.05, y * 0.1)
  33. ..close();
  34. @override
  35. bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
  36. }

字符串

展开查看全部

相关问题