123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import 'package:fis_common/logger/logger.dart';
- import 'package:flutter/material.dart';
- import 'package:qr_code_scanner/qr_code_scanner.dart';
- import 'package:vitalapp/components/appbar.dart';
- import 'dart:math' as math;
- class QRViewExample extends StatefulWidget {
- final Function(String) onScanSuccess;
- final bool isBarcode;
- const QRViewExample({
- Key? key,
- required this.onScanSuccess,
- this.isBarcode = false,
- }) : super(key: key);
- @override
- State<StatefulWidget> createState() => _QRViewExampleState();
- }
- class _QRViewExampleState extends State<QRViewExample> {
- Barcode? result;
- QRViewController? controller;
- final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
- CameraFacing _cameraFacing = CameraFacing.front;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: VAppBar(
- titleText: "扫一扫",
- actions: [
- IconButton(
- onPressed: () async {
- await controller?.flipCamera();
- await Future.delayed(Duration(milliseconds: 500));
- setState(() {
- _cameraFacing = _cameraFacing == CameraFacing.front
- ? CameraFacing.back
- : CameraFacing.front;
- });
- },
- icon: Icon(
- Icons.change_circle_outlined,
- color: Colors.white,
- size: 36,
- ),
- ),
- SizedBox(
- width: 8,
- ),
- ],
- ),
- 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) {
- return Transform(
- transform:
- Matrix4.rotationY(_cameraFacing == CameraFacing.front ? math.pi : 0),
- alignment: Alignment.center,
- child: QRView(
- key: qrKey,
- cameraFacing: _cameraFacing,
- onQRViewCreated: _onQRViewCreated,
- overlay: QrScannerOverlayShape(
- borderColor: Theme.of(context).colorScheme.primary,
- borderRadius: 10,
- borderLength: 30,
- borderWidth: 10,
- // cutOutSize: 400,
- cutOutWidth: 300,
- cutOutHeight: 300,
- ),
- formatsAllowed: widget.isBarcode
- ? [
- ///一维码则指定使用128的格式,会识别快一点
- // BarcodeFormat.code128,
- BarcodeFormat.qrcode,
- ]
- : [],
- 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.onScanSuccess.call(event.code!);
- });
- }
- }
|