123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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';
- 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');
- CameraFacing _cameraFacing = CameraFacing.front;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: VAppBar(
- titleText: "扫一扫",
- actions: [
- // IconButton(
- // onPressed: () {
- // setState(() {
- // _cameraFacing = _cameraFacing == CameraFacing.back
- // ? CameraFacing.front
- // : CameraFacing.back;
- // });
- // },
- // 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) {
- var scanArea = (MediaQuery.of(context).size.width < 400 ||
- MediaQuery.of(context).size.height < 400)
- ? 150.0
- : 300.0;
- return QRView(
- key: qrKey,
- cameraFacing: _cameraFacing,
- 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!);
- });
- }
- }
|