Flutter General example
Prepare environment #
Install Flutter SDK and set up your environment. A new app: flutter create my_app, then cd my_app (the steps below are in that folder).
Add our flutter package #
To add the barkoder_flutter package to your project, you have two options. You can either use the barkoder_flutter package from pub.dev or from a local path.
To use the package from pub.dev, add the package name and version to the dependencies in your project's pubspec.yaml (or run flutter pub add barkoder_flutter, which adds the latest version):
dependencies:
flutter:
sdk: flutter
barkoder_flutter: <package version>
If you prefer to use a local package, download the package from https://barkoder.com and set the package path in your project's dependencies:
dependencies:
flutter:
sdk: flutter
barkoder_flutter:
path: <package local path>
Install dependencies #
Run flutter pub get command in terminal to install the specified dependencies in your project
With barkoder_flutter 1.7.2, the Android build of a new app (Flutter 3.47) stops at checkDebugAarMetadata (":barkoder_flutter is currently compiled against android-31"): the plugin compiles against Android API 31, and the AndroidX libraries a Flutter app uses need 34 or later. Add these lines at the top of android/build.gradle.kts; they compile the plugin against API 36, as the app is:
subprojects {
afterEvaluate {
if (name == "barkoder_flutter") {
extensions.configure<com.android.build.api.dsl.LibraryExtension>("android") {
compileSdk = 36
}
}
}
}
Creating the BarcodeScanner Widget #
Create a new widget lib/barcode_scanner.dart and add the following code:
import 'package:flutter/material.dart';
import 'package:barkoder_flutter/barkoder_flutter.dart';
class BarcodeScanner extends StatefulWidget {
@override
_BarcodeScannerState createState() => _BarcodeScannerState();
}
class _BarcodeScannerState extends State<BarcodeScanner> {
Barkoder? _barkoder;
String? scannedResult;
bool isScanning = false;
void _onBarkoderViewCreated(Barkoder barkoder) {
_barkoder = barkoder;
_barkoder!.setRegionOfInterestVisible(true);
_barkoder!.setRegionOfInterest(5, 5, 90, 90);
_barkoder!.setCloseSessionOnResultEnabled(false);
_barkoder!.setImageResultEnabled(true);
_barkoder!.setBarcodeThumbnailOnResultEnabled(true);
_barkoder!.setBeepOnSuccessEnabled(true);
_barkoder!.setPinchToZoomEnabled(true);
_barkoder!.setZoomFactor(2.0);
// Enable the barcode types and add others if you need
_barkoder!.setBarcodeTypeEnabled(BarcodeType.code128, true);
_barkoder!.setBarcodeTypeEnabled(BarcodeType.code39, true);
_barkoder!.setBarcodeTypeEnabled(BarcodeType.ean13, true);
}
void _startScanning() {
setState(() {
scannedResult = null;
isScanning = true;
});
_barkoder!.startScanning((result) {
setState(() {
scannedResult = result.decoderResults[0].textualData;
isScanning = false;
});
});
}
void _stopScanning() {
_barkoder!.stopScanning();
setState(() {
isScanning = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Barcode Scanner')),
body: Column(
children: [
Expanded(
child: BarkoderView(
licenseKey: 'ADD_YOUR_LICENSE_KEY_HERE',
onBarkoderViewCreated: _onBarkoderViewCreated,
),
),
if (scannedResult != null)
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text('Result: $scannedResult'),
// Here you can add more details like barcode type, or display the thumbnail
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: isScanning ? null : _startScanning,
child: Text('Start Scanning'),
),
ElevatedButton(
onPressed: isScanning ? _stopScanning : null,
child: Text('Stop Scanning'),
),
],
),
),
],
),
);
}
}
Adding to Your Main App #
Make sure to add this widget to your app’s navigation or replace the main screen with it. Below is an example of how you might include this in a simple Flutter app:
import 'package:flutter/material.dart';
import 'barcode_scanner.dart'; // Adjust the path as necessary
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Barcode Scanner',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BarcodeScanner(),
);
}
}
Run the App #
Run your Flutter app using flutter run
Minimum SDK #
The plugin needs Android 5.0 (API 21) or later. A new Flutter project already sets a higher minimum (minSdk = flutter.minSdkVersion, 24 in Flutter 3.47). If your project sets it to 20 or lower, raise it in android/app/build.gradle.kts:
android {
defaultConfig {
minSdk = 21
}
}