2022-03-28 11:58:09 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-04-22 16:28:08 +03:00
|
|
|
import 'package:qrscanner_zxing/qrscanner_zxing_view.dart';
|
2022-03-28 11:58:09 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:34:51 +03:00
|
|
|
class _MobileScannerWrapper extends StatelessWidget {
|
2022-04-26 16:07:36 +03:00
|
|
|
final Function(String) onDetect;
|
2022-03-31 17:31:03 +03:00
|
|
|
final _ScanStatus status;
|
2022-03-29 17:52:51 +03:00
|
|
|
|
2022-05-12 09:34:51 +03:00
|
|
|
const _MobileScannerWrapper({
|
2022-03-29 17:52:51 +03:00
|
|
|
required this.onDetect,
|
2022-03-31 17:31:03 +03:00
|
|
|
required this.status,
|
2022-05-12 10:56:55 +03:00
|
|
|
});
|
2022-03-29 17:01:27 +03:00
|
|
|
|
|
|
|
@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-04-26 16:07:36 +03:00
|
|
|
QRScannerZxingView(
|
|
|
|
marginPct: 20,
|
|
|
|
onDetect: (barCode) {
|
|
|
|
onDetect.call(barCode);
|
|
|
|
}),
|
2022-03-29 17:52:51 +03:00
|
|
|
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-05-12 10:56:55 +03:00
|
|
|
const QrScannerView({super.key});
|
2022-03-28 11:58:09 +03:00
|
|
|
|
2022-03-29 17:01:27 +03:00
|
|
|
@override
|
2022-05-12 09:34:51 +03:00
|
|
|
State<QrScannerView> createState() => _QrScannerViewState();
|
2022-03-29 17:01:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-26 16:07:36 +03:00
|
|
|
void handleResult(String barCode) {
|
2022-03-31 17:31:03 +03:00
|
|
|
if (_status != _ScanStatus.looking) {
|
|
|
|
// on success and error ignore reported codes
|
|
|
|
return;
|
|
|
|
}
|
2022-03-29 17:01:27 +03:00
|
|
|
setState(() {
|
2022-04-26 16:07:36 +03:00
|
|
|
if (barCode.isNotEmpty) {
|
2022-03-29 17:01:27 +03:00
|
|
|
try {
|
2022-04-26 16:07:36 +03:00
|
|
|
var parsedCredential = CredentialData.fromUri(Uri.parse(barCode));
|
2022-03-29 17:01:27 +03:00
|
|
|
_credentialData = parsedCredential;
|
2022-04-26 16:07:36 +03:00
|
|
|
_scannedString = barCode;
|
2022-03-31 17:31:03 +03:00
|
|
|
_status = _ScanStatus.success;
|
|
|
|
|
2022-04-28 16:24:03 +03:00
|
|
|
final navigator = Navigator.of(context);
|
2022-03-31 17:31:03 +03:00
|
|
|
Future.delayed(const Duration(milliseconds: 800), () {
|
2022-04-28 16:24:03 +03:00
|
|
|
if (navigator.canPop()) {
|
2022-03-31 17:31:03 +03:00
|
|
|
// prevent several callbacks
|
2022-04-28 16:24:03 +03:00
|
|
|
navigator.pop(_scannedString);
|
2022-03-31 17:31:03 +03:00
|
|
|
}
|
|
|
|
});
|
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: [
|
2022-05-12 09:34:51 +03:00
|
|
|
_MobileScannerWrapper(
|
2022-03-31 17:31:03 +03:00
|
|
|
status: _status,
|
2022-04-22 12:22:59 +03:00
|
|
|
onDetect: (scannedData) => handleResult(scannedData),
|
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
|
|
|
}
|
|
|
|
}
|