Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Navaron Bracke
2024-01-08 13:32:49 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9de49958ef67be8a4f368b9e6ca509cf62fd61f9
9de49958
1 parent
27d54746
remove the requirement on having the library present beforehand
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
12 deletions
CHANGELOG.md
README.md
example/web/index.html
lib/src/mobile_scanner_platform_interface.dart
lib/src/web/barcode_reader.dart
lib/src/web/mobile_scanner_web.dart
CHANGELOG.md
View file @
9de4995
...
...
@@ -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:
...
...
README.md
View file @
9de4995
...
...
@@ -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
...
...
example/web/index.html
View file @
9de4995
...
...
@@ -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>
...
...
lib/src/mobile_scanner_platform_interface.dart
View file @
9de4995
...
...
@@ -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.'
);
...
...
lib/src/web/barcode_reader.dart
View file @
9de4995
...
...
@@ -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
();
...
...
lib/src/web/mobile_scanner_web.dart
View file @
9de4995
...
...
@@ -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
)
{
...
...
Please
register
or
login
to post a comment