qr_view_example.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class QRViewExample extends StatefulWidget {
  6. final Function(String) onPressed;
  7. const QRViewExample({Key? key, required this.onPressed}) : super(key: key);
  8. @override
  9. State<StatefulWidget> createState() => _QRViewExampleState();
  10. }
  11. class _QRViewExampleState extends State<QRViewExample> {
  12. Barcode? result;
  13. QRViewController? controller;
  14. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  15. CameraFacing _cameraFacing = CameraFacing.front;
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: VAppBar(
  20. titleText: "扫一扫",
  21. actions: [
  22. // IconButton(
  23. // onPressed: () {
  24. // setState(() {
  25. // _cameraFacing = _cameraFacing == CameraFacing.back
  26. // ? CameraFacing.front
  27. // : CameraFacing.back;
  28. // });
  29. // },
  30. // icon: Icon(
  31. // Icons.change_circle_outlined,
  32. // color: Colors.white,
  33. // size: 36,
  34. // ),
  35. // ),
  36. // SizedBox(
  37. // width: 8,
  38. // ),
  39. ],
  40. ),
  41. body: Column(
  42. children: <Widget>[
  43. Expanded(
  44. flex: 4,
  45. child: _buildQrView(
  46. context,
  47. ),
  48. ),
  49. ],
  50. ),
  51. );
  52. }
  53. @override
  54. void dispose() {
  55. controller?.dispose();
  56. super.dispose();
  57. }
  58. @override
  59. void reassemble() {
  60. super.reassemble();
  61. controller?.pauseCamera();
  62. controller?.resumeCamera();
  63. }
  64. Widget _buildQrView(BuildContext context) {
  65. var scanArea = (MediaQuery.of(context).size.width < 400 ||
  66. MediaQuery.of(context).size.height < 400)
  67. ? 150.0
  68. : 300.0;
  69. return QRView(
  70. key: qrKey,
  71. cameraFacing: _cameraFacing,
  72. onQRViewCreated: _onQRViewCreated,
  73. overlay: QrScannerOverlayShape(
  74. borderColor: Theme.of(context).colorScheme.primary,
  75. borderRadius: 10,
  76. borderLength: 30,
  77. borderWidth: 10,
  78. cutOutSize: scanArea,
  79. ),
  80. onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
  81. );
  82. }
  83. void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
  84. logger.i("QRViewExample _onPermissionSet $p");
  85. if (!p) {
  86. ScaffoldMessenger.of(context).showSnackBar(
  87. const SnackBar(
  88. content: Text(
  89. 'no Permission',
  90. ),
  91. ),
  92. );
  93. }
  94. }
  95. void _onQRViewCreated(QRViewController controller) {
  96. setState(() {
  97. this.controller = controller;
  98. });
  99. controller.scannedDataStream.listen((event) async {
  100. controller.dispose();
  101. widget.onPressed.call(event.code!);
  102. });
  103. }
  104. }