【问题标题】:Flutter drawing a heart shape with CustomPainterFlutter 使用 CustomPainter 绘制心形
【发布时间】:2019-09-02 12:07:59
【问题描述】:

我想知道是否有人对如何开始使用 CustomPainter 绘制心形有任何指示。我已经设法画出三角形和正方形或基本圆形之类的东西,但心脏当然有直线和曲线。

我有这个画一个三角形,看起来有点像心脏,但不知道如何获得心脏所需的曲线。

class Heart extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: CustomPaint(
        painter: TrianglePainter(
          strokeColor: Color(0xFFF27788),
          paintingStyle: PaintingStyle.fill,
        ),
        child: Container(
          height: 60 * Dep.hr,
          width: 60 * Dep.hr,
        ),
      ),
    );
  }
}

class TrianglePainter extends CustomPainter {
  final Color strokeColor;
  final PaintingStyle paintingStyle;
  final double strokeWidth;

  TrianglePainter({this.strokeColor, this.strokeWidth = 3, this.paintingStyle = PaintingStyle.stroke});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = strokeColor
      ..strokeWidth = strokeWidth
      ..style = paintingStyle;

    canvas.drawPath(getTrianglePath(size.width, size.height), paint);
  }

  Path getTrianglePath(double x, double y) {
    return Path()
      ..moveTo(y, 0)
      ..lineTo(0, 0)
      ..lineTo(x / 2, y);
  }

  @override
  bool shouldRepaint(TrianglePainter oldDelegate) {
    return oldDelegate.strokeColor != strokeColor ||
        oldDelegate.paintingStyle != paintingStyle ||
        oldDelegate.strokeWidth != strokeWidth;
  }
}

而且它只是一块颜色,但我也确实需要在形状周围加上边框。这是我的预期输出,不知道是不是一厢情愿。

【问题讨论】:

  • 使用 drawArc() 函数在 CustomPainter 中绘制曲线。
  • 什么是 Dep.hr ??

标签: flutter dart custom-painting


【解决方案1】:

试试这个:

  class HeartWidget extends StatefulWidget {
    @override
    _HeartWidgetState createState() => _HeartWidgetState();
  }

  class _HeartWidgetState extends State<HeartWidget> {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Testing'),
        ),
        body: Center(
        child: CustomPaint(
            size: Size(70, 80),
            painter: HeartPainter(),
          ),
        ),
      );
    }
  }

  class HeartPainter extends CustomPainter {
    @override
    void paint(Canvas canvas, Size size) {
      // TODO: implement paint
      Paint paint = Paint();
      paint
        ..color = Colors.black
        ..style = PaintingStyle.stroke
        ..strokeCap = StrokeCap.round
        ..strokeWidth = 6;

      Paint paint1 = Paint();
      paint1
        ..color = Colors.red
        ..style = PaintingStyle.fill
        ..strokeWidth = 0;

      double width = size.width;
      double height = size.height;

      Path path = Path();
      path.moveTo(0.5 * width, height * 0.35);
      path.cubicTo(0.2 * width, height * 0.1, -0.25 * width, height * 0.6,
          0.5 * width, height);
      path.moveTo(0.5 * width, height * 0.35);
      path.cubicTo(0.8 * width, height * 0.1, 1.25 * width, height * 0.6,
          0.5 * width, height);

      canvas.drawPath(path, paint1);
      canvas.drawPath(path, paint);
    }

    @override
    bool shouldRepaint(CustomPainter oldDelegate) {
      return true;
    }
  }

【讨论】:

  • 哇!那是非同寻常的。现在就试试看。谢谢。
  • 太棒了,效果很好。您是如何了解 custompainter 的?我在上面找不到那么多教材,主要是正方形和圆形之类的基本形状,除此之外没有。
  • 我在过去的经验中尝试过,所以根据您的需要与您分享我的代码,在我的情况下,我没有那个边框,我画了它,但最好的选择是为 CustomPainter 谷歌它跨度>
  • 嗯,是的,我用谷歌搜索了它,但只找到了我所说的。无论如何,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 2022-12-25
  • 2021-01-11
  • 1970-01-01
  • 2019-12-29
  • 2012-03-23
  • 2016-02-27
相关资源
最近更新 更多