fluttervisionsdkplugin 0.0.8 fluttervisionsdkplugin: ^0.0.8 copied to clipboard
A Flutter plugin package for integrating the Flutter Vision SDK into your Flutter application for scanning Barcodes and QrCodes
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:fluttervisionsdkplugin/fluttervisionsdkplugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
NativeViewWidgetCommunicator? communicator;
String scannedCode = '';
String scannedType = '';
String scanErrorMessage = '';
bool isManualCaptureMode = false;
bool isAutoScanning = true;
int scanMode = 1;
bool shouldDisplayFocusImage = false;
List<String> receivedCodes = [];
void _onScanError(String errorMessage) {
print('Scan Error: $errorMessage');
setState(() {
scanErrorMessage = errorMessage;
});
}
void updateReceivedCodes(List<String> codes) {
setState(() {
receivedCodes = codes;
});
}
void onDetected(String type) {
setState(() {
scannedType = type;
});
}
void _onCodesReceived(List<String> codes) {
updateReceivedCodes(codes);
if (codes.isNotEmpty) {
updateScannedCode(codes.first);
} else {
updateScannedCode('');
}
}
void _onDetectionResult(
bool textDetected, bool barCodeDetected, bool qrCodeDetected) {
print('Text Detected: $textDetected');
print('Barcode Detected: $barCodeDetected');
print('QR Code Detected: $qrCodeDetected');
}
void _onOCRImagesReceived(Map<String, dynamic> scanResult) {
print(scanResult);
}
void _changeCaptureMode(String mode) {
print('Changing Capture Mode to: $mode');
}
void _changeScanMode(String mode) {
print('Changing Scan Mode to: $mode');
}
void _capture() {
print('Capturing Photo');
}
void updateScannedType(String type) {
setState(() {
scannedType = type;
});
}
void updateCaptureMode(bool isManual) {
setState(() {
isManualCaptureMode = isManual;
});
}
void updateScannedCode(String code) {
setState(() {
scannedCode = code;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Stack(
children: [
NativeViewWidget(
onScanError: _onScanError,
onCodesReceived: _onCodesReceived,
onDetectionResult: _onDetectionResult,
onOCRImagesReceived: _onOCRImagesReceived,
apiKey:
'key_stag_00203c56428WHECda5Hfu53dFQj2gJrJDMmD14FYyfRmkewE5vE66aydykdDZYbnzauGcC9Pz3v2D3pK',
environment: Environment.qa,
onViewCreated: (communicator) =>
this.communicator = communicator,
listener: MainNativeViewWidgetListener(this),
shouldDisplayFocusImage: true,
shouldScanInFocusImageRect: true,
onDetected: onDetected,
isTextIndicationOn: true,
isBarCodeOrQRCodeIndicationOn: true,
selectionTintColor: 'white',
imageTintColor: 'white',
codeDetectionConfidence: 0.5,
sessionPreset: 'high',
nthFrameToProcess: 10,
captureMode: 0,
captureType: 0,
scanMode: 0,
focusImage: '',
focusImageRect: const {
'x': 0,
'y': 0,
'height': 0,
'width': 0,
}),
Positioned(
left: 0,
right: 0,
bottom: 0,
child: Container(
alignment: Alignment.bottomCenter,
width: double.infinity,
color: Colors.white,
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: [
Text(
'Detected Type: $scannedType',
style: const TextStyle(color: Colors.black, fontSize: 16),
),
Text(
'Scanned Code: $scannedCode',
style: const TextStyle(color: Colors.black, fontSize: 16),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Platform.isIOS
? ElevatedButton(
onPressed: () {
const NativeViewWidget()
.onPressChangeCaptureMode('auto');
},
child: const Text('Auto'),
)
: Expanded(
child: RadioListTile(
value: true,
title: const Text('Auto'),
groupValue: isAutoScanning,
onChanged: (i) {
setState(() {
isAutoScanning = true;
});
communicator?.setScanMode(1);
updateCaptureMode(false);
}),
),
Platform.isIOS
? ElevatedButton(
onPressed: () {
const NativeViewWidget()
.onPressChangeCaptureMode('manual');
updateCaptureMode(true);
},
child: const Text('Manual'),
)
: Expanded(
child: RadioListTile(
value: false,
title: const Text('Manual'),
groupValue: isAutoScanning,
onChanged: (i) {
setState(() {
isAutoScanning = false;
});
communicator?.setScanMode(2);
updateCaptureMode(true);
}),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Platform.isIOS
? ElevatedButton(
onPressed: () {
const NativeViewWidget()
.onPressChangeScanMode('barcode');
},
child: const Text('Barcode'),
)
: Expanded(
flex: 1,
child: RadioListTile(
value: 1,
title: const Text('Barcode'),
groupValue: scanMode,
onChanged: (i) {
setState(() {
scanMode = 1;
});
communicator?.setCaptureModeBarcode();
}),
),
Platform.isIOS
? ElevatedButton(
onPressed: () {
const NativeViewWidget()
.onPressChangeScanMode('qrcode');
},
child: const Text(' QR Code'),
)
: Expanded(
flex: 1,
child: RadioListTile(
value: 2,
title: const Text('Qrcode'),
groupValue: scanMode,
onChanged: (i) {
setState(() {
scanMode = 2;
});
communicator?.setCaptureModeQrCode();
}),
),
if (!isAutoScanning)
Expanded(
flex: 1,
child: RadioListTile(
value: 3,
title: const Text('OCR'),
groupValue: scanMode,
onChanged: (i) {
setState(() {
scanMode = 3;
});
communicator?.setCaptureModeOCR();
}),
),
Platform.isIOS
? ElevatedButton(
onPressed: () {
const NativeViewWidget()
.onPressChangeScanMode('ocr');
},
child: const Text('OCR'),
)
: Container(
height: 0.5,
width: 0.5,
)
],
),
if (isManualCaptureMode)
Platform.isIOS
? ElevatedButton(
onPressed: () {
{
// Capture photo in manual mode when barcode or QR code is detected
const NativeViewWidget().onPressCapture();
}
},
child: const Text('Capture Photo'),
)
: ElevatedButton(
onPressed: () {
communicator?.capturePhoto();
},
child: const Text('Capture Photo'),
),
],
),
),
),
],
),
),
);
}
}
class MainNativeViewWidgetListener implements NativeViewWidgetListener {
MainNativeViewWidgetListener(this._state);
final _MyAppState _state;
Timer? _timerTask;
@override
void onCodesReceived(List<String> codes) {
_state.updateScannedCode(codes.join(','));
_resetTextAfterDelay();
}
@override
void onDetectionResult(bool isText, bool isBarcode, bool isQrCode) {
var detectedType = '';
if (isText) {
detectedType = 'Text';
} else if (isBarcode) {
detectedType = 'Barcode';
} else if (isQrCode) {
detectedType = 'Qrcode';
} else {
detectedType = '';
}
_state.updateScannedType(detectedType);
}
@override
void onOCRResult(Map<String, dynamic> result) {
_state.updateScannedCode(result.toString());
_resetTextAfterDelay();
}
@override
void onScanError(String error) {
_state.updateScannedCode(error);
_resetTextAfterDelay();
}
void _resetTextAfterDelay() {
_timerTask?.cancel();
_timerTask = Timer(const Duration(seconds: 2), () {
_state.updateScannedCode('');
});
}
}