veilidchat/lib/contact_invitation/views/scan_invite_dialog.dart

406 lines
14 KiB
Dart
Raw Normal View History

2023-09-23 12:56:54 -04:00
import 'dart:async';
2023-09-28 10:06:22 -04:00
import 'dart:io';
2023-09-27 13:34:19 -04:00
import 'dart:typed_data';
2023-09-23 12:56:54 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-09-30 21:22:12 -04:00
import 'package:flutter/foundation.dart';
2023-09-23 12:56:54 -04:00
import 'package:flutter/material.dart';
2023-09-28 10:06:22 -04:00
import 'package:flutter/scheduler.dart';
2023-09-23 12:56:54 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2023-09-29 22:45:50 -04:00
import 'package:image/image.dart' as img;
import 'package:mobile_scanner/mobile_scanner.dart';
2023-09-28 10:06:22 -04:00
import 'package:pasteboard/pasteboard.dart';
import 'package:zxing2/qrcode.dart';
2023-09-23 12:56:54 -04:00
2024-01-09 20:58:27 -05:00
import '../../theme/theme.dart';
import '../../tools/tools.dart';
2023-09-27 13:34:19 -04:00
import 'invite_dialog.dart';
2023-09-23 12:56:54 -04:00
2023-09-28 10:06:22 -04:00
class BarcodeOverlay extends CustomPainter {
BarcodeOverlay({
required this.barcode,
required this.arguments,
required this.boxFit,
required this.capture,
});
final BarcodeCapture capture;
final Barcode barcode;
final MobileScannerArguments arguments;
final BoxFit boxFit;
@override
void paint(Canvas canvas, Size size) {
final adjustedSize = applyBoxFit(boxFit, arguments.size, size);
var verticalPadding = size.height - adjustedSize.destination.height;
var horizontalPadding = size.width - adjustedSize.destination.width;
if (verticalPadding > 0) {
verticalPadding = verticalPadding / 2;
} else {
verticalPadding = 0;
}
if (horizontalPadding > 0) {
horizontalPadding = horizontalPadding / 2;
} else {
horizontalPadding = 0;
}
2024-01-09 20:58:27 -05:00
final ratioWidth = (Platform.isIOS ? capture.width : arguments.size.width) /
adjustedSize.destination.width;
2023-09-28 10:06:22 -04:00
final ratioHeight =
2024-01-09 20:58:27 -05:00
(Platform.isIOS ? capture.height : arguments.size.height) /
2023-09-28 10:06:22 -04:00
adjustedSize.destination.height;
final adjustedOffset = <Offset>[];
2024-01-09 20:58:27 -05:00
for (final offset in barcode.corners) {
2023-09-28 10:06:22 -04:00
adjustedOffset.add(
Offset(
offset.dx / ratioWidth + horizontalPadding,
offset.dy / ratioHeight + verticalPadding,
),
);
}
final cutoutPath = Path()..addPolygon(adjustedOffset, true);
final backgroundPaint = Paint()
..color = Colors.red.withOpacity(0.3)
..style = PaintingStyle.fill
..blendMode = BlendMode.dstOut;
canvas.drawPath(cutoutPath, backgroundPaint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
class ScannerOverlay extends CustomPainter {
ScannerOverlay(this.scanWindow);
final Rect scanWindow;
@override
void paint(Canvas canvas, Size size) {
final backgroundPath = Path()..addRect(Rect.largest);
final cutoutPath = Path()..addRect(scanWindow);
final backgroundPaint = Paint()
..color = Colors.black.withOpacity(0.5)
..style = PaintingStyle.fill
..blendMode = BlendMode.dstOut;
final backgroundWithCutout = Path.combine(
PathOperation.difference,
backgroundPath,
cutoutPath,
);
canvas.drawPath(backgroundWithCutout, backgroundPaint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
2024-01-09 20:58:27 -05:00
class ScanInviteDialog extends StatefulWidget {
2024-02-14 21:33:15 -05:00
const ScanInviteDialog({required this.modalContext, super.key});
2023-09-23 12:56:54 -04:00
@override
ScanInviteDialogState createState() => ScanInviteDialogState();
2023-09-27 13:34:19 -04:00
static Future<void> show(BuildContext context) async {
await showStyledDialog<void>(
context: context,
title: translate('scan_invite_dialog.title'),
2024-02-14 21:33:15 -05:00
child: ScanInviteDialog(modalContext: context));
}
final BuildContext modalContext;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
.add(DiagnosticsProperty<BuildContext>('modalContext', modalContext));
2023-09-27 13:34:19 -04:00
}
2023-09-23 12:56:54 -04:00
}
2024-01-09 20:58:27 -05:00
class ScanInviteDialogState extends State<ScanInviteDialog> {
2023-09-28 10:06:22 -04:00
bool scanned = false;
2023-09-23 12:56:54 -04:00
@override
void initState() {
super.initState();
}
2023-09-27 13:34:19 -04:00
void onValidationCancelled() {
2023-09-28 10:06:22 -04:00
setState(() {
scanned = false;
});
2023-09-23 12:56:54 -04:00
}
2023-09-28 10:06:22 -04:00
void onValidationSuccess() {}
2023-09-27 13:34:19 -04:00
void onValidationFailed() {
2023-09-28 10:06:22 -04:00
setState(() {
scanned = false;
});
2023-09-23 12:56:54 -04:00
}
2023-09-28 10:06:22 -04:00
2023-09-27 13:34:19 -04:00
bool inviteControlIsValid() => false; // _pasteTextController.text.isNotEmpty;
2023-09-23 12:56:54 -04:00
2023-09-28 10:06:22 -04:00
Future<Uint8List?> scanQRImage(BuildContext context) async {
final theme = Theme.of(context);
//final textTheme = theme.textTheme;
final scale = theme.extension<ScaleScheme>()!;
final windowSize = MediaQuery.of(context).size;
//final maxDialogWidth = min(windowSize.width - 64.0, 800.0 - 64.0);
//final maxDialogHeight = windowSize.height - 64.0;
final scanWindow = Rect.fromCenter(
center: MediaQuery.of(context).size.center(Offset.zero),
width: 200,
height: 200,
);
final cameraController = MobileScannerController();
try {
return showDialog(
context: context,
builder: (context) => Stack(
fit: StackFit.expand,
children: [
MobileScanner(
fit: BoxFit.contain,
scanWindow: scanWindow,
controller: cameraController,
errorBuilder: (context, error, child) =>
ScannerErrorWidget(error: error),
onDetect: (c) {
final barcode = c.barcodes.firstOrNull;
final barcodeBytes = barcode?.rawBytes;
if (barcodeBytes != null) {
cameraController.dispose();
Navigator.pop(context, barcodeBytes);
}
}),
CustomPaint(
painter: ScannerOverlay(scanWindow),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
alignment: Alignment.bottomCenter,
height: 100,
color: Colors.black.withOpacity(0.4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable: cameraController.torchState,
builder: (context, state, child) {
switch (state) {
case TorchState.off:
return Icon(Icons.flash_off,
color:
scale.grayScale.subtleBackground);
case TorchState.on:
return Icon(Icons.flash_on,
color: scale.primaryScale.background);
}
},
),
iconSize: 32,
onPressed: cameraController.toggleTorch,
),
SizedBox(
width: windowSize.width - 120,
height: 50,
child: FittedBox(
child: Text(
translate('scan_invite_dialog.instructions'),
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.labelLarge!
.copyWith(color: Colors.white),
),
),
),
IconButton(
color: Colors.white,
icon: ValueListenableBuilder(
valueListenable:
cameraController.cameraFacingState,
builder: (context, state, child) {
switch (state) {
case CameraFacing.front:
return const Icon(Icons.camera_front);
case CameraFacing.back:
return const Icon(Icons.camera_rear);
}
},
),
iconSize: 32,
onPressed: cameraController.switchCamera,
),
],
),
),
),
Align(
alignment: Alignment.topRight,
child: IconButton(
color: Colors.white,
icon: Icon(Icons.close,
color: scale.grayScale.background),
iconSize: 32,
onPressed: () => {
SchedulerBinding.instance
.addPostFrameCallback((_) {
cameraController.dispose();
Navigator.pop(context, null);
})
})),
],
));
} on MobileScannerException catch (e) {
if (e.errorCode == MobileScannerErrorCode.permissionDenied) {
showErrorToast(
context, translate('scan_invite_dialog.permission_error'));
} else {
showErrorToast(context, translate('scan_invite_dialog.error'));
}
} on Exception catch (_) {
showErrorToast(context, translate('scan_invite_dialog.error'));
}
return null;
}
Future<Uint8List?> pasteQRImage(BuildContext context) async {
final imageBytes = await Pasteboard.image;
if (imageBytes == null) {
if (context.mounted) {
showErrorToast(context, translate('scan_invite_dialog.not_an_image'));
}
return null;
}
final image = img.decodeImage(imageBytes);
if (image == null) {
if (context.mounted) {
showErrorToast(
context, translate('scan_invite_dialog.could_not_decode_image'));
}
return null;
}
try {
final source = RGBLuminanceSource(
image.width,
image.height,
image
.convert(numChannels: 4)
.getBytes(order: img.ChannelOrder.abgr)
.buffer
.asInt32List());
final bitmap = BinaryBitmap(HybridBinarizer(source));
final reader = QRCodeReader();
final result = reader.decode(bitmap);
final segs = result.resultMetadata[ResultMetadataType.byteSegments]!
as List<Int8List>;
return Uint8List.fromList(segs[0].toList());
} on Exception catch (_) {
if (context.mounted) {
showErrorToast(
context, translate('scan_invite_dialog.not_a_valid_qr_code'));
}
return null;
}
}
2023-09-27 13:34:19 -04:00
Widget buildInviteControl(
BuildContext context,
InviteDialogState dialogState,
Future<void> Function({required Uint8List inviteData})
validateInviteData) {
2023-09-30 21:22:12 -04:00
//final theme = Theme.of(context);
2023-09-23 12:56:54 -04:00
//final scale = theme.extension<ScaleScheme>()!;
2023-09-28 10:06:22 -04:00
//final textTheme = theme.textTheme;
2023-09-23 12:56:54 -04:00
//final height = MediaQuery.of(context).size.height;
2023-09-28 10:06:22 -04:00
if (isiOS || isAndroid) {
return Column(mainAxisSize: MainAxisSize.min, children: [
if (!scanned)
Text(
translate('scan_invite_dialog.scan_qr_here'),
).paddingLTRB(0, 0, 0, 8),
if (!scanned)
Container(
constraints: const BoxConstraints(maxHeight: 200),
child: ElevatedButton(
onPressed: dialogState.isValidating
? null
: () async {
final inviteData = await scanQRImage(context);
if (inviteData != null) {
setState(() {
scanned = true;
});
await validateInviteData(inviteData: inviteData);
}
},
child: Text(translate('scan_invite_dialog.scan'))),
).paddingLTRB(0, 0, 0, 8)
]);
}
2023-09-27 13:34:19 -04:00
return Column(mainAxisSize: MainAxisSize.min, children: [
2023-09-28 10:06:22 -04:00
if (!scanned)
Text(
translate('scan_invite_dialog.paste_qr_here'),
).paddingLTRB(0, 0, 0, 8),
if (!scanned)
Container(
constraints: const BoxConstraints(maxHeight: 200),
child: ElevatedButton(
onPressed: dialogState.isValidating
? null
: () async {
final inviteData = await pasteQRImage(context);
if (inviteData != null) {
2023-10-11 23:12:10 -04:00
await validateInviteData(inviteData: inviteData);
2023-09-28 10:06:22 -04:00
setState(() {
scanned = true;
});
}
},
child: Text(translate('scan_invite_dialog.paste'))),
).paddingLTRB(0, 0, 0, 8)
2023-09-27 13:34:19 -04:00
]);
2023-09-23 12:56:54 -04:00
}
@override
2023-09-27 13:34:19 -04:00
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return InviteDialog(
2024-02-14 21:33:15 -05:00
modalContext: widget.modalContext,
2023-09-27 13:34:19 -04:00
onValidationCancelled: onValidationCancelled,
onValidationSuccess: onValidationSuccess,
onValidationFailed: onValidationFailed,
inviteControlIsValid: inviteControlIsValid,
buildInviteControl: buildInviteControl);
2023-09-23 12:56:54 -04:00
}
2023-09-30 21:22:12 -04:00
2023-09-29 22:45:50 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<bool>('scanned', scanned));
}
2023-09-23 12:56:54 -04:00
}