Skip to content
This repository was archived by the owner on Jul 22, 2021. It is now read-only.

Commit baca07f

Browse files
committed
Very crude implementation of tickets check
1 parent 40d9470 commit baca07f

File tree

2 files changed

+202
-16
lines changed

2 files changed

+202
-16
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

lib/ticket_check/ticket_check_page.dart

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import 'dart:convert';
2-
3-
import 'package:cloud_firestore/cloud_firestore.dart';
41
import 'package:conferenceapp/common/logger.dart';
5-
import 'package:conferenceapp/profile/auth_repository.dart';
62
import 'package:conferenceapp/ticket_check/bloc/bloc.dart';
7-
import 'package:conferenceapp/ticket_check/tickets_list.dart';
83
import 'package:conferenceapp/ticket_check/users_list.dart';
94
import 'package:flutter/material.dart';
105
import 'package:fast_qr_reader_view/fast_qr_reader_view.dart';
11-
import 'package:flutter_bloc/flutter_bloc.dart';
12-
import 'package:intl/intl.dart';
13-
import 'package:line_icons/line_icons.dart';
146

7+
import 'scan_party_page.dart';
158
import 'scan_ticket_page.dart';
169

1710
class TicketCheckPage extends StatefulWidget {
@@ -93,21 +86,32 @@ class _TicketCheckPageState extends State<TicketCheckPage> {
9386
}
9487
},
9588
),
96-
RaisedButton(
97-
child: Padding(
98-
padding: const EdgeInsets.all(8.0),
99-
child: Text(
100-
'Wydawanie koszulek',
101-
),
102-
),
103-
),
10489
RaisedButton(
10590
child: Padding(
10691
padding: const EdgeInsets.all(8.0),
10792
child: Text(
10893
'Kontrola na imprezie',
10994
),
11095
),
96+
onPressed: () async {
97+
try {
98+
final cameras = await availableCameras();
99+
final permission = await checkCameraPermission();
100+
if (permission != PermissionStatus.granted) {
101+
await requestCameraPermission();
102+
}
103+
await Navigator.push(
104+
context,
105+
MaterialPageRoute(
106+
builder: (context) => ScanPartyPage(
107+
cameras: cameras,
108+
),
109+
),
110+
);
111+
} on QRReaderException catch (e) {
112+
logError(e.code, e.description);
113+
}
114+
},
111115
),
112116
RaisedButton(
113117
child: Text('Przeglądaj sprawdzone'),

0 commit comments

Comments
 (0)