Possibilities to scan qr-code in flutter web
Following these two articels, I managed to use the jsQR Library in my flutter web application.
https://medium.com/@mk.pyts/how-to-access-webcam-video-stream-in-flutter-for-web-1bdc74f2e9c7
https://medium.com/flutter-community/using-javascript-code-in-flutter-web-903de54a2000
By this, I'm now able to scan a QR-code in flutter web.
I used the library ai_barcode
.
Documentation of the plugin is teribble so I post here my widget:
import 'package:ai_barcode/ai_barcode.dart';import 'package:flutter/material.dart';class _BarcodeScannerWidget extends StatefulWidget { const _BarcodeScannerWidget(this.resultCallback); final void Function(String result) resultCallback; @override State<StatefulWidget> createState() { return _AppBarcodeScannerWidgetState(); }}class _AppBarcodeScannerWidgetState extends State<_BarcodeScannerWidget> { late ScannerController _scannerController; @override void initState() { super.initState(); _scannerController = ScannerController(scannerResult: (result) { widget.resultCallback(result); }, scannerViewCreated: () { final TargetPlatform platform = Theme.of(context).platform; if (TargetPlatform.iOS == platform) { Future.delayed(const Duration(seconds: 2), () { _scannerController ..startCamera() ..startCameraPreview(); }); } else { _scannerController ..startCamera() ..startCameraPreview(); } }); } @override void dispose() { super.dispose(); _scannerController ..stopCameraPreview() ..stopCamera(); } @override Widget build(BuildContext context) { return PlatformAiBarcodeScannerWidget( platformScannerController: _scannerController, ); }}
Also add the jsQR.js from plugin repository.
ai_barcode: ^3.0.1
And add this line to index.html:
<script src="jsQR.js" type="application/javascript"></script> <!-- Add this line --><script src="main.dart.js" type="application/javascript"></script>