paint method
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:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
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.
-
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) {
ui.Image? background = this.background;
Size? backgroundSize;
if (background != null) {
backgroundSize = paintBackground(canvas, size, background);
}
mapScanModules.forEach(
(module, matchedCounterList) {
for (ScanMatchCounter matchedCount in matchedCounterList) {
if (!matchedCount.visible) {
/// If the result is not visible, do nothing.
continue;
}
/// Resize the trapezoid of the result to adapt it to the size of the preview of the camera
double padding = 8;
Trapezoid trapezoid =
matchedCount.scanResult.trapezoid.resizedTrapezoid(
backgroundSize ?? size,
imageSize,
imageRotation,
padding,
padding,
backgroundSize != null
? (size.width - backgroundSize.width) / 2
: 0,
backgroundSize != null
? (size.height - backgroundSize.height) / 2
: 0,
);
canvas = _setCanvasPosition(canvas, size, trapezoid);
/// Calculate the angle between the topLeftOffset and the topRightOffset
double angle = calculateAngle(
Offset(
trapezoid.topLeftOffset.dx.toDouble(),
trapezoid.topLeftOffset.dy.toDouble(),
),
Offset(
trapezoid.topRightOffset.dx.toDouble(),
trapezoid.topRightOffset.dy.toDouble(),
),
);
if (!angle.isFinite) {
angle = 0;
}
/// Calculate the width according to the ratio width / angle.
double lerp = lerpDouble(
1,
2.8, // arbitrary value
((angle < 0 ? -angle : angle) / 90) *
((angle < 0 ? -angle : angle) / 90),
) ??
1;
/// idth calculated from modified canvas angle
double width =
(trapezoid.topRightOffset.dx - trapezoid.topLeftOffset.dx) * lerp;
/// Height calculated from modified canvas angle
double height =
trapezoid.bottomRightOffset.dy - trapezoid.topRightOffset.dy;
if (trapezoid.bottomLeftOffset.dy - trapezoid.topLeftOffset.dy >
height) {
height = trapezoid.bottomLeftOffset.dy - trapezoid.topLeftOffset.dy;
}
Paint paint = Paint()
..color = matchedCount.actualColor()
..style = PaintingStyle.stroke
..strokeWidth = 3;
double r = 15;
RRect fullRect = RRect.fromRectAndRadius(
Rect.fromCenter(
center: Offset((width) / 2, (height) / 2),
width: width + padding,
height: height + padding,
),
Radius.circular(r),
);
canvas.drawRRect(
fullRect,
paint,
);
String? moduleLabel = module.label;
if (moduleLabel != null && matchedCount.validated) {
_paintLabel(
canvas,
moduleLabel,
module.color,
);
} else if (!matchedCount.validated) {
Paint paint = Paint()
..color = matchedCount.actualColor()
..style = PaintingStyle.fill
..strokeWidth = 0;
RRect fullRect2 = RRect.fromRectAndRadius(
Rect.fromPoints(
Offset(
fullRect.left,
fullRect.top,
),
Offset(
fullRect.right * (matchedCount.progressCorrelation() / 100),
fullRect.bottom,
),
),
Radius.circular(r),
);
canvas.drawRRect(
fullRect2,
paint,
);
}
canvas.restore();
}
},
);
}