Advertisement
fauzie811

qr.dart

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