1
- import 'dart:io' ;
2
-
3
1
import 'package:flutter/material.dart' ;
4
2
import 'package:flutter_riverpod/flutter_riverpod.dart' ;
5
- import 'package:qr_code_scanner/qr_code_scanner .dart' ;
3
+ import 'package:mobile_scanner/mobile_scanner .dart' ;
6
4
import 'package:ticket_verification_app/services/api_service.dart' ;
7
5
import 'package:ticket_verification_app/services/provider.dart' ;
8
6
@@ -14,99 +12,76 @@ class QrScannerScreen extends ConsumerStatefulWidget {
14
12
}
15
13
16
14
class _QrScannerScreenState extends ConsumerState <QrScannerScreen > {
17
- final GlobalKey qrKey = GlobalKey (debugLabel: 'QR' );
18
- Barcode ? result;
19
- QRViewController ? controller;
20
-
21
- // In order to get hot reload to work we need to pause the camera if the platform
22
- // is android, or resume the camera if the platform is iOS.
23
- @override
24
- void reassemble () {
25
- super .reassemble ();
26
- if (Platform .isAndroid) {
27
- controller! .pauseCamera ();
28
- } else if (Platform .isIOS) {
29
- controller! .resumeCamera ();
30
- }
31
- }
32
-
15
+ final _mobileScannerController = MobileScannerController ();
33
16
@override
34
17
Widget build (BuildContext context) {
35
18
return Scaffold (
36
- body: QRView (
37
- key: qrKey,
38
- onQRViewCreated: _onQRViewCreated,
39
- ),
40
- );
41
- }
42
-
43
- void _onQRViewCreated (QRViewController controller) {
44
- this .controller = controller;
45
- controller.scannedDataStream.listen ((scanData) async {
46
- controller.pauseCamera ();
47
- result = scanData;
48
- print (result! .code);
49
- if (result! .format != BarcodeFormat .qrcode) {
50
- showDialog (
51
- context: context,
52
- builder: (ctx) => AlertDialog (
53
- title: const Text ('Failure' ),
54
- content: const Text ('The thing scanned is not a qrcode' ),
55
- actions: [
56
- TextButton (
57
- onPressed: () {
58
- Navigator .of (context).pop ();
59
- controller.resumeCamera ();
60
- },
61
- child: const Text ('OK' ),
19
+ appBar: AppBar (title: const Text ('Mobile Scanner' )),
20
+ body: MobileScanner (
21
+ controller: _mobileScannerController,
22
+ onDetect: (capture) async {
23
+ final List <Barcode > barcodes = capture.barcodes;
24
+ if (barcodes.isEmpty) return ;
25
+ final Barcode barcode = barcodes.first;
26
+ _mobileScannerController.stop ();
27
+ if (barcode.format != BarcodeFormat .qrCode || barcode.rawValue == null ) {
28
+ showDialog (
29
+ context: context,
30
+ builder: (ctx) => AlertDialog (
31
+ title: const Text ('Failure' ),
32
+ content: const Text ('The thing scanned is not a qrcode' ),
33
+ actions: [
34
+ TextButton (
35
+ onPressed: () {
36
+ Navigator .of (context).pop ();
37
+ _mobileScannerController.start ();
38
+ },
39
+ child: const Text ('OK' ),
40
+ ),
41
+ ],
62
42
),
63
- ],
64
- ),
65
- );
66
- return ;
67
- }
68
- final apiResult = await ref.read <ApiService >(apiServiceProvider).getQrScanResult (qrCodeContent: result! .code! );
69
- apiResult.fold (
70
- (failure) => showDialog (
71
- context: context,
72
- builder: (ctx) => AlertDialog (
73
- title: const Text ('Failure' ),
74
- content: Text ('The api call failed with message: ${failure .message }' ),
75
- actions: [
76
- TextButton (
77
- onPressed: () {
78
- Navigator .of (context).pop ();
79
- controller.resumeCamera ();
80
- },
81
- child: const Text ('OK' ),
43
+ );
44
+ } else {
45
+ final apiResult =
46
+ await ref.read <ApiService >(apiServiceProvider).getQrScanResult (qrCodeContent: barcode.rawValue! );
47
+ apiResult.fold (
48
+ (failure) => showDialog (
49
+ context: context,
50
+ builder: (ctx) => AlertDialog (
51
+ title: const Text ('Failure' ),
52
+ content: Text ('The api call failed with message: ${failure .message }' ),
53
+ actions: [
54
+ TextButton (
55
+ onPressed: () {
56
+ Navigator .of (context).pop ();
57
+ _mobileScannerController.start ();
58
+ },
59
+ child: const Text ('OK' ),
60
+ ),
61
+ ],
62
+ ),
82
63
),
83
- ],
84
- ) ,
85
- ),
86
- (responseData) => showDialog (
87
- context : context,
88
- builder : (ctx) => AlertDialog (
89
- title : const Text ( 'Success' ),
90
- content : Text (
91
- 'User Name: ${ responseData . name } \n ACM member: ${ responseData . memberStatus ? 'Yes' : 'No' } \n Event Name: ${ responseData . eventName }' ),
92
- actions : [
93
- TextButton (
94
- onPressed : () {
95
- Navigator . of (context). pop ();
96
- controller. resumeCamera ();
97
- } ,
98
- child : const Text ( 'OK' ),
64
+ (responseData) => showDialog (
65
+ context : context ,
66
+ builder : (ctx) => AlertDialog (
67
+ title : const Text ( 'Success' ),
68
+ content : Text (
69
+ 'User Name: ${ responseData . name } \n ACM member: ${ responseData . memberStatus ? 'Yes' : 'No' } \n Event Name: ${ responseData . eventName }' ),
70
+ actions : [
71
+ TextButton (
72
+ onPressed : () {
73
+ Navigator . of (context). pop ();
74
+ _mobileScannerController. start ();
75
+ },
76
+ child : const Text ( 'OK' ),
77
+ ),
78
+ ] ,
79
+ ),
99
80
),
100
- ],
101
- ),
102
- ),
103
- );
104
- });
105
- }
106
-
107
- @override
108
- void dispose () {
109
- controller? .dispose ();
110
- super .dispose ();
81
+ );
82
+ }
83
+ },
84
+ ),
85
+ );
111
86
}
112
87
}
0 commit comments