Skip to content

Commit 4abc91e

Browse files
fix: turn off scanning process after barcode is scanned or cancelled
1 parent f61608c commit 4abc91e

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

lib/assets/barcode.html

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,49 @@
2121
<!-- Div to show the scanner -->
2222
<div id="reader" ></div>
2323
<script>
24-
const html5QrCode = new Html5Qrcode("reader");
25-
console.log("Starting SCANNGING CODE");
26-
const qrCodeSuccessCallback = (decodedText, decodedResult) => {
27-
/* html5QrCode.stop(); */
28-
/* handle success for web */
29-
window.parent.postMessage(decodedText, "*");
24+
//refer doc here https://github.com/mebjas/html5-qrcode
25+
const html5QrCode = new Html5Qrcode("reader");
26+
console.log("Starting SCANNING CODE");
27+
const qrCodeSuccessCallback = (decodedText, decodedResult) => {
28+
html5QrCode.stop();
29+
/* handle success for web */
30+
window.parent.postMessage(decodedText, "*");
3031

31-
/* handle success for window */
32-
if(window.chrome.webview!="undefined"){
33-
var param = {
34-
"methodName":"successCallback",
35-
"data":decodedText
36-
}
37-
window.chrome.webview.postMessage(param);
38-
}
32+
/* handle success for window */
33+
if (window.chrome.webview != "undefined") {
34+
var param = {
35+
"methodName": "successCallback",
36+
"data": decodedText
37+
}
38+
window.chrome.webview.postMessage(param);
39+
}
3940

40-
};
41-
const config = { fps: 10, qrbox: { width: 280, height: 120, aspectRatio: 1.7777778 } };
41+
};
42+
const config = {
43+
fps: 10,
44+
qrbox: {
45+
width: 280,
46+
height: 120,
47+
aspectRatio: 1.7777778
48+
}
49+
};
4250

43-
// If you want to prefer back camera
44-
html5QrCode.start({ facingMode: "environment" }, config, qrCodeSuccessCallback);
45-
//html5QrCode.start({ facingMode: "user" }, config, qrCodeSuccessCallback);
51+
// If you want to prefer back camera
52+
html5QrCode.start({
53+
facingMode: "environment"
54+
}, config, qrCodeSuccessCallback);
55+
//html5QrCode.start({ facingMode: "user" }, config, qrCodeSuccessCallback);
4656

57+
//Window event listener
58+
if (window.chrome.webview != undefined) {
59+
window.chrome.webview.addEventListener('message', function(e) {
60+
let data = JSON.parse(JSON.stringify(e.data));
61+
if (data.event === "close") {
62+
html5QrCode.stop();
63+
}
4764

48-
</script>
65+
});
66+
}
67+
</script>
4968
</body>
5069
</html>

lib/screens/window.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import 'dart:convert';
12
import 'dart:io';
3+
24
import 'package:flutter/material.dart';
5+
import 'package:path/path.dart' as p;
36
import 'package:simple_barcode_scanner/constant.dart';
47
import 'package:simple_barcode_scanner/enum.dart';
58
import 'package:webview_windows/webview_windows.dart';
6-
import 'package:path/path.dart' as p;
79

810
class WindowBarcodeScanner extends StatelessWidget {
911
final String lineColor;
@@ -28,6 +30,14 @@ class WindowBarcodeScanner extends StatelessWidget {
2830
return Scaffold(
2931
appBar: AppBar(
3032
title: Text(kScanPageTitle),
33+
leading: IconButton(
34+
onPressed: () {
35+
/// send close event to web-view
36+
controller.postWebMessage(json.encode({"event": "close"}));
37+
Navigator.pop(context);
38+
},
39+
icon: const Icon(Icons.arrow_back_ios),
40+
),
3141
),
3242
body: FutureBuilder<bool>(
3343
future: initPlatformState(
@@ -39,11 +49,12 @@ class WindowBarcodeScanner extends StatelessWidget {
3949
controller,
4050
permissionRequested: (url, permissionKind, isUserInitiated) =>
4151
_onPermissionRequested(
42-
url: url,
43-
kind: permissionKind,
44-
isUserInitiated: isUserInitiated,
45-
context: context,
46-
isPermissionGranted: isPermissionGranted),
52+
url: url,
53+
kind: permissionKind,
54+
isUserInitiated: isUserInitiated,
55+
context: context,
56+
isPermissionGranted: isPermissionGranted,
57+
),
4758
);
4859
} else if (snapshot.hasError) {
4960
return Center(

0 commit comments

Comments
 (0)