Skip to content

Commit efd30d2

Browse files
updated scanner page
1 parent 5567a27 commit efd30d2

File tree

11 files changed

+206
-69
lines changed

11 files changed

+206
-69
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.9
2+
* `SimpleBarcodeScannerPage` is now depreciated, use `SimpleBarcodeScanner` instead
3+
* Scan barcode with `SimpleBarcodeScanner` `scanBarcode` method
4+
* Stream barcode with `SimpleBarcodeScanner` `streamBarcode` method
5+
16
## 0.1.8
27
* IOS build issue fixed
38
* Delay feature added for android and ios

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ project, I'd appreciate your [🌟 on GitHub](https://github.com/CodingWithTashi
7575

7676
## publish
7777

78-
```agsl
78+
```
7979
dart pub publish --dry-run
8080
```
8181
```
8282
dart pub publish
83-
```
83+
```
84+

android/src/main/java/com/amolg/flutterbarcodescanner/BarcodeCaptureActivity.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ enum USE_FLASH {
100100

101101
private int flashStatus = USE_FLASH.OFF.ordinal();
102102

103+
104+
@Override
105+
public void onBackPressed() {
106+
Barcode barcode = new Barcode();
107+
// -2 is the code for user cancelled the scan
108+
barcode.rawValue = "-2";
109+
barcode.displayValue = "-2";
110+
FlutterBarcodeScannerPlugin.onBarcodeScanReceiver(barcode);
111+
finish();
112+
}
113+
103114
/**
104115
* Initializes the UI and creates the detector pipeline.
105116
*/
@@ -415,11 +426,8 @@ public void onClick(View v) {
415426
Log.e("BarcodeCaptureActivity", "FlashOnFailure: " + e.getLocalizedMessage());
416427
}
417428
} else if (i == R.id.btnBarcodeCaptureCancel) {
418-
Barcode barcode = new Barcode();
419-
barcode.rawValue = "-1";
420-
barcode.displayValue = "-1";
421-
FlutterBarcodeScannerPlugin.onBarcodeScanReceiver(barcode);
422-
finish();
429+
onBackPressed();
430+
423431
} else if (i == R.id.imgViewSwitchCamera) {
424432
int currentFacing = mCameraSource.getCameraFacing();
425433
boolean autoFocus = mCameraSource.getFocusMode() != null;

example/lib/main.dart

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,48 @@ class _HomePageState extends State<HomePage> {
3939
children: [
4040
ElevatedButton(
4141
onPressed: () async {
42-
var res = await Navigator.push(
43-
context,
44-
MaterialPageRoute(
45-
builder: (context) => const SimpleBarcodeScannerPage(
46-
barcodeAppBar: BarcodeAppBar(
47-
appBarTitle: 'Test',
48-
centerTitle: false,
49-
enableBackButton: true,
50-
backButtonIcon: Icon(Icons.arrow_back_ios),
51-
),
52-
delayMillis: 2000,
53-
child: Column(
54-
children: [
55-
SizedBox(
56-
height: 20,
57-
),
58-
TextField(
59-
decoration: InputDecoration(
60-
labelText: 'Enter Text',
61-
border: OutlineInputBorder(),
62-
),
63-
),
64-
],
65-
),
66-
),
67-
));
42+
String? res = await SimpleBarcodeScanner.scanBarcode(
43+
context,
44+
barcodeAppBar: const BarcodeAppBar(
45+
appBarTitle: 'Test',
46+
centerTitle: false,
47+
enableBackButton: true,
48+
backButtonIcon: Icon(Icons.arrow_back_ios),
49+
),
50+
isShowFlashIcon: true,
51+
delayMillis: 2000,
52+
);
6853
setState(() {
69-
if (res is String) {
70-
result = res;
71-
}
54+
result = res as String;
7255
});
7356
},
74-
child: const Text('Open Scanner'),
57+
child: const Text('Scan Barcode'),
58+
),
59+
const SizedBox(
60+
height: 10,
61+
),
62+
Text('Scan Barcode Result: $result'),
63+
const SizedBox(
64+
height: 10,
65+
),
66+
ElevatedButton(
67+
onPressed: () async {
68+
SimpleBarcodeScanner.streamBarcode(
69+
context,
70+
barcodeAppBar: const BarcodeAppBar(
71+
appBarTitle: 'Test',
72+
centerTitle: false,
73+
enableBackButton: true,
74+
backButtonIcon: Icon(Icons.arrow_back_ios),
75+
),
76+
isShowFlashIcon: true,
77+
delayMillis: 2000,
78+
).listen((event) {
79+
print("Stream Barcode Result: $event");
80+
});
81+
},
82+
child: const Text('Stream Barcode'),
7583
),
76-
Text('Barcode Result: $result'),
7784
],
7885
),
7986
),

ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ class BarcodeScannerViewController: UIViewController {
469469
@IBAction private func cancelButtonClicked() {
470470
if SwiftFlutterBarcodeScannerPlugin.isContinuousScan{
471471
self.dismiss(animated: true, completion: {
472-
SwiftFlutterBarcodeScannerPlugin.onBarcodeScanReceiver(barcode: "-1")
472+
SwiftFlutterBarcodeScannerPlugin.onBarcodeScanReceiver(barcode: "-2")
473473
})
474474
}else{
475475
if self.delegate != nil {

lib/constant.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ class PackageConstant {
66
}
77

88
String kScanPageTitle = 'Scan barcode/qrcode';
9+
String kCancelValue = '-2';

lib/screens/io_device.dart

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:simple_barcode_scanner/enum.dart';
55
import 'package:simple_barcode_scanner/screens/window.dart';
66

77
import '../barcode_appbar.dart';
8+
import '../constant.dart';
89
import '../flutter_barcode_scanner.dart';
910

1011
/// Barcode scanner for mobile and desktop devices
@@ -19,20 +20,20 @@ class BarcodeScanner extends StatelessWidget {
1920
final Widget? child;
2021
final BarcodeAppBar? barcodeAppBar;
2122
final int? delayMillis;
22-
23-
const BarcodeScanner({
24-
super.key,
25-
required this.lineColor,
26-
required this.cancelButtonText,
27-
required this.isShowFlashIcon,
28-
required this.scanType,
29-
required this.onScanned,
30-
this.child,
31-
this.appBarTitle,
32-
this.centerTitle,
33-
this.barcodeAppBar,
34-
this.delayMillis,
35-
});
23+
final Function? onClose;
24+
const BarcodeScanner(
25+
{super.key,
26+
required this.lineColor,
27+
required this.cancelButtonText,
28+
required this.isShowFlashIcon,
29+
required this.scanType,
30+
required this.onScanned,
31+
this.child,
32+
this.appBarTitle,
33+
this.centerTitle,
34+
this.barcodeAppBar,
35+
this.delayMillis,
36+
this.onClose});
3637

3738
@override
3839
Widget build(BuildContext context) {
@@ -50,7 +51,24 @@ class BarcodeScanner extends StatelessWidget {
5051
);
5152
} else {
5253
/// Scan Android and ios barcode scanner with flutter_barcode_scanner
53-
_scanBarcodeForMobileAndTabDevices();
54+
/// If onClose is not null then stream barcode otherwise scan barcode
55+
/// Scan barcode for mobile devices
56+
ScanMode scanMode;
57+
switch (scanType) {
58+
case ScanType.barcode:
59+
scanMode = ScanMode.BARCODE;
60+
break;
61+
case ScanType.qr:
62+
scanMode = ScanMode.QR;
63+
break;
64+
default:
65+
scanMode = ScanMode.DEFAULT;
66+
break;
67+
}
68+
onClose != null
69+
? _streamBarcodeForMobileAndTabDevices(scanMode)
70+
: _scanBarcodeForMobileAndTabDevices(scanMode);
71+
5472
return const Scaffold(
5573
body: Center(
5674
child: CircularProgressIndicator(),
@@ -59,22 +77,19 @@ class BarcodeScanner extends StatelessWidget {
5977
}
6078
}
6179

62-
_scanBarcodeForMobileAndTabDevices() async {
63-
/// Scan barcode for mobile devices
64-
ScanMode scanMode;
65-
switch (scanType) {
66-
case ScanType.barcode:
67-
scanMode = ScanMode.BARCODE;
68-
break;
69-
case ScanType.qr:
70-
scanMode = ScanMode.QR;
71-
break;
72-
default:
73-
scanMode = ScanMode.DEFAULT;
74-
break;
75-
}
80+
_scanBarcodeForMobileAndTabDevices(ScanMode scanMode) async {
7681
String barcode = await FlutterBarcodeScanner.scanBarcode(
7782
lineColor, cancelButtonText, isShowFlashIcon, scanMode, delayMillis);
7883
onScanned(barcode);
7984
}
85+
86+
void _streamBarcodeForMobileAndTabDevices(ScanMode scanMode) {
87+
FlutterBarcodeScanner.getBarcodeStreamReceiver(
88+
lineColor, cancelButtonText, isShowFlashIcon, scanMode, delayMillis)
89+
?.listen((barcode) {
90+
if (barcode != null) {
91+
barcode == kCancelValue ? onClose?.call() : onScanned(barcode);
92+
}
93+
});
94+
}
8095
}

lib/screens/unsupported.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class BarcodeScanner extends StatelessWidget {
1313
final Widget? child;
1414
final BarcodeAppBar? barcodeAppBar;
1515
final int? delayMillis;
16+
final Function? onClose;
1617
const BarcodeScanner(
1718
{super.key,
1819
this.lineColor = "#ff6666",
@@ -24,7 +25,8 @@ class BarcodeScanner extends StatelessWidget {
2425
this.child,
2526
this.centerTitle,
2627
this.barcodeAppBar,
27-
this.delayMillis});
28+
this.delayMillis,
29+
this.onClose});
2830

2931
@override
3032
Widget build(BuildContext context) {

lib/screens/web.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class BarcodeScanner extends StatelessWidget {
2020
final Widget? child;
2121
final BarcodeAppBar? barcodeAppBar;
2222
final int? delayMillis;
23+
final Function? onClose;
2324

2425
const BarcodeScanner({
2526
super.key,
@@ -33,6 +34,7 @@ class BarcodeScanner extends StatelessWidget {
3334
this.child,
3435
this.barcodeAppBar,
3536
this.delayMillis,
37+
this.onClose,
3638
});
3739

3840
@override

lib/screens/window.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WindowBarcodeScanner extends StatelessWidget {
2020
final bool? centerTitle;
2121
final BarcodeAppBar? barcodeAppBar;
2222
final int? delayMillis;
23+
final Function? onClose;
2324

2425
const WindowBarcodeScanner({
2526
super.key,
@@ -32,6 +33,7 @@ class WindowBarcodeScanner extends StatelessWidget {
3233
this.centerTitle,
3334
this.barcodeAppBar,
3435
this.delayMillis,
36+
this.onClose,
3537
});
3638

3739
@override

0 commit comments

Comments
 (0)