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) {
  // if the widget has a zero size side then we cannot continue painting.
  if (size.shortestSide == 0) {
    debugPrint(
        "[QR] WARN: width or height is zero. You should set a 'size' value "
        'or nest this painter in a Widget that defines a non-zero size');
    return;
  }

  final paintMetrics = _PaintMetrics(
    containerSize: size.shortestSide,
    moduleCount: _qr!.moduleCount,
    gapSize: gapless ? 0 : _gapSize,
  );

  // draw the finder pattern elements
  _drawFinderPatternItem(
    FinderPatternPosition.topLeft,
    canvas,
    paintMetrics,
  );
  _drawFinderPatternItem(
    FinderPatternPosition.bottomLeft,
    canvas,
    paintMetrics,
  );
  _drawFinderPatternItem(
    FinderPatternPosition.topRight,
    canvas,
    paintMetrics,
  );

  // DEBUG: draw the inner content boundary
//    final paint = Paint()..style = ui.PaintingStyle.stroke;
//    paint.strokeWidth = 1;
//    paint.color = const Color(0x55222222);
//    canvas.drawRect(
//        Rect.fromLTWH(paintMetrics.inset, paintMetrics.inset,
//            paintMetrics.innerContentSize, paintMetrics.innerContentSize),
//        paint);

  double left;
  double top;
  final gap = !gapless ? _gapSize : 0;
  // get the painters for the pixel information
  final pixelPaint = _paintCache.firstPaint(QRCodeElement.codePixel);
  if (color != null) {
    pixelPaint!.color = color!;
  } else {
    pixelPaint!.color = dataModuleStyle.color!;
  }
  Paint? emptyPixelPaint;
  if (emptyColor != null) {
    emptyPixelPaint = _paintCache.firstPaint(QRCodeElement.codePixelEmpty);
    emptyPixelPaint!.color = emptyColor!;
  }
  for (var x = 0; x < _qr!.moduleCount; x++) {
    for (var y = 0; y < _qr!.moduleCount; y++) {
      // draw the finder patterns independently
      if (_isFinderPatternPosition(x, y)) {
        continue;
      }
      final paint = _qrImage.isDark(y, x) ? pixelPaint : emptyPixelPaint;
      if (paint == null) {
        continue;
      }
      // paint a pixel
      left = paintMetrics.inset + (x * (paintMetrics.pixelSize + gap));
      top = paintMetrics.inset + (y * (paintMetrics.pixelSize + gap));
      var pixelHTweak = 0.0;
      var pixelVTweak = 0.0;
      if (gapless && _hasAdjacentHorizontalPixel(x, y, _qr!.moduleCount)) {
        pixelHTweak = 0.5;
      }
      if (gapless && _hasAdjacentVerticalPixel(x, y, _qr!.moduleCount)) {
        pixelVTweak = 0.5;
      }
      final squareRect = Rect.fromLTWH(
        left,
        top,
        paintMetrics.pixelSize + pixelHTweak,
        paintMetrics.pixelSize + pixelVTweak,
      );
      if (dataModuleStyle.dataModuleShape == QRDataModuleShape.square) {
        canvas.drawRect(squareRect, paint);
      } else {
        final roundedRect = RRect.fromRectAndRadius(
          squareRect,
          Radius.circular(paintMetrics.pixelSize + pixelHTweak),
        );
        canvas.drawRRect(roundedRect, paint);
      }
    }
  }

  if (embeddedImage != null) {
    final originalSize = Size(
      embeddedImage!.width.toDouble(),
      embeddedImage!.height.toDouble(),
    );
    final requestedSize = embeddedImageStyle?.size;
    final imageSize = _scaledAspectSize(size, originalSize, requestedSize);
    final position = Offset(
      (size.width - imageSize.width) / 2.0,
      (size.height - imageSize.height) / 2.0,
    );
    // draw the image overlay.
    _drawImageOverlay(canvas, position, imageSize, embeddedImageStyle);
  }
}