Skip to content

Commit 920ebbe

Browse files
committed
add support for border radius & border styling to scan window painter
1 parent 2fc1290 commit 920ebbe

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

lib/src/overlay/scan_window_painter.dart

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,34 @@ import 'package:flutter/material.dart';
44
class ScanWindowPainter extends CustomPainter {
55
/// Construct a new [ScanWindowPainter] instance.
66
const ScanWindowPainter({
7+
required this.borderColor,
8+
required this.borderRadius,
9+
required this.borderStrokeCap,
10+
required this.borderStrokeJoin,
11+
required this.borderStyle,
12+
required this.borderWidth,
713
required this.color,
814
required this.scanWindow,
915
});
1016

17+
/// The color for the scan window border.
18+
final Color borderColor;
19+
20+
/// The border radius for the scan window and its border.
21+
final BorderRadius borderRadius;
22+
23+
/// The stroke cap for the border around the scan window.
24+
final StrokeCap borderStrokeCap;
25+
26+
/// The stroke join for the border around the scan window.
27+
final StrokeJoin borderStrokeJoin;
28+
29+
/// The style for the border around the scan window.
30+
final PaintingStyle borderStyle;
31+
32+
/// The width for the border around the scan window.
33+
final double borderWidth;
34+
1135
/// The color for the scan window box.
1236
final Color color;
1337

@@ -20,22 +44,53 @@ class ScanWindowPainter extends CustomPainter {
2044
return;
2145
}
2246

23-
// Define the main overlay path covering the entire screen
47+
// Define the main overlay path covering the entire screen.
2448
final backgroundPath = Path()..addRect(Offset.zero & size);
2549

26-
// Define the cutout path in the center
27-
final cutoutPath = Path()..addRect(scanWindow);
50+
// The cutout rect depends on the border radius.
51+
final RRect cutoutRect = borderRadius == BorderRadius.zero
52+
? RRect.fromRectAndCorners(scanWindow)
53+
: RRect.fromRectAndCorners(
54+
scanWindow,
55+
topLeft: borderRadius.topLeft,
56+
topRight: borderRadius.topRight,
57+
bottomLeft: borderRadius.bottomLeft,
58+
bottomRight: borderRadius.bottomRight,
59+
);
60+
61+
// The cutout path is always in the center.
62+
final Path cutoutPath = Path()..addRRect(cutoutRect);
2863

2964
// Combine the two paths: overlay minus the cutout area
30-
final overlayWithCutoutPath = Path.combine(PathOperation.difference, backgroundPath, cutoutPath);
65+
final Path overlayWithCutoutPath = Path.combine(
66+
PathOperation.difference,
67+
backgroundPath,
68+
cutoutPath,
69+
);
70+
71+
final Paint overlayWithCutoutPaint = Paint()
72+
..color = color
73+
..style = PaintingStyle.fill
74+
..blendMode = BlendMode.dstOver;
75+
76+
final Paint borderPaint = Paint()
77+
..color = borderColor
78+
..style = borderStyle
79+
..strokeWidth = borderWidth
80+
..strokeCap = borderStrokeCap
81+
..strokeJoin = borderStrokeJoin;
82+
83+
// Paint the overlay with the cutout.
84+
canvas.drawPath(overlayWithCutoutPath, overlayWithCutoutPaint);
3185

32-
// Paint the overlay with the cutout
33-
final paint = Paint()..color = color;
34-
canvas.drawPath(overlayWithCutoutPath, paint);
86+
// Then, draw the border around the cutout area.
87+
canvas.drawRRect(cutoutRect, borderPaint);
3588
}
3689

3790
@override
3891
bool shouldRepaint(ScanWindowPainter oldDelegate) {
39-
return oldDelegate.scanWindow != scanWindow || oldDelegate.color != color;
92+
return oldDelegate.scanWindow != scanWindow ||
93+
oldDelegate.color != color ||
94+
oldDelegate.borderRadius != borderRadius;
4095
}
4196
}

0 commit comments

Comments
 (0)