fluffychat/lib/pages/new_private_chat/qr_scanner_modal.dart

76 lines
1.8 KiB
Dart
Raw 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 {
const QrScannerModal({Key key}) : super(key: key);
@override
_QrScannerModalState createState() => _QrScannerModalState();
}
class _QrScannerModalState extends State<QrScannerModal> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
QRViewController controller;
@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
controller.pauseCamera();
} else if (Platform.isIOS) {
controller.resumeCamera();
}
}
@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,
2021-08-24 14:07:49 +02:00
tooltip: L10n.of(context).close,
2021-08-22 21:19:22 +02:00
),
title: Text(L10n.of(context).scanQrCode),
),
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;
2021-08-24 14:07:49 +02:00
StreamSubscription sub;
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();
}
}