fluffychat/lib/pages/new_private_chat/qr_scanner_modal.dart

82 lines
2.1 KiB
Dart
Raw Permalink Normal View History

2021-08-24 14:07:49 +02:00
import 'dart:async';
2021-08-22 21:19:22 +02:00
import 'dart:io';
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
2021-08-22 21:19:22 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:fluffychat/utils/url_launcher.dart';
2021-08-22 21:19:22 +02:00
class QrScannerModal extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const QrScannerModal({Key? key}) : super(key: key);
2021-08-22 21:19:22 +02:00
@override
_QrScannerModalState createState() => _QrScannerModalState();
}
class _QrScannerModalState extends State<QrScannerModal> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
2022-01-29 12:35:03 +01:00
QRViewController? controller;
2021-08-22 21:19:22 +02:00
@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
2022-01-29 12:35:03 +01:00
controller!.pauseCamera();
2021-08-22 21:19:22 +02:00
} else if (Platform.isIOS) {
2022-01-29 12:35:03 +01:00
controller!.resumeCamera();
2021-08-22 21:19:22 +02:00
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.close_outlined),
2021-08-22 21:19:22 +02:00
onPressed: Navigator.of(context).pop,
2022-01-29 12:35:03 +01:00
tooltip: L10n.of(context)!.close,
2021-08-22 21:19:22 +02:00
),
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.scanQrCode),
2021-08-22 21:19:22 +02:00
),
2021-08-24 14:07:49 +02:00
body: Stack(
children: [
QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Theme.of(context).primaryColor,
borderRadius: 10,
borderLength: 30,
borderWidth: 8,
),
),
],
2021-08-22 21:19:22 +02:00
),
);
}
void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
2022-05-30 09:24:26 +02:00
// Workaround for QR Scanner is started in Pause mode
// https://github.com/juliuscanute/qr_code_scanner/issues/538#issuecomment-1133883828
if (Platform.isAndroid) {
controller.pauseCamera();
}
controller.resumeCamera();
2022-01-29 12:35:03 +01:00
late StreamSubscription sub;
2021-08-24 14:07:49 +02:00
sub = controller.scannedDataStream.listen((scanData) {
sub.cancel();
2021-08-22 21:19:22 +02:00
Navigator.of(context).pop();
UrlLauncher(context, scanData.code).openMatrixToUrl();
});
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}