-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqr_code_scanner_screen.dart
334 lines (287 loc) · 8.65 KB
/
qr_code_scanner_screen.dart
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:wakelock_plus/wakelock_plus.dart' show WakelockPlus;
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../strings_context.dart';
import '../app_theme.dart';
/// QR code scanner screen.
///
/// Will return raw text in [Navigator] result.
class QRCodeScannerScreen extends StatefulWidget {
const QRCodeScannerScreen({super.key});
@override
State<QRCodeScannerScreen> createState() => _QRCodeScannerScreenState();
}
class _QRCodeScannerScreenState extends State<QRCodeScannerScreen> {
/// [MobileScannerController] to toggle torch.
final MobileScannerController _controller = MobileScannerController(
formats: [BarcodeFormat.qrCode],
);
/// Lock to prevent multiple snackbars.
bool _hasSnackBar = false;
@override
void initState() {
super.initState();
WakelockPlus.enable();
}
@override
void dispose() {
WakelockPlus.disable();
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final body = Stack(
children: [
// Scanner
MobileScanner(
controller: _controller,
onDetect: _handleBarcode,
),
// Back arrow
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: kScreenMargin.copyWith(
top: MediaQuery.paddingOf(context).top,
),
child: Semantics(
label: context.strings.qrCodeScannerBackSemantics,
button: true,
excludeSemantics: true,
child: SquareButton(
onPressed: () {
Navigator.maybePop(context);
},
child: Icon(
Icons.arrow_back,
color: Theme.of(context).colorScheme.onSurface,
),
),
),
),
),
// View finder
const Align(
alignment: Alignment.center,
child: _ViewFinder(),
),
// Toggle torch button + bottom info panel
Padding(
padding: kScreenMargin,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SquareButton(
onPressed: () {
_controller.toggleTorch();
},
child: ValueListenableBuilder<TorchState>(
valueListenable: _controller.torchState,
builder: (context, torchState, _) {
final icon = switch (torchState) {
TorchState.off => Icons.flashlight_on,
TorchState.on => Icons.flashlight_off,
};
final strings = context.strings;
final semanticsLabel = switch (torchState) {
TorchState.off => strings.qrCodeScannerTorchOnSemantics,
TorchState.on => strings.qrCodeScannerTorchOffSemantics,
};
return Semantics(
button: true,
label: semanticsLabel,
child: Icon(
icon,
color: Theme.of(context).colorScheme.onSurface,
),
);
},
),
),
const _InfoPanel(),
],
),
),
],
);
return Scaffold(
backgroundColor: Colors.black,
extendBodyBehindAppBar: true,
body: body,
);
}
// ignore: non_constant_identifier_names
Widget SquareButton({
VoidCallback? onPressed,
required Widget child,
}) {
final colors = Theme.of(context).colorScheme;
const size = Size(kMinInteractiveDimension, kMinInteractiveDimension);
return FilledButton(
onPressed: onPressed,
style: FilledButton.styleFrom(
minimumSize: size,
padding: EdgeInsets.zero,
backgroundColor: colors.surface,
),
child: child,
);
}
void _handleBarcode(BarcodeCapture barcodes) {
final barcode = barcodes.barcodes.firstOrNull;
if (barcode == null) {
return;
}
if (!(barcode.type == BarcodeType.text ||
barcode.type == BarcodeType.url)) {
final strings = context.strings;
final message = strings.qrCodeScannerUnsupportedContentErrorMessage;
_showError(message);
return;
}
// Stop camera and code scanner
_controller.dispose();
ScaffoldMessenger.of(context).hideCurrentSnackBar();
Navigator.of(context).maybePop(barcode.rawValue);
}
void _showError(String message) {
if (_hasSnackBar) {
return;
}
_hasSnackBar = true;
final snackBar = SnackBar(
content: Text(message),
action: SnackBarAction(
label: context.strings.buttonOKLabel,
onPressed: () {},
),
);
ScaffoldMessenger.of(context)
.showSnackBar(snackBar)
.closed
.then((value) => _hasSnackBar = false);
}
}
/// View with borders only in corners.
class _ViewFinder extends StatelessWidget {
const _ViewFinder();
@override
Widget build(BuildContext context) {
final color = Theme.of(context).colorScheme.primary;
return CustomPaint(
foregroundPainter: _BorderPainter(color),
child: const SizedBox.square(dimension: 200),
);
}
}
/// Custom painter for [_ViewFinder].
class _BorderPainter extends CustomPainter {
final Paint _paint;
_BorderPainter(Color color)
: _paint = Paint()
..color = color
..strokeWidth = 3
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
@override
void paint(Canvas canvas, Size size) {
final width = size.width;
final height = size.height;
final cornerSide = height * 0.08;
final offsetLine = height * 0.08;
final path = Path()
// top left:
..moveTo(cornerSide + offsetLine, 0)
..lineTo(cornerSide, 0)
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..lineTo(0, cornerSide + offsetLine)
// bottom left:
..moveTo(0, height - cornerSide - offsetLine)
..lineTo(0, height - cornerSide)
..quadraticBezierTo(0, height, cornerSide, height)
..lineTo(cornerSide + offsetLine, height)
// bottom right
..moveTo(width - cornerSide - offsetLine, height)
..lineTo(width - cornerSide, height)
..quadraticBezierTo(width, height, width, height - cornerSide)
..lineTo(width, height - cornerSide - offsetLine)
// top right
..moveTo(width, cornerSide + offsetLine)
..lineTo(width, cornerSide)
..quadraticBezierTo(width, 0, width - cornerSide, 0)
..lineTo(width - cornerSide - offsetLine, 0);
canvas.drawPath(path, _paint);
}
@override
bool shouldRepaint(_BorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(_BorderPainter oldDelegate) => false;
}
/// Info panel with "i" icon and text.
class _InfoPanel extends StatelessWidget {
const _InfoPanel();
@override
Widget build(BuildContext context) {
const double iconSize = 80;
return Stack(
alignment: Alignment.topCenter,
children: [
Padding(
padding: const EdgeInsets.only(top: iconSize / 2),
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: const BorderRadius.all(Radius.circular(16)),
),
width: double.infinity,
padding:
const EdgeInsets.all(16).add(const EdgeInsets.only(top: 16)),
child: Text(
context.strings.qrCodeScannerInfoBody,
style: const TextStyle(fontSize: 14),
textAlign: TextAlign.center,
),
),
),
SvgPicture.asset(
"assets/images/info.svg",
width: iconSize,
height: iconSize,
),
],
);
}
}
@widgetbook.UseCase(
path: '[Core]',
name: '',
type: _InfoPanel,
)
Widget previewInfoPanel(BuildContext context) {
return const Center(
child: _InfoPanel(),
);
}
@widgetbook.UseCase(
path: '[Core]',
name: '',
type: _ViewFinder,
)
Widget previewViewFinder(BuildContext context) {
return const Center(
child: _ViewFinder(),
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: '',
type: QRCodeScannerScreen,
)
Widget previewQRCodeScannerScreen(BuildContext context) {
return const QRCodeScannerScreen();
}