You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.3KB

  1. import 'dart:io';
  2. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:qr_code_scanner/qr_code_scanner.dart';
  6. class QrCodeScannerScreen extends StatefulWidget {
  7. @override
  8. State<StatefulWidget> createState() => _QrCodeScannerScreenState();
  9. }
  10. class _QrCodeScannerScreenState extends State<QrCodeScannerScreen> {
  11. // Barcode? result;
  12. QRViewController controller;
  13. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  14. // In order to get hot reload to work we need to pause the camera if the platform
  15. // is android, or resume the camera if the platform is iOS.
  16. @override
  17. void reassemble() {
  18. super.reassemble();
  19. if (Platform.isAndroid) {
  20. controller.pauseCamera();
  21. }
  22. controller.resumeCamera();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return Scaffold(
  27. appBar: AppBarWidget(
  28. isBack: true,
  29. ),
  30. body: Column(
  31. children: <Widget>[
  32. Expanded(flex: 4, child: _buildQrView(context)),
  33. ],
  34. ),
  35. );
  36. }
  37. Widget _buildQrView(BuildContext context) {
  38. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  39. var scanArea = (MediaQuery.of(context).size.width < 400 || MediaQuery.of(context).size.height < 400) ? 150.0 : 300.0;
  40. // To ensure the Scanner view is properly sizes after rotation
  41. // we need to listen for Flutter SizeChanged notification and update controller
  42. return QRView(
  43. key: qrKey,
  44. onQRViewCreated: _onQRViewCreated,
  45. overlay: QrScannerOverlayShape(borderColor: Colors.red, borderRadius: 10, borderLength: 30, borderWidth: 10, cutOutSize: scanArea),
  46. );
  47. }
  48. void _onQRViewCreated(QRViewController controller) {
  49. setState(() {
  50. this.controller = controller;
  51. });
  52. controller.scannedDataStream.listen((scanData) {
  53. if (scanData != null) {
  54. Get.back(result: scanData);
  55. }
  56. });
  57. }
  58. void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
  59. if (!p) {
  60. ScaffoldMessenger.of(context).showSnackBar(
  61. const SnackBar(content: Text('no Permission')),
  62. );
  63. }
  64. }
  65. @override
  66. void dispose() {
  67. controller?.dispose();
  68. super.dispose();
  69. }
  70. }