qr_view_example.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:fis_common/logger/logger.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:qr_code_scanner/qr_code_scanner.dart';
  4. import 'package:vitalapp/components/appbar.dart';
  5. import 'dart:math' as math;
  6. class QRViewExample extends StatefulWidget {
  7. final Function(String) onScanSuccess;
  8. final bool isBarcode;
  9. const QRViewExample({
  10. Key? key,
  11. required this.onScanSuccess,
  12. this.isBarcode = false,
  13. }) : super(key: key);
  14. @override
  15. State<StatefulWidget> createState() => _QRViewExampleState();
  16. }
  17. class _QRViewExampleState extends State<QRViewExample> {
  18. Barcode? result;
  19. QRViewController? controller;
  20. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  21. CameraFacing _cameraFacing = CameraFacing.front;
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: VAppBar(
  26. titleText: "扫一扫",
  27. actions: [
  28. IconButton(
  29. onPressed: () async {
  30. await controller?.flipCamera();
  31. await Future.delayed(Duration(milliseconds: 500));
  32. setState(() {
  33. _cameraFacing = _cameraFacing == CameraFacing.front
  34. ? CameraFacing.back
  35. : CameraFacing.front;
  36. });
  37. },
  38. icon: Icon(
  39. Icons.change_circle_outlined,
  40. color: Colors.white,
  41. size: 36,
  42. ),
  43. ),
  44. SizedBox(
  45. width: 8,
  46. ),
  47. ],
  48. ),
  49. body: Column(
  50. children: <Widget>[
  51. Expanded(
  52. flex: 4,
  53. child: _buildQrView(
  54. context,
  55. ),
  56. ),
  57. ],
  58. ),
  59. );
  60. }
  61. @override
  62. void dispose() {
  63. controller?.dispose();
  64. super.dispose();
  65. }
  66. @override
  67. void reassemble() {
  68. super.reassemble();
  69. controller?.pauseCamera();
  70. controller?.resumeCamera();
  71. }
  72. Widget _buildQrView(BuildContext context) {
  73. return Transform(
  74. transform:
  75. Matrix4.rotationY(_cameraFacing == CameraFacing.front ? math.pi : 0),
  76. alignment: Alignment.center,
  77. child: QRView(
  78. key: qrKey,
  79. cameraFacing: _cameraFacing,
  80. onQRViewCreated: _onQRViewCreated,
  81. overlay: QrScannerOverlayShape(
  82. borderColor: Theme.of(context).colorScheme.primary,
  83. borderRadius: 10,
  84. borderLength: 30,
  85. borderWidth: 10,
  86. // cutOutSize: 400,
  87. cutOutWidth: 300,
  88. cutOutHeight: 300,
  89. ),
  90. formatsAllowed: widget.isBarcode
  91. ? [
  92. ///一维码则指定使用128的格式,会识别快一点
  93. // BarcodeFormat.code128,
  94. BarcodeFormat.qrcode,
  95. ]
  96. : [],
  97. onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
  98. ),
  99. );
  100. }
  101. void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
  102. logger.i("QRViewExample _onPermissionSet $p");
  103. if (!p) {
  104. ScaffoldMessenger.of(context).showSnackBar(
  105. const SnackBar(
  106. content: Text(
  107. 'no Permission',
  108. ),
  109. ),
  110. );
  111. }
  112. }
  113. void _onQRViewCreated(QRViewController controller) {
  114. setState(() {
  115. this.controller = controller;
  116. });
  117. controller.scannedDataStream.listen((event) async {
  118. controller.dispose();
  119. widget.onScanSuccess.call(event.code!);
  120. });
  121. }
  122. }