import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:url_launcher/url_launcher.dart'; class LabelScanBox extends StatelessWidget { const LabelScanBox({ Key? key, }) : super(key: key); Future scan() async { try { var barcode = await BarcodeScanner.scan(); if (barcode.type.name == 'Cancelled') { return 'cancel'; } else if (barcode.type.name == 'Failed') { return 'error'; } // print(barcode.split('/')); try { var id = barcode.rawContent.contains('www.cssd-doc.com') ? barcode.rawContent.split('/')[5] : barcode.rawContent; return id; } catch (_) { return 'null'; } } on PlatformException catch (_) { return 'access'; } catch (_) { return 'error'; } // } on FormatException { // setState(() => this.testCode = // 'null (User returned using the "back"-button before scanning anything. Result)'); // } catch (e) { // setState(() => this.testCode = 'Unknown error: $e'); // } // return null; } @override Widget build(BuildContext context) { final _media = MediaQuery.of(context); // final double _pixelRatio = (_media.size.height / _media.size.width * 7 / 5); final double _devicewidth = _media.size.width; final double _deviceBlockSize = _devicewidth / 100; return GestureDetector( child: Container( margin: EdgeInsets.symmetric(vertical: 10), height: 200, decoration: BoxDecoration(border: Border.all()), child: Center( child: Text( 'Please Scan The Label Barcode', style: TextStyle( color: Colors.black, fontSize: 5 * _deviceBlockSize, fontWeight: FontWeight.w100, letterSpacing: 3, ), textAlign: TextAlign.center, ), ), ), onTap: () { scan().then( (String result) { if (result == 'cancel') { print('cancel'); } else if (result == 'error') { print('error'); } else if (result == 'access') { print('access'); } else { launch( 'https://cssd-doc.com/Show/Label/$result', ); // Navigator.of(context).pushNamed( // Routes.labelDetailScreen, // arguments: result, // ); } }, ); }, ); } }