fluffychat/lib/pages/qr_scanner_modal.dart
2021-08-22 21:19:22 +02:00

60 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:fluffychat/utils/url_launcher.dart';
import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
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(
icon: Icon(Icons.close_outlined),
onPressed: Navigator.of(context).pop,
),
title: Text(L10n.of(context).scanQrCode),
),
body: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
);
}
void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
Navigator.of(context).pop();
UrlLauncher(context, scanData.code).openMatrixToUrl();
});
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}