fauzie811

qrcode.dart

Dec 10th, 2021
1,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:developer';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:provider/provider.dart';
  6.  
  7. import 'package:qr_code_scanner/qr_code_scanner.dart';
  8. import 'dasboard.dart';
  9. import 'model/undangan.dart';
  10. import 'package:qrcode2/apiTamu.dart';
  11.  
  12. class QRViewExample extends StatefulWidget {
  13.   const QRViewExample({Key? key}) : super(key: key);
  14.  
  15.   @override
  16.   State<StatefulWidget> createState() => _QRViewExampleState();
  17. }
  18.  
  19. class _QRViewExampleState extends State<QRViewExample> {
  20.   Barcode? result;
  21.   QRViewController? controller;
  22.   final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  23.  
  24.   @override
  25.   void reassemble() {
  26.     super.reassemble();
  27.     if (Platform.isAndroid) {
  28.       controller?.pauseCamera();
  29.     }
  30.     controller?.resumeCamera();
  31.   }
  32.  
  33.   @override
  34.   Widget build(BuildContext context) {
  35.     return Scaffold(
  36.       body: Column(
  37.         children: <Widget>[
  38.           if (result == null)
  39.             Expanded(flex: 4, child: _buildQrView(context))
  40.           else
  41.             const CircularProgressIndicator(),
  42.           Expanded(
  43.             flex: 1,
  44.             child: FittedBox(
  45.               fit: BoxFit.contain,
  46.               child: Column(
  47.                 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  48.                 children: <Widget>[
  49.                   if (result != null)
  50.                     Text(
  51.                         'Barcode Type: ${describeEnum(result!.format)}   Data: ${result!.code}')
  52.                   else
  53.                     const Text('Scan a code'),
  54.                   ElevatedButton(
  55.                     onPressed: () {
  56.                       setState(() {
  57.                         result = null;
  58.                       });
  59.                     },
  60.                     child: const Text('coba lagi'),
  61.                   ),
  62.                   Row(
  63.                     mainAxisAlignment: MainAxisAlignment.center,
  64.                     crossAxisAlignment: CrossAxisAlignment.center,
  65.                     children: <Widget>[
  66.                       Container(
  67.                         margin: const EdgeInsets.all(8),
  68.                         child: ElevatedButton(
  69.                           onPressed: () async {
  70.                             await controller?.toggleFlash();
  71.                             setState(() {});
  72.                           },
  73.                           child: FutureBuilder(
  74.                             future: controller?.getFlashStatus(),
  75.                             builder: (context, snapshot) {
  76.                               return Text('Flash: ${snapshot.data}');
  77.                             },
  78.                           ),
  79.                         ),
  80.                       ),
  81.                       Container(
  82.                         margin: const EdgeInsets.all(8),
  83.                         child: ElevatedButton(
  84.                           onPressed: () async {
  85.                             await controller?.flipCamera();
  86.                             setState(() {});
  87.                           },
  88.                           child: FutureBuilder(
  89.                             future: controller?.getCameraInfo(),
  90.                             builder: (context, snapshot) {
  91.                               if (snapshot.data != null) {
  92.                                 return Text(
  93.                                     'Camera facing ${describeEnum(snapshot.data!)}');
  94.                               } else {
  95.                                 return const Text('loading');
  96.                               }
  97.                             },
  98.                           ),
  99.                         ),
  100.                       ),
  101.                     ],
  102.                   ),
  103.                   Row(
  104.                     mainAxisAlignment: MainAxisAlignment.center,
  105.                     crossAxisAlignment: CrossAxisAlignment.center,
  106.                     children: <Widget>[
  107.                       Container(
  108.                         margin: const EdgeInsets.all(8),
  109.                         child: ElevatedButton(
  110.                           onPressed: () async {
  111.                             await controller?.pauseCamera();
  112.                           },
  113.                           child: const Text(
  114.                             'pause',
  115.                             style: TextStyle(fontSize: 20),
  116.                           ),
  117.                         ),
  118.                       ),
  119.                       Container(
  120.                         margin: const EdgeInsets.all(8),
  121.                         child: ElevatedButton(
  122.                           onPressed: () async {
  123.                             await controller?.resumeCamera();
  124.                           },
  125.                           child: const Text('resume',
  126.                               style: TextStyle(fontSize: 20)),
  127.                         ),
  128.                       ),
  129.                     ],
  130.                   ),
  131.                 ],
  132.               ),
  133.             ),
  134.           )
  135.         ],
  136.       ),
  137.     );
  138.   }
  139.  
  140.   Widget _buildQrView(BuildContext context) {
  141.     // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  142.     var scanArea = (MediaQuery.of(context).size.width < 400 ||
  143.             MediaQuery.of(context).size.height < 400)
  144.         ? 400.0
  145.         : 400.0;
  146.     // To ensure the Scanner view is properly sizes after rotation
  147.     // we need to listen for Flutter SizeChanged notification and update controller
  148.     return QRView(
  149.       key: qrKey,
  150.       onQRViewCreated: _onQRViewCreated,
  151.       overlay: QrScannerOverlayShape(
  152.         borderColor: Colors.red,
  153.         borderRadius: 10,
  154.         borderLength: 30,
  155.         borderWidth: 10,
  156.         cutOutSize: scanArea,
  157.       ),
  158.       onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
  159.     );
  160.   }
  161.  
  162.   void _onQRViewCreated(QRViewController controller) {
  163.     setState(() {
  164.       this.controller = controller;
  165.     });
  166.     controller.scannedDataStream.listen((scanData) {
  167.       setState(() {
  168.         result = scanData;
  169.       });
  170.       _cekUndangan(scanData.code!);
  171.     });
  172.   }
  173.  
  174.   _cekUndangan(String code) async {
  175.     final undangan = await ApiUndangan.cekUndangan(code);
  176.  
  177.     if (undangan != null) {
  178.       final undanganProvider = Provider.of<UndanganProvider>(context);
  179.       undanganProvider.undangan = undangan;
  180.  
  181.       Navigator.push(context, MaterialPageRoute(builder: (_) => Dasboard()));
  182.     } else {
  183.       setState(() {
  184.         result = null;
  185.       });
  186.     }
  187.   }
  188.  
  189.   void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
  190.     log('${DateTime.now().toIso8601String()}_onPermissionSet $p');
  191.     if (!p) {
  192.       ScaffoldMessenger.of(context).showSnackBar(
  193.         const SnackBar(content: Text('no Permission')),
  194.       );
  195.     }
  196.   }
  197.  
  198.   @override
  199.   void dispose() {
  200.     controller?.dispose();
  201.     super.dispose();
  202.   }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment