Initial commit
This commit is contained in:
80
lib/pages/new_private_chat/qr_scanner_modal.dart
Normal file
80
lib/pages/new_private_chat/qr_scanner_modal.dart
Normal file
@@ -0,0 +1,80 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:tatchat/l10n/l10n.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';
|
||||
|
||||
class QrScannerModal extends StatefulWidget {
|
||||
final void Function(String) onScan;
|
||||
const QrScannerModal({required this.onScan, super.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: const Icon(Icons.close_outlined),
|
||||
onPressed: Navigator.of(context).pop,
|
||||
tooltip: L10n.of(context).close,
|
||||
),
|
||||
title: Text(L10n.of(context).scanQrCode),
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
QRView(
|
||||
key: qrKey,
|
||||
onQRViewCreated: _onQRViewCreated,
|
||||
overlay: QrScannerOverlayShape(
|
||||
borderColor: Theme.of(context).colorScheme.primary,
|
||||
borderRadius: 10,
|
||||
borderLength: 30,
|
||||
borderWidth: 8,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onQRViewCreated(QRViewController controller) {
|
||||
this.controller = controller;
|
||||
// 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();
|
||||
late StreamSubscription sub;
|
||||
sub = controller.scannedDataStream.listen((scanData) {
|
||||
sub.cancel();
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pop();
|
||||
final data = scanData.code;
|
||||
if (data != null) widget.onScan(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user