paintPathStroke static method
void
paintPathStroke(})
Implementation
static void paintPathStroke(
Canvas canvas,
Path path,
Color color, {
required double strokeWidth,
double strokeAlign = 0.0,
StrokeCap strokeCap = .butt,
StrokeJoin strokeJoin = .miter,
double strokeMiterLimit = 4.0,
}) {
// Early return if the stroke would be invisible.
if (strokeWidth == 0.0 || color.a <= 0.0) return;
// Explicitly set miter limit to 0 if not using miter.
if (strokeJoin != .miter) strokeMiterLimit = 0.0;
final paint = Paint()
..style = .stroke
..color = color
..strokeCap = strokeCap
..strokeJoin = strokeJoin
..strokeMiterLimit = strokeMiterLimit;
// The most optimal path is when the stroke is center-aligned.
// BorderSide.strokeAlignCenter is explicitly not used here.
if (strokeAlign == 0.0) {
paint.strokeWidth = strokeWidth;
canvas.drawPath(path, paint);
return;
}
// Inlined from [BorderSide].
final strokeInset = strokeWidth * (1.0 - (1.0 + strokeAlign) / 2.0);
final strokeOutset = strokeWidth * (1.0 + strokeAlign) / 2.0;
final minOffset = math.min(strokeInset, strokeOutset);
final maxOffset = math.max(strokeInset, strokeOutset);
// Compute the minimum safe bounds for the layer and clip path.
final maxMiterLength = math.max(maxOffset, maxOffset * strokeMiterLimit);
final bounds = path.getBounds().inflate(maxMiterLength);
// Save layer is needed for compound strokes with a translucent color.
final needsSaveLayer =
color.a < 1.0 && strokeInset > 0.0 && strokeOutset > 0.0;
// Apply opacity to everything.
if (needsSaveLayer) {
canvas.saveLayer(
bounds,
Paint()..color = color.withValues(red: 0.0, green: 0.0, blue: 0.0),
);
paint.color = color.withValues(alpha: 1.0);
}
// Paint the lesser offset.
if (minOffset > 0.0) {
paint.strokeWidth = 2.0 * minOffset;
canvas.drawPath(path, paint);
}
// Paint the greater offset.
paint.strokeWidth = 2.0 * maxOffset;
if (strokeInset > strokeOutset) {
// Clip the inside of the path.
canvas
..save()
..clipPath(path)
..drawPath(path, paint)
..restore();
} else {
// Clip the outside of the path.
canvas
..save()
..clipPath(.combine(.difference, Path()..addRect(bounds), path))
..drawPath(path, paint)
..restore();
}
// Don't forget to restore the canvas if needed.
if (needsSaveLayer) {
canvas.restore();
}
}