paint method

  1. @override
void paint(
  1. dynamic canvas,
  2. dynamic size
)

Implementation

@override
void paint(Canvas canvas, Size size) {
  if (detections.isEmpty) return;
  final double scaleX = size.width / sourceWidth;
  final double scaleY = size.height / sourceHeight;

  final Paint boxPaint = Paint()
    ..style = PaintingStyle.stroke
    ..strokeWidth = 2
    ..color = Colors.greenAccent;

  for (final det in detections) {
    final rect = Rect.fromLTRB(
      det.x1 * scaleX,
      det.y1 * scaleY,
      det.x2 * scaleX,
      det.y2 * scaleY,
    );
    canvas.drawRect(rect, boxPaint);

    final textPainter = TextPainter(
      text: TextSpan(
        text: '${(det.score * 100).toStringAsFixed(0)}%',
        style: const TextStyle(
          color: Colors.black,
          fontSize: 12,
          backgroundColor: Colors.greenAccent,
        ),
      ),
      textDirection: TextDirection.ltr,
    )..layout();
    textPainter.paint(
      canvas,
      Offset(
        rect.left,
        (rect.top - textPainter.height).clamp(0, size.height),
      ),
    );
  }
}