flutter_barcode_scanner.dart
2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import 'dart:async';
import 'package:flutter/services.dart';
/// Scan mode which is either QR code or BARCODE
enum ScanMode { QR, BARCODE, DEFAULT }
/// Provides access to the barcode scanner.
///
/// This class is an interface between the native Android and iOS classes and a
/// Flutter project.
class FlutterBarcodeScanner {
static const MethodChannel _channel =
MethodChannel('flutter_barcode_scanner');
static const EventChannel _eventChannel =
EventChannel('flutter_barcode_scanner_receiver');
static Stream? _onBarcodeReceiver;
/// Scan with the camera until a barcode is identified, then return.
///
/// Shows a scan line with [lineColor] over a scan window. A flash icon is
/// displayed if [isShowFlashIcon] is true. The text of the cancel button can
/// be customized with the [cancelButtonText] string.
static Future<String> scanBarcode(String lineColor, String cancelButtonText,
bool isShowFlashIcon, ScanMode scanMode) async {
if (cancelButtonText.isEmpty) {
cancelButtonText = 'Cancel';
}
// Pass params to the plugin
Map params = <String, dynamic>{
'lineColor': lineColor,
'cancelButtonText': cancelButtonText,
'isShowFlashIcon': isShowFlashIcon,
'isContinuousScan': false,
'scanMode': scanMode.index
};
/// Get barcode scan result
final barcodeResult =
await _channel.invokeMethod('scanBarcode', params) ?? '';
return barcodeResult;
}
/// Returns a continuous stream of barcode scans until the user cancels the
/// operation.
///
/// Shows a scan line with [lineColor] over a scan window. A flash icon is
/// displayed if [isShowFlashIcon] is true. The text of the cancel button can
/// be customized with the [cancelButtonText] string. Returns a stream of
/// detected barcode strings.
static Stream? getBarcodeStreamReceiver(String lineColor,
String cancelButtonText, bool isShowFlashIcon, ScanMode scanMode) {
if (cancelButtonText.isEmpty) {
cancelButtonText = 'Cancel';
}
// Pass params to the plugin
Map params = <String, dynamic>{
'lineColor': lineColor,
'cancelButtonText': cancelButtonText,
'isShowFlashIcon': isShowFlashIcon,
'isContinuousScan': true,
'scanMode': scanMode.index
};
// Invoke method to open camera, and then create an event channel which will
// return a stream
_channel.invokeMethod('scanBarcode', params);
_onBarcodeReceiver ??= _eventChannel.receiveBroadcastStream();
return _onBarcodeReceiver;
}
}