paint method

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

Called whenever the object needs to paint. The given Canvas has its coordinate space configured such that the origin is at the top left of the box. The area of the box is the size of the size argument.

Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.

Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.

To paint text on a Canvas, use a TextPainter.

To paint an image on a Canvas:

  1. Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.

  2. Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.

  3. In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.

Implementation

@override
void paint(Canvas canvas, Size size) {
  final effectiveOverlayColor =
      theme?.overlayMaskColor ?? Colors.black.withValues(alpha: 0.65);
  final backgroundPaint = Paint()
    ..color = effectiveOverlayColor
    ..style = PaintingStyle.fill;

  final width = size.width;
  final height = size.height;

  late Rect scanRect;
  if (customScanArea != null) {
    scanRect = customScanArea!;
  } else {
    final targetRatio = scanMode.targetAspectRatio;
    double calcWidth = width * 0.85;
    double calcHeight = calcWidth / targetRatio;

    if (calcHeight > height * 0.6) {
      calcHeight = height * 0.6;
      calcWidth = calcHeight * targetRatio;
    }

    final calcLeft = (width - calcWidth) / 2;
    final calcTop = (height - calcHeight) / 2;
    scanRect = Rect.fromLTWH(calcLeft, calcTop, calcWidth, calcHeight);
  }

  final double rectLeft = scanRect.left;
  final double rectTop = scanRect.top;
  final double rectWidth = scanRect.width;
  final double rectHeight = scanRect.height;

  final backgroundPath = Path()..addRect(Rect.fromLTWH(0, 0, width, height));

  final cornerRadius = theme?.reticleCornerRadius ?? 16.0;
  final cutoutPath = Path();
  if (scanMode == ScanMode.face) {
    cutoutPath.addOval(scanRect);
  } else {
    cutoutPath.addRRect(
      RRect.fromRectAndRadius(scanRect, Radius.circular(cornerRadius)),
    );
  }

  final path = Path.combine(
    PathOperation.difference,
    backgroundPath,
    cutoutPath,
  );
  canvas.drawPath(path, backgroundPaint);

  final effectiveBorderColor =
      theme?.reticleBorderColor ??
      (theme?.accentColor ?? accentColor).withValues(alpha: 0.85);
  final borderPaint = Paint()
    ..color = effectiveBorderColor
    ..style = PaintingStyle.stroke
    ..strokeWidth = theme?.reticleBorderWidth ?? 2.5;

  if (scanMode == ScanMode.face) {
    canvas.drawOval(scanRect, borderPaint);
  } else {
    canvas.drawRRect(
      RRect.fromRectAndRadius(scanRect, Radius.circular(cornerRadius)),
      borderPaint,
    );
  }

  if (scanMode != ScanMode.face) {
    final effectiveCornerColor =
        theme?.reticleCornerColor ?? theme?.accentColor ?? accentColor;
    final cornerPaint = Paint()
      ..color = effectiveCornerColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = theme?.reticleCornerWidth ?? 4.5
      ..strokeCap = StrokeCap.round;

    final cornerLen = theme?.reticleCornerLength ?? 28.0;

    canvas.drawLine(
      Offset(rectLeft, rectTop + cornerLen),
      Offset(rectLeft, rectTop),
      cornerPaint,
    );
    canvas.drawLine(
      Offset(rectLeft, rectTop),
      Offset(rectLeft + cornerLen, rectTop),
      cornerPaint,
    );

    canvas.drawLine(
      Offset(rectLeft + rectWidth - cornerLen, rectTop),
      Offset(rectLeft + rectWidth, rectTop),
      cornerPaint,
    );
    canvas.drawLine(
      Offset(rectLeft + rectWidth, rectTop),
      Offset(rectLeft + rectWidth, rectTop + cornerLen),
      cornerPaint,
    );

    canvas.drawLine(
      Offset(rectLeft, rectTop + rectHeight - cornerLen),
      Offset(rectLeft, rectTop + rectHeight),
      cornerPaint,
    );
    canvas.drawLine(
      Offset(rectLeft, rectTop + rectHeight),
      Offset(rectLeft + cornerLen, rectTop + rectHeight),
      cornerPaint,
    );

    canvas.drawLine(
      Offset(rectLeft + rectWidth - cornerLen, rectTop + rectHeight),
      Offset(rectLeft + rectWidth, rectTop + rectHeight),
      cornerPaint,
    );
    canvas.drawLine(
      Offset(rectLeft + rectWidth, rectTop + rectHeight - cornerLen),
      Offset(rectLeft + rectWidth, rectTop + rectHeight),
      cornerPaint,
    );
  }

  // Render Laser Scanline with Gradient Glow
  if (theme?.showLaserBeam ?? true) {
    final effectiveLaserColor =
        theme?.laserBeamColor ?? theme?.accentColor ?? accentColor;
    final beamY = rectTop + (rectHeight * animationValue);

    final glowRect = Rect.fromLTWH(
      rectLeft + 8,
      beamY - 12,
      rectWidth - 16,
      24,
    );
    final glowPaint = Paint()
      ..shader = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          effectiveLaserColor.withValues(alpha: 0.0),
          effectiveLaserColor.withValues(alpha: 0.25),
          effectiveLaserColor.withValues(alpha: 0.0),
        ],
      ).createShader(glowRect);
    canvas.drawRect(glowRect, glowPaint);

    final beamPaint = Paint()
      ..color = effectiveLaserColor
      ..strokeWidth = 2.5
      ..style = PaintingStyle.stroke;

    canvas.drawLine(
      Offset(rectLeft + 8, beamY),
      Offset(rectLeft + rectWidth - 8, beamY),
      beamPaint,
    );
  }

  // Render Tap-to-Focus Ring with ripple effect
  if (focusPoint != null) {
    final rippleRadius = 22.0 + (animationValue * 6.0);
    final focusPaint = Paint()
      ..color = Colors.amberAccent.withValues(alpha: 0.85)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;
    canvas.drawCircle(focusPoint!, rippleRadius, focusPaint);
    canvas.drawLine(
      Offset(focusPoint!.dx - 8, focusPoint!.dy),
      Offset(focusPoint!.dx + 8, focusPoint!.dy),
      focusPaint,
    );
    canvas.drawLine(
      Offset(focusPoint!.dx, focusPoint!.dy - 8),
      Offset(focusPoint!.dx, focusPoint!.dy + 8),
      focusPaint,
    );
  }

  if (scanMode == ScanMode.passport) {
    final mrzHeight = rectHeight * 0.35;
    final mrzRect = Rect.fromLTWH(
      rectLeft + 12,
      rectTop + rectHeight - mrzHeight - 12,
      rectWidth - 24,
      mrzHeight,
    );
    final mrzPaint = Paint()
      ..color = Colors.amber.withValues(alpha: 0.4)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.5;
    canvas.drawRRect(
      RRect.fromRectAndRadius(mrzRect, const Radius.circular(8)),
      mrzPaint,
    );
  } else if (scanMode == ScanMode.face) {
    final guidePaint = Paint()
      ..color = Colors.white.withValues(alpha: 0.25)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.2;

    final eyeY = rectTop + rectHeight * 0.38;
    canvas.drawCircle(
      Offset(rectLeft + rectWidth * 0.32, eyeY),
      14,
      guidePaint,
    );
    canvas.drawCircle(
      Offset(rectLeft + rectWidth * 0.68, eyeY),
      14,
      guidePaint,
    );
  } else if (scanMode == ScanMode.document) {
    final docPaint = Paint()
      ..color = const Color(0xFF00E5FF).withValues(alpha: 0.5)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.5;
    final margin = 16.0;
    canvas.drawRRect(
      RRect.fromRectAndRadius(
        Rect.fromLTWH(
          rectLeft + margin,
          rectTop + margin,
          rectWidth - (margin * 2),
          rectHeight - (margin * 2),
        ),
        const Radius.circular(12),
      ),
      docPaint,
    );
  } else if (scanMode == ScanMode.multiCode) {
    final multiPaint = Paint()
      ..color = Colors.lightGreenAccent.withValues(alpha: 0.6)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.5;
    canvas.drawCircle(
      Offset(rectLeft + rectWidth * 0.3, rectTop + rectHeight * 0.35),
      24,
      multiPaint,
    );
    canvas.drawCircle(
      Offset(rectLeft + rectWidth * 0.7, rectTop + rectHeight * 0.65),
      24,
      multiPaint,
    );
  }
}