mobile_scanner.dart
5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
import 'package:mobile_scanner/src/mobile_scanner_platform_interface.dart';
import 'package:mobile_scanner/src/objects/mobile_scanner_state.dart';
import 'package:mobile_scanner/src/scan_window_calculation.dart';
/// The function signature for the error builder.
typedef MobileScannerErrorBuilder = Widget Function(
BuildContext,
MobileScannerException,
Widget?,
);
/// This widget displays a live camera preview for the barcode scanner.
class MobileScanner extends StatefulWidget {
/// Create a new [MobileScanner] using the provided [controller].
const MobileScanner({
required this.controller,
this.fit = BoxFit.cover,
this.errorBuilder,
this.overlayBuilder,
this.placeholderBuilder,
this.scanWindow,
super.key,
});
/// The controller for the camera preview.
final MobileScannerController controller;
/// The error builder for the camera preview.
///
/// If this is null, a black [ColoredBox],
/// with a centered white [Icons.error] icon is used as error widget.
final MobileScannerErrorBuilder? errorBuilder;
/// The [BoxFit] for the camera preview.
///
/// Defaults to [BoxFit.cover].
final BoxFit fit;
/// The builder for the overlay above the camera preview.
///
/// The resulting widget can be combined with the [scanWindow] rectangle
/// to create a cutout for the camera preview.
///
/// The [BoxConstraints] for this builder
/// are the same constraints that are used to compute the effective [scanWindow].
///
/// The overlay is only displayed when the camera preview is visible.
final LayoutWidgetBuilder? overlayBuilder;
/// The placeholder builder for the camera preview.
///
/// If this is null, a black [ColoredBox] is used as placeholder.
///
/// The placeholder is displayed when the camera preview is being initialized.
final Widget Function(BuildContext, Widget?)? placeholderBuilder;
/// The scan window rectangle for the barcode scanner.
///
/// If this is not null, the barcode scanner will only scan barcodes
/// which intersect this rectangle.
///
/// The rectangle is relative to the layout size of the *camera preview widget*,
/// rather than the actual camera preview size,
/// since the actual widget size might not be the same as the camera preview size.
///
/// For example, the applied [fit] has an effect on the size of the camera preview widget,
/// while the camera preview size remains the same.
final Rect? scanWindow;
@override
State<MobileScanner> createState() => _MobileScannerState();
}
class _MobileScannerState extends State<MobileScanner> {
/// The current scan window.
Rect? scanWindow;
/// Recalculate the scan window based on the updated [constraints].
void _maybeUpdateScanWindow(
MobileScannerState scannerState,
BoxConstraints constraints,
) {
if (widget.scanWindow != null && scanWindow == null) {
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
textureSize: scannerState.size,
widgetSize: constraints.biggest,
);
unawaited(widget.controller.updateScanWindow(scanWindow));
}
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<MobileScannerState>(
valueListenable: widget.controller,
builder: (BuildContext context, MobileScannerState value, Widget? child) {
if (!value.isInitialized) {
const Widget defaultPlaceholder = ColoredBox(color: Colors.black);
return widget.placeholderBuilder?.call(context, child) ??
defaultPlaceholder;
}
final MobileScannerException? error = value.error;
if (error != null) {
const Widget defaultError = ColoredBox(
color: Colors.black,
child: Center(child: Icon(Icons.error, color: Colors.white)),
);
return widget.errorBuilder?.call(context, error, child) ??
defaultError;
}
return LayoutBuilder(
builder: (context, constraints) {
_maybeUpdateScanWindow(value, constraints);
final Widget? overlay =
widget.overlayBuilder?.call(context, constraints);
final Size cameraPreviewSize = value.size;
final Widget scannerWidget = ClipRect(
child: SizedBox.fromSize(
size: constraints.biggest,
child: FittedBox(
fit: widget.fit,
child: SizedBox(
width: cameraPreviewSize.width,
height: cameraPreviewSize.height,
child: MobileScannerPlatform.instance.buildCameraView(),
),
),
),
);
if (overlay == null) {
return scannerWidget;
}
return Stack(
alignment: Alignment.center,
children: <Widget>[
scannerWidget,
overlay,
],
);
},
);
},
);
}
@override
void dispose() {
// When this widget is unmounted, reset the scan window.
widget.controller.updateScanWindow(null);
super.dispose();
}
}