|
@@ -0,0 +1,92 @@
|
|
|
+import 'package:fis_common/logger/logger.dart';
|
|
|
+import 'package:flutter/material.dart';
|
|
|
+import 'package:qr_code_scanner/qr_code_scanner.dart';
|
|
|
+
|
|
|
+class QRViewExample extends StatefulWidget {
|
|
|
+ final Function(String) onPressed;
|
|
|
+
|
|
|
+ const QRViewExample({Key? key, required this.onPressed}) : super(key: key);
|
|
|
+
|
|
|
+ @override
|
|
|
+ State<StatefulWidget> createState() => _QRViewExampleState();
|
|
|
+}
|
|
|
+
|
|
|
+class _QRViewExampleState extends State<QRViewExample> {
|
|
|
+ Barcode? result;
|
|
|
+ QRViewController? controller;
|
|
|
+ final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Scaffold(
|
|
|
+ appBar: AppBar(
|
|
|
+ title: Text('扫一扫'),
|
|
|
+ ),
|
|
|
+ body: Column(
|
|
|
+ children: <Widget>[
|
|
|
+ Expanded(
|
|
|
+ flex: 4,
|
|
|
+ child: _buildQrView(
|
|
|
+ context,
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void dispose() {
|
|
|
+ controller?.dispose();
|
|
|
+ super.dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void reassemble() {
|
|
|
+ super.reassemble();
|
|
|
+ controller?.pauseCamera();
|
|
|
+ controller?.resumeCamera();
|
|
|
+ }
|
|
|
+
|
|
|
+ Widget _buildQrView(BuildContext context) {
|
|
|
+ var scanArea = (MediaQuery.of(context).size.width < 400 ||
|
|
|
+ MediaQuery.of(context).size.height < 400)
|
|
|
+ ? 150.0
|
|
|
+ : 300.0;
|
|
|
+ return QRView(
|
|
|
+ key: qrKey,
|
|
|
+ onQRViewCreated: _onQRViewCreated,
|
|
|
+ overlay: QrScannerOverlayShape(
|
|
|
+ borderColor: Theme.of(context).colorScheme.primary,
|
|
|
+ borderRadius: 10,
|
|
|
+ borderLength: 30,
|
|
|
+ borderWidth: 10,
|
|
|
+ cutOutSize: scanArea,
|
|
|
+ ),
|
|
|
+ onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
|
|
|
+ logger.i("QRViewExample _onPermissionSet $p");
|
|
|
+ if (!p) {
|
|
|
+ ScaffoldMessenger.of(context).showSnackBar(
|
|
|
+ const SnackBar(
|
|
|
+ content: Text(
|
|
|
+ 'no Permission',
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void _onQRViewCreated(QRViewController controller) {
|
|
|
+ setState(() {
|
|
|
+ this.controller = controller;
|
|
|
+ });
|
|
|
+ controller.scannedDataStream.listen((event) async {
|
|
|
+ controller.dispose();
|
|
|
+ widget.onPressed.call(event.code!);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|