Skip to content

Commit 2fc1290

Browse files
committed
clean up the scan window overlay
1 parent 7d3f4c8 commit 2fc1290

File tree

5 files changed

+56
-42
lines changed

5 files changed

+56
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Bugs fixed:
1111

1212
Improvements:
1313
* Added a basic barcode overlay widget, for use with the camera preview.
14+
* Added a basic scan window overlay widget, for use with the camera preview.
1415
* Update the bundled MLKit model for Android to version `17.3.0`.
1516
* Added documentation in places where it was missing.
1617
* Added `color` and `style` properties to the `BarcodePainter` widget.

lib/mobile_scanner.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export 'src/objects/url_bookmark.dart';
2929
export 'src/objects/wifi.dart';
3030
export 'src/overlay/barcode_overlay.dart';
3131
export 'src/overlay/barcode_painter.dart';
32-
export 'src/overlay/scanner_overlay.dart';
33-
export 'src/overlay/scanner_painter.dart';
32+
export 'src/overlay/scan_window_overlay.dart';
33+
export 'src/overlay/scan_window_painter.dart';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
3+
import 'package:mobile_scanner/src/overlay/scan_window_painter.dart';
4+
5+
/// This widget represents an overlay that paints a scan window cutout.
6+
class ScanWindowOverlay extends StatelessWidget {
7+
/// Construct a new [ScanWindowOverlay] instance.
8+
const ScanWindowOverlay({
9+
super.key,
10+
required this.controller,
11+
required this.scanWindow,
12+
this.color = const Color(0x80000000),
13+
});
14+
15+
/// The color for the scan window box.
16+
///
17+
/// Defaults to [Colors.black] with 50% opacity.
18+
final Color color;
19+
20+
/// The controller that manages the camera preview.
21+
final MobileScannerController controller;
22+
23+
/// The scan window for the overlay.
24+
final Rect scanWindow;
25+
26+
@override
27+
Widget build(BuildContext context) {
28+
if (scanWindow.isEmpty || scanWindow.isInfinite) {
29+
return const SizedBox();
30+
}
31+
32+
return ValueListenableBuilder(
33+
valueListenable: controller,
34+
builder: (context, value, child) {
35+
// Not ready.
36+
if (!value.isInitialized || !value.isRunning || value.error != null || value.size.isEmpty) {
37+
return const SizedBox();
38+
}
39+
40+
return CustomPaint(
41+
size: value.size,
42+
painter: ScanWindowPainter(
43+
scanWindow: scanWindow,
44+
color: color,
45+
),
46+
);
47+
},
48+
);
49+
}
50+
}

lib/src/overlay/scan_window_painter.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import 'package:flutter/material.dart';
44
class ScanWindowPainter extends CustomPainter {
55
/// Construct a new [ScanWindowPainter] instance.
66
const ScanWindowPainter({
7+
required this.color,
78
required this.scanWindow,
8-
this.color = const Color(0x80000000),
99
});
1010

1111
/// The color for the scan window box.
1212
final Color color;
1313

14-
/// The rectangle defining the scan window.
15-
///
16-
/// Defaults to [Colors.black] with 50% opacity.
14+
/// The rectangle that defines the scan window.
1715
final Rect scanWindow;
1816

1917
@override
@@ -32,7 +30,7 @@ class ScanWindowPainter extends CustomPainter {
3230
final overlayWithCutoutPath = Path.combine(PathOperation.difference, backgroundPath, cutoutPath);
3331

3432
// Paint the overlay with the cutout
35-
final paint = Paint()..color = Colors.black.withOpacity(0.5);
33+
final paint = Paint()..color = color;
3634
canvas.drawPath(overlayWithCutoutPath, paint);
3735
}
3836

lib/src/overlay/scanner_overlay.dart

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)