Advertisement
taufiqjack

QrScannerView

Jan 19th, 2023 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 10.10 KB | None | 0 0
  1. import 'package:blurrycontainer/blurrycontainer.dart';
  2. import 'package:clipboard/clipboard.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:icons_plus/icons_plus.dart';
  7. import 'package:mobile_scanner/mobile_scanner.dart';
  8. import 'package:qr_maker_scan/utils/route.dart';
  9. import 'package:qr_maker_scan/views/dashboard_view.dart';
  10. import 'package:toast/toast.dart';
  11. import 'package:url_launcher/url_launcher_string.dart';
  12.  
  13. class ScannerView extends StatefulWidget {
  14.   const ScannerView({Key? key}) : super(key: key);
  15.  
  16.   @override
  17.   State<ScannerView> createState() => _ScannerViewState();
  18. }
  19.  
  20. class _ScannerViewState extends State<ScannerView>
  21.     with SingleTickerProviderStateMixin {
  22.   static const flashlighton = FontAwesome.bolt;
  23.   static const flashlightoff = CupertinoIcons.bolt_slash_fill;
  24.   static const frontCamera = 'Kamera Depan';
  25.   // static const backCamera = 'Kamera Belakang';
  26.  
  27.   // Barcode? qrcode;
  28.   var flashState = flashlightoff;
  29.   var cameraState = frontCamera;
  30.   // QRViewController? controller;
  31.   MobileScannerController controller = MobileScannerController();
  32.   String? result;
  33.  
  34.   final GlobalKey qrKey = GlobalKey();
  35.  
  36.   Future<void> _launchBrowser(String url) async {
  37.     if (await canLaunchUrlString(url)) {
  38.       await launchUrlString(
  39.         url,
  40.         mode: LaunchMode.externalApplication,
  41.       );
  42.     } else {
  43.       Toast.show('link url bermasalah', gravity: Toast.bottom);
  44.     }
  45.   }
  46.  
  47.   bool _isFlashOn(IconData current) {
  48.     return flashlighton == current;
  49.   }
  50.  
  51.   @override
  52.   void dispose() {
  53.     super.dispose();
  54.     controller.dispose();
  55.   }
  56.  
  57.   @override
  58.   Widget build(BuildContext context) {
  59.     return Scaffold(
  60.       body: Stack(children: [
  61.         MobileScanner(
  62.           controller: controller,
  63.           onDetect: ((qrcode, args) {
  64.             setState(() {
  65.               result = qrcode.rawValue;
  66.             });
  67.           }),
  68.         ),
  69.         Positioned(
  70.           top: 0,
  71.           left: 0,
  72.           right: 0,
  73.           child: Align(
  74.             alignment: Alignment.topLeft,
  75.             child: Padding(
  76.               padding: const EdgeInsets.only(
  77.                 top: 50,
  78.                 bottom: 10,
  79.                 left: 10,
  80.               ),
  81.               child: InkWell(
  82.                 onTap: () {
  83.                   Go.back();
  84.                 },
  85.                 child: const Icon(
  86.                   Icons.arrow_back,
  87.                   color: Colors.white,
  88.                 ),
  89.               ),
  90.             ),
  91.           ),
  92.         ),
  93.         Positioned(
  94.           top: MediaQuery.of(context).size.height / 10,
  95.           left: 0,
  96.           right: 0,
  97.           child: const Align(
  98.             alignment: Alignment.center,
  99.             child: Padding(
  100.                 padding: EdgeInsets.only(
  101.                   top: 50,
  102.                   bottom: 10,
  103.                   left: 10,
  104.                 ),
  105.                 child: Text(
  106.                   'Pindai kode QR pada\nperangkat',
  107.                   textAlign: TextAlign.center,
  108.                   style: TextStyle(
  109.                       fontSize: 16,
  110.                       color: Colors.white,
  111.                       fontWeight: FontWeight.w500),
  112.                 )),
  113.           ),
  114.         ),
  115.         Positioned(
  116.           bottom: 0,
  117.           left: 0,
  118.           right: 0,
  119.           child: BlurryContainer(
  120.             elevation: 0,
  121.             height: MediaQuery.of(context).size.height / 4,
  122.             blur: 5,
  123.             borderRadius: const BorderRadius.only(
  124.               topLeft: Radius.circular(20),
  125.               topRight: Radius.circular(20),
  126.             ),
  127.             color: Colors.black.withOpacity(0.3),
  128.             child: Align(
  129.               alignment: Alignment.bottomCenter,
  130.               child: Column(
  131.                 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  132.                 children: [
  133.                   if (controller == null)
  134.                     Row(
  135.                       mainAxisAlignment: MainAxisAlignment.spaceAround,
  136.                       children: [
  137.                         Text(
  138.                           'hasil convert : $result',
  139.                           style:
  140.                               TextStyle(color: Colors.white.withOpacity(0.8)),
  141.                         ),
  142.                         const SizedBox(
  143.                           width: 10,
  144.                         ),
  145.                         InkWell(
  146.                           onTap: () async {
  147.                             FlutterClipboard.copy('$result').then((value) {
  148.                               if (kDebugMode) {
  149.                                 print('copied');
  150.                               }
  151.                               Toast.show('Teks disalin', gravity: Toast.bottom);
  152.                             });
  153.                           },
  154.                           child: Icon(Icons.copy,
  155.                               color: Colors.white.withOpacity(0.8)),
  156.                         ),
  157.                       ],
  158.                     )
  159.                   else if (result != null)
  160.                     // else
  161.                     Text(
  162.                       'Scan Kode',
  163.                       style: TextStyle(color: Colors.white.withOpacity(0.8)),
  164.                     )
  165.                   else
  166.                     const SizedBox(),
  167.                   const SizedBox(
  168.                     height: 10,
  169.                   ),
  170.                   result == null ? const SizedBox() : openLink(context),
  171.                   Container(
  172.                       height: 50,
  173.                       width: 50,
  174.                       decoration: BoxDecoration(
  175.                           border: Border.all(
  176.                               color: _isFlashOn(flashState)
  177.                                   ? Colors.orange
  178.                                   : Colors.grey,
  179.                               width: 2),
  180.                           borderRadius: BorderRadius.circular(30)),
  181.                       child: InkWell(
  182.                         onTap: () {
  183.                           controller.toggleTorch();
  184.                           if (_isFlashOn(flashState)) {
  185.                             setState(() {
  186.                               flashState = flashlightoff;
  187.                             });
  188.                           } else {
  189.                             setState(() {
  190.                               flashState = flashlighton;
  191.                             });
  192.                           }
  193.                         },
  194.                         child: Icon(
  195.                           flashState,
  196.                           color: _isFlashOn(flashState)
  197.                               ? Colors.orange
  198.                               : Colors.grey,
  199.                         ),
  200.                       )),
  201.                   const SizedBox(
  202.                     height: 50,
  203.                   )
  204.                 ],
  205.               ),
  206.             ),
  207.           ),
  208.         ),
  209.       ]),
  210.     );
  211.   }
  212.  
  213.   Future<bool> exitApp() async {
  214.     Navigator.pushAndRemoveUntil(
  215.         context,
  216.         MaterialPageRoute(builder: (context) => const DashboardView()),
  217.         (route) => false);
  218.     return false;
  219.   }
  220.  
  221.   openLink(BuildContext context) {
  222.     Future.delayed(const Duration(seconds: 1), () {
  223.       showDialog(
  224.           context: context,
  225.           builder: (context) {
  226.             return ConstrainedBox(
  227.                 constraints: const BoxConstraints(maxHeight: 100),
  228.                 child: AlertDialog(
  229.                   content: Column(
  230.                     mainAxisSize: MainAxisSize.min,
  231.                     children: [
  232.                       const Text('Buka Link?'),
  233.                       Row(
  234.                         mainAxisAlignment: MainAxisAlignment.spaceAround,
  235.                         children: [
  236.                           Text('$result'),
  237.                           InkWell(
  238.                             onTap: () async {
  239.                               FlutterClipboard.copy('$result').then((value) {
  240.                                 if (kDebugMode) {
  241.                                   print('copied');
  242.                                 }
  243.                                 Toast.show('Teks disalin',
  244.                                     gravity: Toast.bottom);
  245.                               });
  246.                             },
  247.                             child: Icon(Icons.copy,
  248.                                 color: const Color.fromARGB(255, 0, 0, 0)
  249.                                     .withOpacity(0.8)),
  250.                           ),
  251.                         ],
  252.                       ),
  253.                     ],
  254.                   ),
  255.                   actions: [
  256.                     InkWell(
  257.                       onTap: () {
  258.                         _launchBrowser('$result');
  259.                       },
  260.                       child: Container(
  261.                         height: 30,
  262.                         width: 50,
  263.                         decoration: BoxDecoration(
  264.                             borderRadius: BorderRadius.circular(8),
  265.                             color: Colors.blue.shade300),
  266.                         child: const Center(
  267.                             child: Text(
  268.                           'OK',
  269.                           style: TextStyle(color: Colors.white),
  270.                         )),
  271.                       ),
  272.                     ),
  273.                     InkWell(
  274.                       onTap: () {
  275.                         Go.back();
  276.                       },
  277.                       child: Container(
  278.                         height: 30,
  279.                         width: 50,
  280.                         decoration: BoxDecoration(
  281.                             borderRadius: BorderRadius.circular(8),
  282.                             color: Colors.grey),
  283.                         child: const Center(
  284.                             child: Text(
  285.                           'Cancel',
  286.                           style: TextStyle(color: Colors.white),
  287.                         )),
  288.                       ),
  289.                     ),
  290.                   ],
  291.                 ));
  292.           });
  293.     });
  294.     return const SizedBox();
  295.   }
  296. }
Tags: qrscanner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement