yubioath-flutter/lib/android/qr_scanner/qr_scanner_view.dart

231 lines
7.3 KiB
Dart
Raw Normal View History

2022-03-28 11:58:09 +03:00
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import '../../app/navigation_service.dart';
2022-03-29 17:01:27 +03:00
import '../../oath/models.dart';
2022-03-28 11:58:09 +03:00
2022-03-31 17:31:03 +03:00
/// Status of view state
enum _ScanStatus { looking, error, success }
2022-03-29 17:52:51 +03:00
class OverlayClipper extends CustomClipper<Path> {
2022-03-31 17:31:03 +03:00
/// helper method to calculate position of the rect
Rect _getOverlayRect(Size size, double width) => Rect.fromCenter(
center: Offset(size.width / 2, size.height / 2),
width: width,
height: width);
2022-03-29 17:52:51 +03:00
@override
Path getClip(Size size) {
const r = 40.0;
var w = size.width - 40;
return Path()
..addRect(Rect.fromLTWH(0, 0, size.width, size.height))
2022-03-31 17:31:03 +03:00
..addRRect(RRect.fromRectXY(_getOverlayRect(size, w), r, r))
2022-03-29 17:52:51 +03:00
..fillType = PathFillType.evenOdd;
}
@override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
}
class MobileScannerWrapper extends StatelessWidget {
2022-03-29 17:01:27 +03:00
final MobileScannerController controller;
final Function(Barcode barcode, MobileScannerArguments? args)? onDetect;
2022-03-31 17:31:03 +03:00
final _ScanStatus status;
2022-03-29 17:52:51 +03:00
2022-03-29 17:01:27 +03:00
const MobileScannerWrapper({
Key? key,
required this.controller,
2022-03-29 17:52:51 +03:00
required this.onDetect,
2022-03-31 17:31:03 +03:00
required this.status,
2022-03-29 17:01:27 +03:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-03-31 17:31:03 +03:00
var backgroundColor = status == _ScanStatus.looking
? Colors.white
: status == _ScanStatus.error
? Colors.red.shade900
: Colors.green.shade900;
var size = MediaQuery.of(context).size;
var positionRect = Rect.fromCenter(
center: Offset(size.width / 2, size.height / 2 - 51),
width: size.width - 38,
height: size.width - 38);
2022-03-29 17:01:27 +03:00
return Stack(children: [
2022-03-29 17:52:51 +03:00
MobileScanner(
controller: controller,
2022-03-31 17:31:03 +03:00
allowDuplicates: true,
2022-03-29 17:52:51 +03:00
onDetect: (barcode, args) {
onDetect?.call(barcode, args);
}),
ClipPath(
clipper: OverlayClipper(),
child: Opacity(
2022-03-31 17:31:03 +03:00
opacity: 0.3,
2022-03-29 17:52:51 +03:00
child: ColoredBox(
2022-03-31 17:31:03 +03:00
color: backgroundColor,
2022-03-29 17:52:51 +03:00
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: const [Spacer()],
)))),
2022-03-31 17:31:03 +03:00
if (status == _ScanStatus.success)
Positioned.fromRect(
rect: positionRect,
child: Icon(
Icons.check_circle,
size: 200,
color: Colors.green.shade400,
)),
if (status == _ScanStatus.error)
Positioned.fromRect(
rect: positionRect,
child: Icon(
Icons.error,
size: 200,
color: Colors.red.shade400,
)),
2022-03-29 17:01:27 +03:00
]);
}
}
class QrScannerView extends StatefulWidget {
2022-03-28 11:58:09 +03:00
const QrScannerView({Key? key}) : super(key: key);
2022-03-29 17:01:27 +03:00
@override
_QrScannerViewState createState() => _QrScannerViewState();
}
class _QrScannerViewState extends State<QrScannerView> {
String? _scannedString;
2022-03-31 17:31:03 +03:00
// will be used later
// ignore: unused_field
2022-03-29 17:01:27 +03:00
CredentialData? _credentialData;
2022-03-31 17:31:03 +03:00
_ScanStatus _status = _ScanStatus.looking;
2022-03-29 17:01:27 +03:00
final MobileScannerController _controller =
MobileScannerController(facing: CameraFacing.back, torchEnabled: false);
2022-03-31 17:31:03 +03:00
void setError() {
_credentialData = null;
_scannedString = null;
_status = _ScanStatus.error;
Future.delayed(const Duration(milliseconds: 2000), () {
resetError();
});
}
void resetError() {
setState(() {
_credentialData = null;
_scannedString = null;
_status = _ScanStatus.looking;
});
}
void handleResult(String? code, MobileScannerArguments? args) {
if (_status != _ScanStatus.looking) {
// on success and error ignore reported codes
return;
}
2022-03-29 17:01:27 +03:00
setState(() {
if (code != null) {
try {
var parsedCredential = CredentialData.fromUri(Uri.parse(code));
_credentialData = parsedCredential;
_scannedString = code;
2022-03-31 17:31:03 +03:00
_status = _ScanStatus.success;
Future.delayed(const Duration(milliseconds: 800), () {
BuildContext dialogContext =
NavigationService.navigatorKey.currentContext!;
if (Navigator.of(dialogContext).canPop()) {
// prevent several callbacks
Navigator.of(dialogContext).pop(_scannedString);
}
});
2022-03-29 17:01:27 +03:00
} on ArgumentError catch (_) {
2022-03-31 17:31:03 +03:00
setError();
} catch (e) {
setError();
2022-03-29 17:01:27 +03:00
}
} else {
2022-03-31 17:31:03 +03:00
setError();
2022-03-29 17:01:27 +03:00
}
});
}
2022-03-28 11:58:09 +03:00
@override
Widget build(BuildContext context) {
2022-03-29 17:01:27 +03:00
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text('Scan QR code'),
leading: BackButton(
onPressed: () {
Navigator.of(context).pop();
},
),
),
2022-03-29 17:52:51 +03:00
body: Stack(children: [
MobileScannerWrapper(
controller: _controller,
2022-03-31 17:31:03 +03:00
status: _status,
onDetect: (barcode, args) =>
handleResult(barcode.rawValue, args),
2022-03-29 17:52:51 +03:00
),
Padding(
padding:
const EdgeInsets.symmetric(vertical: 32, horizontal: 32),
child: Column(
2022-03-31 17:31:03 +03:00
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2022-03-29 17:52:51 +03:00
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
2022-03-31 17:31:03 +03:00
Column(children: [
const SizedBox(
height: 32,
),
if (_status == _ScanStatus.looking)
Text('Looking for a code...',
style: Theme.of(context)
.textTheme
.headline6
?.copyWith(color: Colors.black)),
if (_status == _ScanStatus.success)
Text('Found a valid code',
style: Theme.of(context)
.textTheme
.headline6
?.copyWith(color: Colors.white)),
if (_status == _ScanStatus.error)
Text('This code is not valid, try again.',
style: Theme.of(context)
.textTheme
.headline6
?.copyWith(color: Colors.white)),
]),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: Colors.white38,
onPressed: () {
2022-04-01 10:43:54 +03:00
Navigator.of(context).pop('');
2022-03-31 17:31:03 +03:00
},
child: const Text('Add manually'),
)
],
)
2022-03-29 17:52:51 +03:00
],
)),
])));
2022-03-28 11:58:09 +03:00
}
}