Skip to content

Commit f343685

Browse files
committed
use the new widget in the sample
1 parent 30f4b6a commit f343685

File tree

5 files changed

+20
-73
lines changed

5 files changed

+20
-73
lines changed

example/lib/barcode_scanner_window.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class _BarcodeScannerWithScanWindowState
4444
},
4545
),
4646
BarcodeOverlay(controller: controller, boxFit: boxFit),
47-
ScannerOverlay(
47+
ScanWindowOverlay(
4848
scanWindow: scanWindow,
4949
controller: controller,
5050
),

example/lib/mobile_scanner_overlay.dart

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
5757
builder: (context, value, child) {
5858
if (!value.isInitialized ||
5959
!value.isRunning ||
60-
value.error != null) {
60+
value.error != null ||
61+
scanWindow.isEmpty) {
6162
return const SizedBox();
6263
}
6364

64-
return CustomPaint(
65-
painter: ScannerOverlay(scanWindow: scanWindow),
65+
return ScanWindowOverlay(
66+
controller: controller,
67+
scanWindow: scanWindow,
6668
);
6769
},
6870
),
@@ -90,67 +92,3 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
9092
await controller.dispose();
9193
}
9294
}
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-
}

lib/src/overlay/barcode_overlay.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class BarcodeOverlay extends StatelessWidget {
4444
final BarcodeCapture? barcodeCapture = snapshot.data;
4545

4646
// No barcode or preview size.
47-
if (barcodeCapture == null || barcodeCapture.size.isEmpty || barcodeCapture.barcodes.isEmpty) {
47+
if (barcodeCapture == null ||
48+
barcodeCapture.size.isEmpty ||
49+
barcodeCapture.barcodes.isEmpty) {
4850
return const SizedBox();
4951
}
5052

lib/src/overlay/barcode_painter.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class BarcodePainter extends CustomPainter {
3434

3535
@override
3636
void paint(Canvas canvas, Size size) {
37-
if (barcodeCorners.isEmpty || barcodeSize.isEmpty || cameraPreviewSize.isEmpty) {
37+
if (barcodeCorners.isEmpty ||
38+
barcodeSize.isEmpty ||
39+
cameraPreviewSize.isEmpty) {
3840
return;
3941
}
4042

@@ -54,8 +56,10 @@ class BarcodePainter extends CustomPainter {
5456
horizontalPadding = 0;
5557
}
5658

57-
final double ratioWidth = cameraPreviewSize.width / adjustedSize.destination.width;
58-
final double ratioHeight = cameraPreviewSize.height / adjustedSize.destination.height;
59+
final double ratioWidth =
60+
cameraPreviewSize.width / adjustedSize.destination.width;
61+
final double ratioHeight =
62+
cameraPreviewSize.height / adjustedSize.destination.height;
5963

6064
final List<Offset> adjustedOffset = [
6165
for (final offset in barcodeCorners)

lib/src/overlay/scan_window_overlay.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ class ScanWindowOverlay extends StatelessWidget {
6969
valueListenable: controller,
7070
builder: (context, value, child) {
7171
// Not ready.
72-
if (!value.isInitialized || !value.isRunning || value.error != null || value.size.isEmpty) {
72+
if (!value.isInitialized ||
73+
!value.isRunning ||
74+
value.error != null ||
75+
value.size.isEmpty) {
7376
return const SizedBox();
7477
}
7578

0 commit comments

Comments
 (0)