dart 如何用CustomPainter绘制曲线图?

mwngjboj  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(187)

我尝试用一些平滑曲线绘制一个线形图,如fl_chart中的这一个:

我尝试使用quadraticBezierTo,但它不工作:

这是我的密码,还有别的办法吗?

import 'package:flutter/material.dart';

class DrawLines extends StatefulWidget {
  final Coordinates lineCoordinates;
  final double yHeight;

  DrawLines({required this.lineCoordinates, required this.yHeight});

  @override
  _DrawLinesState createState() => _DrawLinesState();
}

class _DrawLinesState extends State<DrawLines> {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      foregroundPainter: LinePainter(
          coordinates: widget.lineCoordinates, yHeight: widget.yHeight),
    );
  }
}

class CoordValues {
  late double x;
  late double y;

  CoordValues({required this.x, required this.y});
}

class Coordinates {
  late CoordValues startCoords;
  late CoordValues endCoords;

  Coordinates({required this.startCoords, required this.endCoords});
}

class LinePainter extends CustomPainter {
  late Coordinates coordinates;
  late double yHeight;

  LinePainter({required this.coordinates, required this.yHeight});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Color(0xFF295AE3)
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 2;

    //curved
    Path path = Path();

    path.moveTo(coordinates.startCoords.x, yHeight - coordinates.startCoords.y);
    path.quadraticBezierTo(
        coordinates.startCoords.x + 15,
        coordinates.startCoords.y,
        coordinates.endCoords.x + 15,
        yHeight - coordinates.endCoords.y);
    //for every two points one line is drawn

  

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

我想我的问题很简单,但不知何故stackoverflow想让我写更多。

o2gm4chl

o2gm4chl1#

您可以使用fl_chart依赖项,此处为链接

相关问题