-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_scan_widget.dart
376 lines (349 loc) · 11.1 KB
/
qr_scan_widget.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:qr_scan/qr_scan.dart';
class QrScanWidget extends StatefulWidget {
final Function(String) onScan;
final double scanBoxRatio;
final Color boxLineColor;
QrScanWidget({
Key key,
@required this.onScan,
this.boxLineColor = Colors.blueAccent,
this.scanBoxRatio = 0.7,
}) : super(key: key);
@override
QrScanWidgetState createState() => new QrScanWidgetState();
}
class QrScanWidgetState extends State<QrScanWidget>
with TickerProviderStateMixin, WidgetsBindingObserver {
QrReaderViewController _controller;
AnimationController _animationController;
bool openFlashlight;
Timer _timer;
bool _isCameraPermission = false;
String _scanStr = '';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
openFlashlight = false;
_permissionStatus();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
_permissionStatus();
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
_permissionStatus() async {
Permission.camera.request().then((status) {
_isCameraPermission = status.isGranted;
if (status.isGranted) {
startScan();
}
setState(() {});
});
}
void _initAnimation() {
if (_animationController == null) {
_animationController = AnimationController(
vsync: this, duration: Duration(milliseconds: 1000));
_animationController
..addListener(_upState)
..addStatusListener((state) {
if (state == AnimationStatus.completed) {
_timer = Timer(Duration(seconds: 1), () {
_animationController?.reverse(from: 1.0);
});
} else if (state == AnimationStatus.dismissed) {
_timer = Timer(Duration(seconds: 1), () {
_animationController?.forward(from: 0.0);
});
}
});
_animationController.forward(from: 0.0);
}
}
void _clearAnimation() {
_timer?.cancel();
if (_animationController != null) {
_animationController?.dispose();
_animationController = null;
}
}
void _upState() {
setState(() {});
}
void _onCreateController(QrReaderViewController controller) async {
_controller = controller;
}
bool isScan = false;
Future _onQrBack(data, _) async {
if (isScan == true) return;
isScan = true;
stopScan();
await widget.onScan(data);
_scanStr = data;
setState(() {});
}
void startScan() {
isScan = false;
_controller?.startScan(_onQrBack);
_initAnimation();
}
void stopScan() {
_clearAnimation();
_controller?.stopScan();
}
Future _scanImage() async {
try {
PermissionStatus photosStatus = await Permission.photos.request();
if (photosStatus.isGranted) {
stopScan();
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image == null) {
startScan();
return;
}
final rest = await QrScan.imgScan(image);
await widget.onScan(rest);
_scanStr = rest;
setState(() {});
startScan();
} else {
// Open photo album permissions
openAppSettings();
}
} catch (e) {
if (Platform.isAndroid && e is PlatformException) {
if (e.code == "photo_access_denied") {
openAppSettings();
}
}
}
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final qrScanSize = constraints.maxWidth * widget.scanBoxRatio;
return Stack(
alignment: Alignment.center,
children: <Widget>[
SizedBox(
width: constraints.maxWidth,
height: constraints.maxHeight,
child: _isCameraPermission ? QrReaderView(
width: constraints.maxWidth,
height: constraints.maxHeight,
callback: _onCreateController,
) : Container(
child: QrReaderView(
width: 300,
height: 300,
callback: (controller){
controller.startScan((data,_){
// scan success
});
controller.stopScan();
controller.setFlashlight();
},
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: qrScanSize,
height: qrScanSize,
margin: EdgeInsets.only(bottom: 20),
child: Stack(
alignment: Alignment.bottomCenter,
children: [
CustomPaint(
painter: QrScanBoxPainter(
boxLineColor: widget.boxLineColor,
animationValue: _animationController?.value ?? 0,
isForward: _animationController?.status ==
AnimationStatus.forward,
),
child: SizedBox(
width: qrScanSize,
height: qrScanSize,
),
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () async {
openFlashlight = !openFlashlight;
_controller.setFlashlight();
setState(() {});
},
child: Container(
margin: EdgeInsets.only(bottom: 10),
child: Icon(
Icons.highlight_rounded,
size: 35,
color: openFlashlight
? Theme.of(context).primaryColor
: Colors.grey,
),
),
)
],
),
),
Text(
"Please place the QR code in the box",
style: TextStyle(color: Theme.of(context).primaryColor),
),
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _scanImage,
child: Container(
width: 45,
height: 45,
alignment: Alignment.center,
child: Icon(Icons.photo, size: 35, color: Colors.white54),
),
),
],
),
Positioned(
top: 80,
child: !_isCameraPermission
? GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
openAppSettings();
},
child: Container(
padding:
EdgeInsets.symmetric(vertical: 4, horizontal: 8),
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.all(
Radius.circular(24), // 也可控件一边圆角大小
)),
child: Text(
'To allow camera permissions, click Settings',
style: TextStyle(color: Colors.white),
)))
: Container(
margin: EdgeInsets.symmetric(horizontal: 20),
child: Text(_scanStr,
style:
TextStyle(color: Theme.of(context).primaryColor))),
),
],
);
});
}
@override
void dispose() {
stopScan();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
class QrScanBoxPainter extends CustomPainter {
final double animationValue;
final bool isForward;
final Color boxLineColor;
QrScanBoxPainter(
{@required this.animationValue,
@required this.isForward,
this.boxLineColor})
: assert(animationValue != null),
assert(isForward != null);
@override
void paint(Canvas canvas, Size size) {
final borderRadius = BorderRadius.all(Radius.circular(12)).toRRect(
Rect.fromLTWH(0, 0, size.width, size.height),
);
canvas.drawRRect(
borderRadius,
Paint()
..color = Colors.white54
..style = PaintingStyle.stroke
..strokeWidth = 1,
);
final borderPaint = Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = 2;
final path = new Path();
// leftTop
path.moveTo(0, 50);
path.lineTo(0, 12);
path.quadraticBezierTo(0, 0, 12, 0);
path.lineTo(50, 0);
// rightTop
path.moveTo(size.width - 50, 0);
path.lineTo(size.width - 12, 0);
path.quadraticBezierTo(size.width, 0, size.width, 12);
path.lineTo(size.width, 50);
// rightBottom
path.moveTo(size.width, size.height - 50);
path.lineTo(size.width, size.height - 12);
path.quadraticBezierTo(
size.width, size.height, size.width - 12, size.height);
path.lineTo(size.width - 50, size.height);
// leftBottom
path.moveTo(50, size.height);
path.lineTo(12, size.height);
path.quadraticBezierTo(0, size.height, 0, size.height - 12);
path.lineTo(0, size.height - 50);
canvas.drawPath(path, borderPaint);
canvas.clipRRect(
BorderRadius.all(Radius.circular(12)).toRRect(Offset.zero & size));
// 绘制横向网格
final linePaint = Paint();
final lineSize = size.height * 0.45;
final leftPress = (size.height + lineSize) * animationValue - lineSize;
linePaint.style = PaintingStyle.stroke;
linePaint.shader = LinearGradient(
colors: [Colors.transparent, boxLineColor],
begin: isForward ? Alignment.topCenter : Alignment(0.0, 2.0),
end: isForward ? Alignment(0.0, 0.5) : Alignment.topCenter,
).createShader(Rect.fromLTWH(0, leftPress, size.width, lineSize));
for (int i = 0; i < size.height / 5; i++) {
canvas.drawLine(
Offset(
i * 5.0,
leftPress,
),
Offset(i * 5.0, leftPress + lineSize),
linePaint,
);
}
for (int i = 0; i < lineSize / 5; i++) {
canvas.drawLine(
Offset(0, leftPress + i * 5.0),
Offset(
size.width,
leftPress + i * 5.0,
),
linePaint,
);
}
}
@override
bool shouldRepaint(QrScanBoxPainter oldDelegate) =>
animationValue != oldDelegate.animationValue;
@override
bool shouldRebuildSemantics(QrScanBoxPainter oldDelegate) =>
animationValue != oldDelegate.animationValue;
}