import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:mymuseum_visitapp/Screens/Article/article.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; class ScannerPage extends StatefulWidget { const ScannerPage({Key? key, required this.title}) : super(key: key); // This widget is the home page of your application. It is stateful, meaning // that it has a State object (defined below) that contains fields that affect // how it looks. // This class is the configuration for the state. It holds the values (in this // case the title) provided by the parent (in this case the App widget) and // used by the build method of the State. Fields in a Widget subclass are // always marked "final". final String title; @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() { super.reassemble(); if (Platform.isAndroid) { controller!.pauseCamera(); } controller!.resumeCamera(); } @override Widget build(BuildContext context) { return Scaffold( 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 { 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; }); controller.scannedDataStream.listen((scanData) { setState(() { result = scanData; print(result); if(result!.format == BarcodeFormat.qrcode) { controller!.pauseCamera(); print("QR CODE FOUND"); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('QR CODE FOUND')), ); // TODO search in local if data == Id of Article // If so, navigate to article view Navigator.push( context, MaterialPageRoute( builder: (context) { return ArticlePage(title: "test"); }, ), ); } }); }); } 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(); } }