@@ -4,10 +4,34 @@ import 'package:flutter/material.dart';
4
4
class ScanWindowPainter extends CustomPainter {
5
5
/// Construct a new [ScanWindowPainter] instance.
6
6
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,
7
13
required this .color,
8
14
required this .scanWindow,
9
15
});
10
16
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
+
11
35
/// The color for the scan window box.
12
36
final Color color;
13
37
@@ -20,22 +44,53 @@ class ScanWindowPainter extends CustomPainter {
20
44
return ;
21
45
}
22
46
23
- // Define the main overlay path covering the entire screen
47
+ // Define the main overlay path covering the entire screen.
24
48
final backgroundPath = Path ()..addRect (Offset .zero & size);
25
49
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);
28
63
29
64
// 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);
31
85
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);
35
88
}
36
89
37
90
@override
38
91
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;
40
95
}
41
96
}
0 commit comments