barcode_scanner_picklist.dart
6.29 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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/picklist/classes/detect_collision.dart';
import 'package:mobile_scanner_example/picklist/widgets/crosshair.dart';
import 'package:mobile_scanner_example/picklist/widgets/draw_detected_barcodes.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
class BarcodeScannerPicklist extends StatefulWidget {
const BarcodeScannerPicklist({super.key});
@override
State<BarcodeScannerPicklist> createState() => _BarcodeScannerPicklistState();
}
class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
with WidgetsBindingObserver {
final _mobileScannerController = MobileScannerController(autoStart: false);
StreamSubscription<Object?>? _barcodesSubscription;
final _scannerDisabled = ValueNotifier(false);
late final Offset _crosshair;
bool barcodeDetected = false;
@override
void didChangeDependencies() {
_crosshair = MediaQuery.sizeOf(context).center(Offset.zero);
super.didChangeDependencies();
}
@override
void initState() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WidgetsBinding.instance.addObserver(this);
_barcodesSubscription = _mobileScannerController.barcodes.listen(
_handleBarcodes,
);
super.initState();
unawaited(_mobileScannerController.start());
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (!_mobileScannerController.value.isInitialized) {
return;
}
switch (state) {
case AppLifecycleState.detached:
case AppLifecycleState.hidden:
case AppLifecycleState.paused:
return;
case AppLifecycleState.resumed:
_barcodesSubscription =
_mobileScannerController.barcodes.listen(_handleBarcodes);
unawaited(_mobileScannerController.start());
case AppLifecycleState.inactive:
unawaited(_barcodesSubscription?.cancel());
_barcodesSubscription = null;
unawaited(_mobileScannerController.stop());
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
unawaited(_barcodesSubscription?.cancel());
_barcodesSubscription = null;
super.dispose();
_mobileScannerController.dispose();
}
void _handleBarcodes(BarcodeCapture barcodes) {
if (_scannerDisabled.value) {
return;
}
for (final barcode in barcodes.barcodes) {
if (isOffsetInsideShape(
_crosshair,
barcode.corners,
)) {
if (!barcodeDetected) {
barcodeDetected = true;
Navigator.of(context).pop(barcode);
}
return;
}
}
}
@override
Widget build(BuildContext context) {
const boxFit = BoxFit.contain;
return PopScope(
onPopInvokedWithResult: (didPop, result) {
if (didPop) {
SystemChrome.setPreferredOrientations([...DeviceOrientation.values]);
}
},
child: Scaffold(
appBar: AppBar(title: const Text('Picklist scanner')),
backgroundColor: Colors.black,
body: StreamBuilder(
stream: _mobileScannerController.barcodes,
builder: (context, snapshot) {
final barcodes = snapshot.data;
if (barcodes == null) {
debugPrint('ISNULL');
}
return Listener(
behavior: HitTestBehavior.opaque,
onPointerDown: (_) => _scannerDisabled.value = true,
onPointerUp: (_) => _scannerDisabled.value = false,
onPointerCancel: (_) => _scannerDisabled.value = false,
child: Stack(
fit: StackFit.expand,
children: [
MobileScanner(
controller: _mobileScannerController,
errorBuilder: (context, error, child) {
return ScannerErrorWidget(error: error);
},
fit: boxFit,
),
...drawDetectedBarcodes(
barcodes: barcodes?.barcodes,
cameraPreviewSize: _mobileScannerController.value.size,
fit: boxFit,
),
ValueListenableBuilder(
valueListenable: _scannerDisabled,
builder: (context, value, child) {
return Crosshair(
scannerDisabled: value,
);
},
),
],
),
);
},
)
// body: Stack(
// fit: StackFit.expand,
// children: [
// MobileScanner(
// controller: _mobileScannerController,
// errorBuilder: (context, error, child) {
// return ScannerErrorWidget(error: error);
// },
// fit: boxFit,
// ),
// ...drawDetectedBarcodes(
// controller: _mobileScannerController,
// cameraPreviewSize: _mobileScannerController.value.size,
// fit: boxFit,
// ),
// ...drawDetectedBarcodes(
// controller: _mobileScannerController,
// cameraPreviewSize: _mobileScannerController.value.size,
// fit: boxFit,
// ),
// // barcodes: _mobileScannerController. value. _barcodes.value,
// // cameraPreviewSize: _mobileScannerController.value.size,
// // fit: boxFit,
// // ),
// CustomPaint(
// painter: BarcodeOverlay(
// barcodeCorners: [
// const Offset(0, 0),
// const Offset(50, 0),
// const Offset(50, 50),
// const Offset(0, 50)
// ],
// barcodeSize: Size(50, 50),
// boxFit: boxFit,
// cameraPreviewSize: _mobileScannerController.value.size,
// ),
// ),
// Crosshair(
// crosshairRectangle: _crosshairRectangle,
// scannerDisabled: _scannerDisabled.value,
// ),
// ],
// ),
,
),
);
}
}