@@ -57,12 +57,14 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
57
57
builder: (context, value, child) {
58
58
if (! value.isInitialized ||
59
59
! value.isRunning ||
60
- value.error != null ) {
60
+ value.error != null ||
61
+ scanWindow.isEmpty) {
61
62
return const SizedBox ();
62
63
}
63
64
64
- return CustomPaint (
65
- painter: ScannerOverlay (scanWindow: scanWindow),
65
+ return ScanWindowOverlay (
66
+ controller: controller,
67
+ scanWindow: scanWindow,
66
68
);
67
69
},
68
70
),
@@ -90,67 +92,3 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
90
92
await controller.dispose ();
91
93
}
92
94
}
93
-
94
- class ScannerOverlay extends CustomPainter {
95
- const ScannerOverlay ({
96
- required this .scanWindow,
97
- this .borderRadius = 12.0 ,
98
- });
99
-
100
- final Rect scanWindow;
101
- final double borderRadius;
102
-
103
- @override
104
- void paint (Canvas canvas, Size size) {
105
- // TODO: use `Offset.zero & size` instead of Rect.largest
106
- // we need to pass the size to the custom paint widget
107
- final backgroundPath = Path ()..addRect (Rect .largest);
108
-
109
- final cutoutPath = Path ()
110
- ..addRRect (
111
- RRect .fromRectAndCorners (
112
- scanWindow,
113
- topLeft: Radius .circular (borderRadius),
114
- topRight: Radius .circular (borderRadius),
115
- bottomLeft: Radius .circular (borderRadius),
116
- bottomRight: Radius .circular (borderRadius),
117
- ),
118
- );
119
-
120
- final backgroundPaint = Paint ()
121
- ..color = Colors .black.withOpacity (0.5 )
122
- ..style = PaintingStyle .fill
123
- ..blendMode = BlendMode .dstOut;
124
-
125
- final backgroundWithCutout = Path .combine (
126
- PathOperation .difference,
127
- backgroundPath,
128
- cutoutPath,
129
- );
130
-
131
- final borderPaint = Paint ()
132
- ..color = Colors .white
133
- ..style = PaintingStyle .stroke
134
- ..strokeWidth = 4.0 ;
135
-
136
- final borderRect = RRect .fromRectAndCorners (
137
- scanWindow,
138
- topLeft: Radius .circular (borderRadius),
139
- topRight: Radius .circular (borderRadius),
140
- bottomLeft: Radius .circular (borderRadius),
141
- bottomRight: Radius .circular (borderRadius),
142
- );
143
-
144
- // First, draw the background,
145
- // with a cutout area that is a bit larger than the scan window.
146
- // Finally, draw the scan window itself.
147
- canvas.drawPath (backgroundWithCutout, backgroundPaint);
148
- canvas.drawRRect (borderRect, borderPaint);
149
- }
150
-
151
- @override
152
- bool shouldRepaint (ScannerOverlay oldDelegate) {
153
- return scanWindow != oldDelegate.scanWindow ||
154
- borderRadius != oldDelegate.borderRadius;
155
- }
156
- }
0 commit comments