Skip to content

Commit 5dae508

Browse files
committed
Added web support for the app
1 parent 3caf81a commit 5dae508

File tree

5 files changed

+78
-100
lines changed

5 files changed

+78
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import 'dart:io';
2-
31
import 'package:flutter/material.dart';
42
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';
64
import 'package:ticket_verification_app/services/api_service.dart';
75
import 'package:ticket_verification_app/services/provider.dart';
86

@@ -14,99 +12,76 @@ class QrScannerScreen extends ConsumerStatefulWidget {
1412
}
1513

1614
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();
3316
@override
3417
Widget build(BuildContext context) {
3518
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+
],
6242
),
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+
),
8263
),
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}\nACM 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}\nACM 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+
),
9980
),
100-
],
101-
),
102-
),
103-
);
104-
});
105-
}
106-
107-
@override
108-
void dispose() {
109-
controller?.dispose();
110-
super.dispose();
81+
);
82+
}
83+
},
84+
),
85+
);
11186
}
11287
}

lib/services/api_service.dart

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ApiService {
2424
}
2525
return right(ResponseData.fromJson(response.data as Map<String, dynamic>));
2626
} catch (e) {
27+
// rethrow;
2728
return left(Failure(message: 'Exception caught: $e'));
2829
}
2930
}

macos/Flutter/GeneratedPluginRegistrant.swift

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import mobile_scanner
89

910
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
11+
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
1012
}

pubspec.lock

+8-8
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ packages:
400400
url: "https://pub.dev"
401401
source: hosted
402402
version: "1.0.4"
403+
mobile_scanner:
404+
dependency: "direct main"
405+
description:
406+
name: mobile_scanner
407+
sha256: bcc0926b5e7f0c62c4e5a56bc66d93af619373f19e1117e553d49af3c9293b3a
408+
url: "https://pub.dev"
409+
source: hosted
410+
version: "3.1.1"
403411
package_config:
404412
dependency: transitive
405413
description:
@@ -448,14 +456,6 @@ packages:
448456
url: "https://pub.dev"
449457
source: hosted
450458
version: "1.2.1"
451-
qr_code_scanner:
452-
dependency: "direct main"
453-
description:
454-
name: qr_code_scanner
455-
sha256: f23b68d893505a424f0bd2e324ebea71ed88465d572d26bb8d2e78a4749591fd
456-
url: "https://pub.dev"
457-
source: hosted
458-
version: "1.0.1"
459459
riverpod:
460460
dependency: transitive
461461
description:

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ dependencies:
3535
# The following adds the Cupertino Icons font to your application.
3636
# Use with the CupertinoIcons class for iOS style icons.
3737
cupertino_icons: ^1.0.2
38-
qr_code_scanner: ^1.0.1
3938
dio: ^5.0.1
4039
auto_route: ^5.0.4
4140
flutter_riverpod: ^2.2.0
4241
dartz: ^0.10.1
4342
freezed_annotation: ^2.2.0
4443
json_annotation: ^4.8.0
44+
mobile_scanner: ^3.1.1
4545

4646
dev_dependencies:
4747
flutter_test:

0 commit comments

Comments
 (0)