-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!); | ||
} | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters