Navaron Bracke

remove the requirement on having the library present beforehand

... ... @@ -22,6 +22,7 @@ BREAKING CHANGES:
Improvements:
* The `MobileScannerController` is now a ChangeNotifier, with `MobileScannerState` as its model.
* The web implementation now supports alternate URLs for loading the barcode library.
## 4.0.1
Bugs fixed:
... ...
... ... @@ -61,14 +61,22 @@ Ensure that you granted camera permission in XCode -> Signing & Capabilities:
## Web
Include the `ZXing` library in the `<head>` of your `index.html` as a script.
As of version 4.0.0 adding the library to the `index.html` is no longer required,
as the library is automatically loaded on first use.
```html
<head>
<!-- other things in the tag -->
### Providing a mirror for the barcode scanning library
<script src="https://unpkg.com/@zxing/library@0.19.1" type="application/javascript"></script>
</head>
If a different mirror is needed to load the barcode scanning library,
the source URL can be set beforehand.
```dart
import 'package:flutter/foundation.dart';
final String scriptUrl = // ...
if (kIsWeb) {
MobileScannerPlatform.instance.setBarcodeLibraryScriptUrl(scriptUrl);
}
```
## Usage
... ...
... ... @@ -38,9 +38,6 @@
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
<!-- Add the ZXing library to allow `mobile_scanner` to work on the web. -->
<script src="https://unpkg.com/@zxing/library@0.19.1" type="application/javascript"></script>
</head>
<body>
<script>
... ...
... ... @@ -62,6 +62,11 @@ abstract class MobileScannerPlatform extends PlatformInterface {
throw UnimplementedError('resetZoomScale() has not been implemented.');
}
/// Set the source url for the barcode library.
///
/// This is only supported on the web.
void setBarcodeLibraryScriptUrl(String scriptUrl) {}
/// Set the torch state of the active camera.
Future<void> setTorchState(TorchState torchState) {
throw UnimplementedError('setTorchState() has not been implemented.');
... ...
... ... @@ -49,8 +49,11 @@ abstract class BarcodeReader {
/// Load the barcode reader library.
///
/// If [alternateScriptUrl] is provided,
/// the script is loaded from that url instead.
///
/// Does nothing if the library is already loaded.
Future<void> maybeLoadLibrary() async {
Future<void> maybeLoadLibrary({String? alternateScriptUrl}) async {
// Script already exists.
if (document.querySelector('script#$scriptId') != null) {
return;
... ... @@ -66,7 +69,7 @@ abstract class BarcodeReader {
..type = 'application/javascript'
..lang = 'javascript'
..crossOrigin = 'anonymous'
..src = scriptUrl
..src = alternateScriptUrl ?? scriptUrl
..onload = allowInterop((JSAny _) {
if (!completer.isCompleted) {
completer.complete();
... ...
... ... @@ -19,6 +19,9 @@ class MobileScannerWeb extends MobileScannerPlatform {
/// Constructs a [MobileScannerWeb] instance.
MobileScannerWeb();
/// The alternate script url for the barcode library.
String? _alternateScriptUrl;
/// The internal barcode reader.
final BarcodeReader _barcodeReader = ZXingBarcodeReader();
... ... @@ -76,8 +79,15 @@ class MobileScannerWeb extends MobileScannerPlatform {
}
@override
void setBarcodeLibraryScriptUrl(String scriptUrl) {
_alternateScriptUrl ??= scriptUrl;
}
@override
Future<MobileScannerViewAttributes> start(StartOptions startOptions) async {
await _barcodeReader.maybeLoadLibrary();
await _barcodeReader.maybeLoadLibrary(
alternateScriptUrl: _alternateScriptUrl,
);
// Setup the view factory & container element.
if (_divElement == null) {
... ...