import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:mymuseum_visitapp/Components/CustomAppBar.dart'; import 'package:mymuseum_visitapp/Screens/Article/article.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; // NOT USED ANYMORE class ScannerPage extends StatefulWidget { const ScannerPage({Key? key}) : super(key: key); @override State createState() => _ScannerPageState(); } class _ScannerPageState extends State { Barcode? result; QRViewController? controller; final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); // In order to get hot reload to work we need to pause the camera if the platform // is android, or resume the camera if the platform is iOS. @override void reassemble() { print("reassemble "); super.reassemble(); if (Platform.isAndroid) { controller!.pauseCamera(); } controller!.resumeCamera(); } @override Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( title: "QR Code scan", isHomeButton: false, ), body: Column( children: [ Expanded(flex: 4, child: _buildQrView(context)), Expanded( flex: 1, child: FittedBox( fit: BoxFit.contain, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ if (result != null) Text( 'Barcode Type: ${describeEnum(result!.format)} Data: ${result!.code}') else const Text('Scan a code'), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( margin: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () async { await controller?.toggleFlash(); setState(() {}); }, child: FutureBuilder( future: controller?.getFlashStatus(), builder: (context, snapshot) { return Icon(Icons.flash_on); }, )), ), Container( margin: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () async { await controller?.flipCamera(); setState(() {}); }, child: FutureBuilder( future: controller?.getCameraInfo(), builder: (context, snapshot) { if (snapshot.data != null) { return Text( 'Camera facing ${describeEnum(snapshot.data!)}'); } else { return const Text('loading'); } }, )), ) ], ), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( margin: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () async { await controller?.pauseCamera(); }, child: const Text('pause', style: TextStyle(fontSize: 20)), ), ), Container( margin: const EdgeInsets.all(8), child: ElevatedButton( onPressed: () async { //print(controller); await controller?.resumeCamera(); }, child: const Text('resume', style: TextStyle(fontSize: 20)), ), ) ], ), ], ), ), ) ], ), ); } Widget _buildQrView(BuildContext context) { // For this example we check how width or tall the device is and change the scanArea and overlay accordingly. var scanArea = (MediaQuery.of(context).size.width < 400 || MediaQuery.of(context).size.height < 400) ? 225.0 : 300.0; // To ensure the Scanner view is properly sizes after rotation // we need to listen for Flutter SizeChanged notification and update controller return QRView( key: qrKey, onQRViewCreated: _onQRViewCreated, overlay: QrScannerOverlayShape( borderColor: Colors.blueAccent, borderRadius: 10, borderLength: 25, borderWidth: 5, cutOutSize: 225.0), onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p), ); } void _onQRViewCreated(QRViewController controller) { setState(() { this.controller = controller; }); if (Platform.isAndroid) { controller!.pauseCamera(); } controller!.resumeCamera(); controller.scannedDataStream.listen((scanData) { setState(() { result = scanData; print(result); print(result!.code.toString()); var code = result == null ? "" : result!.code.toString(); if(result!.format == BarcodeFormat.qrcode) { controller.pauseCamera(); print("QR CODE FOUND"); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('QR CODE FOUND - ${code.toString()}')), ); // TODO search in local if data == Id of Article // If so, navigate to article view Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) { return ArticlePage(articleId: code); }, ), ); } }); }); } void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) { //log('${DateTime.now().toIso8601String()}_onPermissionSet $p'); if (!p) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('no Permission')), ); } } @override void dispose() { controller?.dispose(); super.dispose(); } }