|
| 1 | +import 'package:cloud_firestore/cloud_firestore.dart'; |
| 2 | +import 'package:conferenceapp/common/logger.dart'; |
| 3 | +import 'package:fast_qr_reader_view/fast_qr_reader_view.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:vibration/vibration.dart'; |
| 6 | + |
| 7 | +class ScanPartyPage extends StatefulWidget { |
| 8 | + final List<QrCameraDescription> cameras; |
| 9 | + |
| 10 | + const ScanPartyPage({Key key, this.cameras}) : super(key: key); |
| 11 | + @override |
| 12 | + _ScanPartyPageState createState() => _ScanPartyPageState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _ScanPartyPageState extends State<ScanPartyPage> { |
| 16 | + QRReaderController controller; |
| 17 | + bool scanning = true; |
| 18 | + final collection = Firestore.instance.collection('party'); |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + onNewCameraSelected(widget.cameras[0]); |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return Scaffold( |
| 29 | + appBar: AppBar( |
| 30 | + title: Text('Ticket scanning at the party'), |
| 31 | + ), |
| 32 | + body: SafeArea( |
| 33 | + child: Stack( |
| 34 | + children: <Widget>[ |
| 35 | + StreamBuilder<QuerySnapshot>( |
| 36 | + stream: collection.snapshots(), |
| 37 | + builder: (context, snapshot) { |
| 38 | + if (snapshot.hasData) { |
| 39 | + final List array = snapshot.data.documents; |
| 40 | + final length = array.length; |
| 41 | + return Positioned( |
| 42 | + top: 0, |
| 43 | + left: 0, |
| 44 | + right: 0, |
| 45 | + child: Container( |
| 46 | + color: Colors.black45, |
| 47 | + child: Text( |
| 48 | + '$length', |
| 49 | + style: TextStyle( |
| 50 | + fontSize: 30, |
| 51 | + color: Colors.white, |
| 52 | + ), |
| 53 | + textAlign: TextAlign.center, |
| 54 | + ), |
| 55 | + ), |
| 56 | + ); |
| 57 | + } |
| 58 | + return Container(); |
| 59 | + }, |
| 60 | + ), |
| 61 | + Center( |
| 62 | + child: _cameraPreviewWidget(), |
| 63 | + ), |
| 64 | + if (!scanning) |
| 65 | + Positioned( |
| 66 | + bottom: 0, |
| 67 | + left: 0, |
| 68 | + right: 0, |
| 69 | + child: Padding( |
| 70 | + padding: const EdgeInsets.all(8.0), |
| 71 | + child: RaisedButton( |
| 72 | + onPressed: startScanning, |
| 73 | + child: Text('Scan again'), |
| 74 | + ), |
| 75 | + ), |
| 76 | + ) |
| 77 | + ], |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + Widget _cameraPreviewWidget() { |
| 84 | + if (controller == null || !controller.value.isInitialized) { |
| 85 | + return const Text( |
| 86 | + 'No camera selected', |
| 87 | + style: const TextStyle( |
| 88 | + color: Colors.white, |
| 89 | + fontSize: 24.0, |
| 90 | + fontWeight: FontWeight.w900, |
| 91 | + ), |
| 92 | + ); |
| 93 | + } else { |
| 94 | + return new AspectRatio( |
| 95 | + aspectRatio: controller.value.aspectRatio, |
| 96 | + child: new QRReaderPreview(controller), |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + void onNewCameraSelected(QrCameraDescription cameraDescription) async { |
| 102 | + if (controller != null) { |
| 103 | + await controller.dispose(); |
| 104 | + } |
| 105 | + controller = new QRReaderController( |
| 106 | + cameraDescription, |
| 107 | + ResolutionPreset.low, |
| 108 | + [ |
| 109 | + CodeFormat.qr, |
| 110 | + CodeFormat.pdf417, |
| 111 | + ], |
| 112 | + onCodeRead, |
| 113 | + ); |
| 114 | + |
| 115 | + // If the controller is updated then update the UI. |
| 116 | + controller.addListener(() { |
| 117 | + if (mounted) setState(() {}); |
| 118 | + if (controller.value.hasError) { |
| 119 | + showInSnackBar('Camera error ${controller.value.errorDescription}'); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + try { |
| 124 | + await controller.initialize(); |
| 125 | + } on QRReaderException catch (e) { |
| 126 | + logger.errorException(e.description); |
| 127 | + showInSnackBar('Error: ${e.code}\n${e.description}'); |
| 128 | + } |
| 129 | + |
| 130 | + if (mounted) { |
| 131 | + setState(() {}); |
| 132 | + controller.startScanning(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + void onCodeRead(dynamic value) { |
| 137 | + logger.info("Stopping scanning, value detected: $value"); |
| 138 | + Vibration.vibrate(duration: 300); |
| 139 | + |
| 140 | + // setState(() { |
| 141 | + // scanning = false; |
| 142 | + // }); |
| 143 | + try { |
| 144 | + final values = value.toString().split(' '); |
| 145 | + final id = values[0]; |
| 146 | + collection.document('$id').setData( |
| 147 | + { |
| 148 | + 'updated': Timestamp.now(), |
| 149 | + 'orderId': values[1], |
| 150 | + 'ticketId': values[2], |
| 151 | + }, |
| 152 | + ); |
| 153 | + // controller.stopScanning(); |
| 154 | + } catch (e, s) { |
| 155 | + logger.errorException(e, s); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + void startScanning() { |
| 160 | + logger.info("Starting scanning"); |
| 161 | + try { |
| 162 | + if (scanning == false) controller.stopScanning(); |
| 163 | + } catch (e) { |
| 164 | + logger.errorException(e); |
| 165 | + } |
| 166 | + try { |
| 167 | + controller.startScanning(); |
| 168 | + setState(() { |
| 169 | + scanning = true; |
| 170 | + }); |
| 171 | + } catch (e) { |
| 172 | + logger.errorException(e); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + void showInSnackBar(String message) { |
| 177 | + Scaffold.of(context).showSnackBar(new SnackBar( |
| 178 | + content: new Text(message), |
| 179 | + behavior: SnackBarBehavior.floating, |
| 180 | + )); |
| 181 | + } |
| 182 | +} |
0 commit comments