Quick start | Flutter Barcode Scanner
In this section, you'll learn how to set up the Scanbot Flutter Barcode Scanner SDK for Flutter in your app, with Barcode Scanning enabled, all within 10 minutes – thanks to our Ready-to-Use UI!
Installing Scanbot Barcode Scanner SDK package
To add and install the Scanbot Barcode Scanner SDK package, use the following command in your terminal or through your IDE:
$ flutter pub add barcode_scanner
To get the latest version of the Scanbot SDK, please always refer to the SDK's changelog.
Configure native projects
The Scanbot Barcode Scanner SDK needs access to the device camera, so it can scan from a live camera stream. Therefore, the camera permission must be defined.
Android
For Android, we need to add the camera permissions in android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Note how we also added the uses-feature
tag for better recognition of your app on the Google Play Store (see more).
Our Ready-to-Use UI Components handle the runtime permissions automatically, so there is no need to add anything else in the code.
iOS
For iOS, we need to include a description for the camera permission in ios/{projectName}/Info.plist anywhere inside the <dict>
element:
<key>NSCameraUsageDescription</key>
<string>Camera permission is needed to scan barcodes</string>
Initialize the SDK
Before using any feature of Flutter Scanbot Barcode SDK, we need to initialize it. Ideally, initialization should be done as soon as the app is launched, before any of the Scanbot Barcode Scanner SDK features are used.
Make sure to add the following import to the top of the file:
import 'package:barcode_scanner/scanbot_barcode_sdk.dart';
To initialize the SDK, simply use the initializeSdk
function
var config = ScanbotSdkConfig(
licenseKey: "<YOUR_SCANBOT_SDK_LICENSE_KEY>",
loggingEnabled: true,
);
ScanbotBarcodeSdk.initScanbotSdk(config);
You can use the Scanbot Flutter Barcode Scanner SDK for quick testing or evaluation purposes even without a license key. However, the SDK will only work for 60 seconds per app session and may not be used for production purposes. Want to scan longer than 60 seconds? Get your free trial license key here.
Start the Barcode Scanner and process the result
Our RTU UI components make it easy to deploy our Barcode Scanner SDK’s different scanning modes in your app.
To start our single scanning mode within a component, we are going to add our function:
import 'package:barcode_scanner/scanbot_barcode_sdk.dart';
void _startBarcodeScanning() async {
try {
// Create the default configuration object.
var configuration = BarcodeScannerConfiguration();
// Initialize the use case for single scanning.
var scanningMode = SingleScanningMode();
// Enable and configure the confirmation sheet.
scanningMode.confirmationSheetEnabled = true;
scanningMode.sheetColor = ScanbotColor("#FFFFFF");
// Hide/unhide the barcode image.
scanningMode.barcodeImageVisible = true;
// Configure the barcode title of the confirmation sheet.
scanningMode.barcodeTitle.visible = true;
scanningMode.barcodeTitle.color = ScanbotColor("#000000");
// Configure the barcode subtitle of the confirmation sheet.
scanningMode.barcodeSubtitle.visible = true;
scanningMode.barcodeSubtitle.color = ScanbotColor("#000000");
// Configure the cancel button of the confirmation sheet.
scanningMode.cancelButton.text = "Close";
scanningMode.cancelButton.foreground.color = ScanbotColor("#C8193C");
scanningMode.cancelButton.background.fillColor = ScanbotColor("#00000000");
// Configure the submit button of the confirmation sheet.
scanningMode.submitButton.text = "Submit";
scanningMode.submitButton.foreground.color = ScanbotColor("#FFFFFF");
scanningMode.submitButton.background.fillColor = ScanbotColor("#C8193C");
// Configure other parameters, pertaining to single-scanning mode as needed.
configuration.useCase = scanningMode;
// Set an array of accepted barcode types.
// configuration.recognizerConfiguration.barcodeFormats = [
// BarcodeFormat.AZTEC,
// BarcodeFormat.PDF_417,
// BarcodeFormat.QR_CODE,
// BarcodeFormat.MICRO_QR_CODE,
// BarcodeFormat.MICRO_PDF_417,
// BarcodeFormat.ROYAL_MAIL,
/// .....
// ];
// Configure other parameters as needed.
var result = await ScanbotBarcodeSdkUiV2.startBarcodeScanner(configuration);
if(result.status == OperationStatus.OK)
{
// TODO: present barcode result as needed
print(result.value?.items.first.type?.name);
}
} catch (e) {
print('Error: $e');
}
}
Now, to launch the Barcode Scanner UI, you just call the _startBarcodeScanning
where needed.
For example, on onPressed
event of a button:
ElevatedButton(
onPressed: _startBarcodeScanning,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 40),
textStyle: const TextStyle(fontSize: 18),
),
child: const Text("Start single-barcode scanning"),
),
🚀 That's it! 🚀 You have successfully integrated a full-featured barcode scanner as an RTU UI component into your app.
💡 Customization: In this Quick Start guide, we use the default configuration for the scanner UI. Feel free to explore the configs and customize the UI and behavior according to your needs via the `BarcodeScannerConfiguration` class. For more details, please refer to the Ready-to-Use UI page.
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get your free Trial LicenseWhat do you think of this documentation?
What can we do to improve it? Please be as detailed as you like.