barkoder_flutter 1.5.3 barkoder_flutter: ^1.5.3 copied to clipboard
Flutter support for Barkoder - a Barcode Scanner SDK for the modern enterprise.
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:barkoder_flutter/barkoder_flutter.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
late Barkoder _barkoder;
bool _isScanningActive = false;
String _barkoderVersion = '';
List<String> _resultValues = [];
List<String> _typeValues = [];
List<String> _extrasValues = [];
Uint8List? _resultImage;
List<Uint8List?> _resultThumbnailImages = [];
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
_barkoder.stopScanning();
_updateState(null, false);
}
}
void _onBarkoderViewCreated(barkoder) async {
_barkoder = barkoder;
String barkoderVersion = await _barkoder.getVersion;
_setActiveBarcodeTypes();
_setBarkoderSettings();
// _barkoder.configureBarkoder(BarkoderConfig(
// imageResultEnabled: true,
// decoder: DekoderConfig(qr: BarcodeConfig(enabled: true)),
// ));
if (!mounted) return;
setState(() {
_barkoderVersion = barkoderVersion;
});
}
void _setActiveBarcodeTypes() {
_barkoder.setBarcodeTypeEnabled(BarcodeType.qr, true);
_barkoder.setBarcodeTypeEnabled(BarcodeType.ean13, true);
_barkoder.setBarcodeTypeEnabled(BarcodeType.upcA, true);
_barkoder.setBarcodeTypeEnabled(BarcodeType.code128, true);
}
void _setBarkoderSettings() {
_barkoder.setImageResultEnabled(true);
_barkoder.setLocationInImageResultEnabled(true);
_barkoder.setMaximumResultsCount(200);
_barkoder.setRegionOfInterestVisible(true);
_barkoder.setPinchToZoomEnabled(true);
_barkoder.setBarcodeThumbnailOnResultEnabled(true);
_barkoder.setRegionOfInterest(5, 5, 90, 90);
// When using image scan it is recommended to use rigorous decoding speed
// _barkoder.setDecodingSpeed(DecodingSpeed.rigorous);
}
void _updateState(BarkoderResult? result, bool scanningIsActive) {
if (!mounted) return;
setState(() {
_isScanningActive = scanningIsActive;
if (result != null) {
// Append new values to the lists
_resultValues.add(result.textualData);
_typeValues.add(result.barcodeTypeName);
_extrasValues.add(result.extra?.toString() ?? '');
if (result.resultImageAsBase64 != null) {
_resultImage =
const Base64Decoder().convert(result.resultImageAsBase64!);
} else {
_resultImage = null;
}
// Clear previous thumbnails
_resultThumbnailImages.clear();
// Add the new thumbnails
if (result.resultThumbnailsAsBase64 != null) {
for (var thumbnailBase64 in result.resultThumbnailsAsBase64!) {
final newThumbnail = const Base64Decoder().convert(thumbnailBase64);
_resultThumbnailImages.add(newThumbnail);
}
}
} else {
// Clear all values if no result
_resultValues.clear();
_typeValues.clear();
_extrasValues.clear();
_resultImage = null;
_resultThumbnailImages.clear();
}
});
}
void _scanPressed() {
if (_isScanningActive) {
_barkoder.stopScanning();
} else {
_barkoder.startScanning((result) {
_updateState(result, false);
});
}
_updateState(null, !_isScanningActive);
}
void _scanImage() async {
final ImagePicker _picker = ImagePicker();
final XFile? pickedFile =
await _picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
// Read the file and convert it to base64
final bytes = await pickedFile.readAsBytes();
String base64Image = base64Encode(bytes);
// Call scanImage with the base64 string and the callback
_barkoder.scanImage((result) {
_updateState(result, false);
}, base64Image);
}
_updateState(null, !_isScanningActive);
}
void _showFullResult() {
if (_resultValues.isNotEmpty) {
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Result'),
content: Text(_resultValues.join('\n')),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: AppBar(
backgroundColor: const Color(0xFFFF0022),
title: Text('Barkoder Sample (v$_barkoderVersion)'),
),
body: Column(
children: [
Expanded(
child: Stack(
children: <Widget>[
const Align(
alignment: Alignment.center,
child: Text('Press the button to start scanning',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey, fontSize: 20))),
if (_resultImage != null)
Align(
alignment: Alignment.center,
child: Image.memory(_resultImage!)),
BarkoderView(
licenseKey: '',
onBarkoderViewCreated: _onBarkoderViewCreated),
],
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
GestureDetector(
onTap: _showFullResult,
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(5.0),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Column(children: [
const Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(
"Result",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold),
),
),
// Display all results
Text(
_resultValues.join('\n'),
maxLines: 10,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
]),
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Container(
padding: const EdgeInsets.all(5.0),
margin: const EdgeInsets.only(right: 5.0),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(10))),
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(
"Type",
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold),
),
),
// Display all types
Text(_typeValues.join('\n')),
],
),
),
),
Expanded(
child: Container(
padding: const EdgeInsets.all(5.0),
margin: const EdgeInsets.only(left: 5.0),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(10))),
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(
"Extras",
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold),
),
),
// Display all extras
Text(_extrasValues.join('\n')),
],
),
),
),
],
),
),
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: _resultThumbnailImages
.where((thumbnail) => thumbnail != null)
.map((thumbnail) => Padding(
padding: const EdgeInsets.all(4.0),
child: Image.memory(
thumbnail!,
height: 130,
width: 130,
),
))
.toList(),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: _scanImage,
child: const Text('Scan Image'),
),
),
],
),
),
),
],
),
floatingActionButton: FloatingActionButton(
tooltip: _isScanningActive ? 'Stop scan' : 'Start scan',
onPressed: _scanPressed,
backgroundColor: _isScanningActive ? Colors.red : Colors.white,
child: const Icon(Icons.qr_code_scanner),
),
);
}
}