Skip to content

Commit

Permalink
Change barcode scanner package
Browse files Browse the repository at this point in the history
  • Loading branch information
kilimnik committed Feb 12, 2023
1 parent 356b6dc commit ef6eec4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 53 deletions.
25 changes: 5 additions & 20 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:vepiot/scanner.dart';
import 'package:vepiot/storage.dart';
import 'package:vepiot/unlock.dart';
import 'package:flutter/material.dart';
import 'package:flutter_settings_screens/flutter_settings_screens.dart';
import 'package:barcode_scan2/barcode_scan2.dart';
import 'auth.dart';
import 'config.dart';
import 'fcm.dart';
Expand Down Expand Up @@ -104,25 +104,10 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
}

Future scan() async {
ScanResult result = await BarcodeScanner.scan();
String uri = result.rawContent;

var errorMessage = "";

try {
OTP otp = OTP.createOTP(uri);
setState(() => StorageService.OTPs.value[otp.username] = otp);
await StorageService.writeOTP();
} on FormatException catch (e) {
errorMessage = e.message;
}

if (errorMessage.isNotEmpty) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
}
await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ScanScreen()),
);
}

Future<void> handleClick(String value) async {
Expand Down
107 changes: 107 additions & 0 deletions app/lib/scanner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:vepiot/storage.dart';

import 'otp.dart';

class ScanScreen extends StatefulWidget {
const ScanScreen({super.key});

@override
State<StatefulWidget> createState() => _ScanScreenState();
}

class _ScanScreenStateNotifier extends ChangeNotifier {
void notify() {
StorageService.OTPs.notifyListeners();
}
}

class _ScanScreenState extends State<ScanScreen> {
MobileScannerController cameraController = MobileScannerController();
_ScanScreenStateNotifier notifier = _ScanScreenStateNotifier();

@override
void dispose() {
cameraController.dispose();
notifier.dispose();

super.dispose();
}

Future handleScan(String uri) async {
var errorMessage = "";

try {
OTP otp = OTP.createOTP(uri);
setState(() => StorageService.OTPs.value[otp.username] = otp);
await StorageService.writeOTP();
notifier.notify();
} on FormatException catch (e) {
errorMessage = e.message;
}

if (!mounted) return;
if (errorMessage.isNotEmpty) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
} else {
Navigator.pop(context);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scanner'),
actions: [
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable: cameraController.torchState,
builder: (context, state, child) {
switch (state as TorchState) {
case TorchState.off:
return const Icon(Icons.flash_off, color: Colors.grey);
case TorchState.on:
return const Icon(Icons.flash_on, color: Colors.yellow);
}
},
),
iconSize: 32.0,
onPressed: () => cameraController.toggleTorch(),
),
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable: cameraController.cameraFacingState,
builder: (context, state, child) {
switch (state as CameraFacing) {
case CameraFacing.front:
return const Icon(Icons.camera_front);
case CameraFacing.back:
return const Icon(Icons.camera_rear);
}
},
),
iconSize: 32.0,
onPressed: () => cameraController.switchCamera(),
),
],
),
body: MobileScanner(
controller: cameraController,
onDetect: (capture) {
final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
handleScan(barcode.rawValue!);
}
},
),
);
}
}
64 changes: 32 additions & 32 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ packages:
dependency: transitive
description:
name: _flutterfire_internals
sha256: "3ff770dfff04a67b0863dff205a0936784de1b87a5e99b11c693fc10e66a9ce3"
sha256: "6215ac7d00ed98300b72f45ed2b38c2ca841f9f4e6965fab33cbd591e45e4473"
url: "https://pub.dev"
source: hosted
version: "1.0.12"
version: "1.0.13"
analyzer:
dependency: transitive
description:
Expand All @@ -29,10 +29,10 @@ packages:
dependency: transitive
description:
name: args
sha256: "139d809800a412ebb26a3892da228b2d0ba36f0ef5d9a82166e5e52ec8d61611"
sha256: "4cab82a83ffef80b262ddedf47a0a8e56ee6fbf7fe21e6e768b02792034dd440"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
version: "2.4.0"
async:
dependency: transitive
description:
Expand All @@ -41,14 +41,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.10.0"
barcode_scan2:
dependency: "direct main"
description:
name: barcode_scan2
sha256: f9af9252b8f3f5fa446f5456fd45f8871d09f883d8389a1d608b39231bfbc3fa
url: "https://pub.dev"
source: hosted
version: "4.2.3"
base32:
dependency: transitive
description:
Expand Down Expand Up @@ -245,50 +237,50 @@ packages:
dependency: "direct main"
description:
name: firebase_core
sha256: c129209ba55f3d4272c89fb4a4994c15bea77fb6de63a82d45fb6bc5c94e4355
sha256: be13e431c0c950f0fc66bdb67b41b8059121d7e7d8bbbc21fb59164892d561f8
url: "https://pub.dev"
source: hosted
version: "2.4.1"
version: "2.5.0"
firebase_core_platform_interface:
dependency: transitive
description:
name: firebase_core_platform_interface
sha256: "5fab93f5b354648efa62e7cc829c90efb68c8796eecf87e0888cae2d5f3accd4"
sha256: "5615b30c36f55b2777d0533771deda7e5730e769e5d3cb7fda79e9bed86cfa55"
url: "https://pub.dev"
source: hosted
version: "4.5.2"
version: "4.5.3"
firebase_core_web:
dependency: transitive
description:
name: firebase_core_web
sha256: "18b35ce111b0a4266abf723c825bcf9d4e2519d13638cc7f06f2a8dd960c75bc"
sha256: "4b3a41410f3313bb95fd560aa5eb761b6ad65c185de772c72231e8b4aeed6d18"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.1"
firebase_messaging:
dependency: "direct main"
description:
name: firebase_messaging
sha256: dc010a6436333029fba858415fe65887c3fe44d8f6e6ea162bb8d3dd764fbcb6
sha256: dbccddc62fef6f3745ba83062bfd1fbf2eb6a931db4c73d03f85c5772dfdec7f
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.2"
firebase_messaging_platform_interface:
dependency: transitive
description:
name: firebase_messaging_platform_interface
sha256: abda2d766486096eb1c568c7b20aef46180596c8b0708190b929133ff03e0a8d
sha256: "564a47ea76db9cd2d17e7d95790428ad3de9d0075795d14c4c901ba0bf518e1a"
url: "https://pub.dev"
source: hosted
version: "4.2.10"
version: "4.2.11"
firebase_messaging_web:
dependency: transitive
description:
name: firebase_messaging_web
sha256: "7a0ce957bd2210e8636325152234728874dad039f1c7271ba1be5c752fdc5888"
sha256: "76291494583a003d4ce0d613c41cb87c58fab25773317daa66a245f537e1c3f7"
url: "https://pub.dev"
source: hosted
version: "3.2.11"
version: "3.2.12"
fixnum:
dependency: transitive
description:
Expand Down Expand Up @@ -354,10 +346,10 @@ packages:
dependency: transitive
description:
name: flutter_secure_storage_linux
sha256: "736436adaf91552433823f51ce22e098c2f0551db06b6596f58597a25b8ea797"
sha256: "0912ae29a572230ad52d8a4697e5518d7f0f429052fd51df7e5a7952c7efe2a3"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
version: "1.1.3"
flutter_secure_storage_macos:
dependency: transitive
description:
Expand Down Expand Up @@ -516,10 +508,10 @@ packages:
dependency: transitive
description:
name: local_auth_android
sha256: ba48fe0e1cae140a0813ce68c2540250d7f573a8ae4d4b6c681b2d2583584953
sha256: cfcbc4936e288d61ef85a04feef6b95f49ba496d4fd98364e6abafb462b06a1f
url: "https://pub.dev"
source: hosted
version: "1.0.17"
version: "1.0.18"
local_auth_ios:
dependency: transitive
description:
Expand Down Expand Up @@ -584,6 +576,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.4"
mobile_scanner:
dependency: "direct main"
description:
name: mobile_scanner
sha256: "4045e8441e21f1fb8998a76fbffd054510dd3a3b1dee55c7c9a2083eee687345"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -644,10 +644,10 @@ packages:
dependency: transitive
description:
name: path_provider_linux
sha256: ab0987bf95bc591da42dffb38c77398fc43309f0b9b894dcc5d6f40c4b26c379
sha256: "2e32f1640f07caef0d3cb993680f181c79e54a3827b997d5ee221490d131fbd9"
url: "https://pub.dev"
source: hosted
version: "2.1.7"
version: "2.1.8"
path_provider_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -748,10 +748,10 @@ packages:
dependency: "direct main"
description:
name: share_plus
sha256: e387077716f80609bb979cd199331033326033ecd1c8f200a90c5f57b1c9f55e
sha256: "8c6892037b1824e2d7e8f59d54b3105932899008642e6372e5079c6939b4b625"
url: "https://pub.dev"
source: hosted
version: "6.3.0"
version: "6.3.1"
share_plus_platform_interface:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ dependencies:
firebase_messaging: ^14.2.1
http: ^0.13.5
share_plus: ^6.3.0
barcode_scan2: ^4.2.3
flutter_local_notifications: ^13.0.0
local_auth: ^2.1.3
mobile_scanner: ^3.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit ef6eec4

Please sign in to comment.