mobile_scanner.dart
10.2 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import 'dart:async';
import 'package:flutter/foundation.dart';
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/objects/barcode_capture.dart';
import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart';
/// The function signature for the error builder.
typedef MobileScannerErrorBuilder = Widget Function(
BuildContext,
MobileScannerException,
Widget?,
);
/// The [MobileScanner] widget displays a live camera preview.
class MobileScanner extends StatefulWidget {
/// The controller that manages the barcode scanner.
///
/// If this is null, the scanner will manage its own controller.
final MobileScannerController? controller;
/// The function that builds an error widget when the scanner
/// could not be started.
///
/// If this is null, defaults to a black [ColoredBox]
/// with a centered white [Icons.error] icon.
final MobileScannerErrorBuilder? errorBuilder;
/// The [BoxFit] for the camera preview.
///
/// Defaults to [BoxFit.cover].
final BoxFit fit;
/// The function that signals when new codes were detected by the [controller].
final void Function(BarcodeCapture barcodes) onDetect;
/// The function that signals when the barcode scanner is started.
@Deprecated('Use onScannerStarted() instead.')
final void Function(MobileScannerArguments? arguments)? onStart;
/// The function that signals when the barcode scanner is started.
final void Function(MobileScannerArguments? arguments)? onScannerStarted;
/// The function that builds a placeholder widget when the scanner
/// is not yet displaying its camera preview.
///
/// If this is null, a black [ColoredBox] is used as placeholder.
final Widget Function(BuildContext, Widget?)? placeholderBuilder;
/// if set barcodes will only be scanned if they fall within this [Rect]
/// useful for having a cut-out overlay for example. these [Rect]
/// coordinates are relative to the widget size, so by how much your
/// rectangle overlays the actual image can depend on things like the
/// [BoxFit]
final Rect? scanWindow;
/// Only set this to true if you are starting another instance of mobile_scanner
/// right after disposing the first one, like in a PageView.
///
/// Default: false
final bool startDelay;
/// The overlay which will be painted above the scanner when has started successful.
/// Will no be pointed when an error occurs or the scanner hasn't be started yet.
final Widget? overlay;
/// Create a new [MobileScanner] using the provided [controller]
/// and [onBarcodeDetected] callback.
const MobileScanner({
this.controller,
this.errorBuilder,
this.fit = BoxFit.cover,
required this.onDetect,
@Deprecated('Use onScannerStarted() instead.') this.onStart,
this.onScannerStarted,
this.placeholderBuilder,
this.scanWindow,
this.startDelay = false,
this.overlay,
super.key,
});
@override
State<MobileScanner> createState() => _MobileScannerState();
}
class _MobileScannerState extends State<MobileScanner>
with WidgetsBindingObserver {
/// The subscription that listens to barcode detection.
StreamSubscription<BarcodeCapture>? _barcodesSubscription;
/// The internally managed controller.
late MobileScannerController _controller;
/// Whether the controller should resume
/// when the application comes back to the foreground.
bool _resumeFromBackground = false;
MobileScannerException? _startException;
Widget _buildPlaceholderOrError(BuildContext context, Widget? child) {
final error = _startException;
if (error != null) {
return widget.errorBuilder?.call(context, error, child) ??
const ColoredBox(
color: Colors.black,
child: Center(child: Icon(Icons.error, color: Colors.white)),
);
}
return widget.placeholderBuilder?.call(context, child) ??
const ColoredBox(color: Colors.black);
}
/// Start the given [scanner].
Future<void> _startScanner() async {
if (widget.startDelay) {
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
}
_barcodesSubscription ??= _controller.barcodes.listen(
widget.onDetect,
);
if (!_controller.autoStart) {
debugPrint(
'mobile_scanner: not starting automatically because autoStart is set to false in the controller.',
);
return;
}
_controller.start().then((arguments) {
// ignore: deprecated_member_use_from_same_package
widget.onStart?.call(arguments);
widget.onScannerStarted?.call(arguments);
}).catchError((error) {
if (mounted) {
setState(() {
_startException = error as MobileScannerException;
});
}
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_controller = widget.controller ?? MobileScannerController();
_startScanner();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// App state changed before the controller was initialized.
if (_controller.isStarting) {
return;
}
switch (state) {
case AppLifecycleState.resumed:
if (_resumeFromBackground) {
_startScanner();
}
break;
case AppLifecycleState.inactive:
_resumeFromBackground = true;
_controller.stop();
break;
default:
break;
}
}
/// the [scanWindow] rect will be relative and scaled to the [widgetSize] not the texture. so it is possible,
/// depending on the [fit], for the [scanWindow] to partially or not at all overlap the [textureSize]
///
/// since when using a [BoxFit] the content will always be centered on its parent. we can convert the rect
/// to be relative to the texture.
///
/// since the textures size and the actuall image (on the texture size) might not be the same, we also need to
/// calculate the scanWindow in terms of percentages of the texture, not pixels.
Rect calculateScanWindowRelativeToTextureInPercentage(
BoxFit fit,
Rect scanWindow,
Size textureSize,
Size widgetSize,
) {
double fittedTextureWidth;
double fittedTextureHeight;
switch (fit) {
case BoxFit.contain:
final widthRatio = widgetSize.width / textureSize.width;
final heightRatio = widgetSize.height / textureSize.height;
final scale = widthRatio < heightRatio ? widthRatio : heightRatio;
fittedTextureWidth = textureSize.width * scale;
fittedTextureHeight = textureSize.height * scale;
break;
case BoxFit.cover:
final widthRatio = widgetSize.width / textureSize.width;
final heightRatio = widgetSize.height / textureSize.height;
final scale = widthRatio > heightRatio ? widthRatio : heightRatio;
fittedTextureWidth = textureSize.width * scale;
fittedTextureHeight = textureSize.height * scale;
break;
case BoxFit.fill:
fittedTextureWidth = widgetSize.width;
fittedTextureHeight = widgetSize.height;
break;
case BoxFit.fitHeight:
final ratio = widgetSize.height / textureSize.height;
fittedTextureWidth = textureSize.width * ratio;
fittedTextureHeight = widgetSize.height;
break;
case BoxFit.fitWidth:
final ratio = widgetSize.width / textureSize.width;
fittedTextureWidth = widgetSize.width;
fittedTextureHeight = textureSize.height * ratio;
break;
case BoxFit.none:
case BoxFit.scaleDown:
fittedTextureWidth = textureSize.width;
fittedTextureHeight = textureSize.height;
break;
}
final offsetX = (widgetSize.width - fittedTextureWidth) / 2;
final offsetY = (widgetSize.height - fittedTextureHeight) / 2;
final left = (scanWindow.left - offsetX) / fittedTextureWidth;
final top = (scanWindow.top - offsetY) / fittedTextureHeight;
final right = (scanWindow.right - offsetX) / fittedTextureWidth;
final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;
return Rect.fromLTRB(left, top, right, bottom);
}
Rect? scanWindow;
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
return ValueListenableBuilder<MobileScannerArguments?>(
valueListenable: _controller.startArguments,
builder: (context, value, child) {
if (value == null) {
return _buildPlaceholderOrError(context, child);
}
if (widget.scanWindow != null && scanWindow == null) {
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
value.size,
Size(constraints.maxWidth, constraints.maxHeight),
);
_controller.updateScanWindow(scanWindow);
}
if (widget.overlay != null) {
return Stack(
alignment: Alignment.center,
children: [
_scanner(value.size, value.webId, value.textureId),
widget.overlay!,
],
);
} else {
return _scanner(value.size, value.webId, value.textureId);
}
},
);
},
);
}
Widget _scanner(Size size, String? webId, int? textureId) {
return ClipRect(
child: LayoutBuilder(
builder: (_, constraints) {
return SizedBox.fromSize(
size: constraints.biggest,
child: FittedBox(
fit: widget.fit,
child: SizedBox(
width: size.width,
height: size.height,
child: kIsWeb
? HtmlElementView(viewType: webId!)
: Texture(textureId: textureId!),
),
),
);
},
),
);
}
@override
void dispose() {
_controller.updateScanWindow(null);
WidgetsBinding.instance.removeObserver(this);
_barcodesSubscription?.cancel();
_barcodesSubscription = null;
_controller.dispose();
super.dispose();
}
}