General Example

Prepare environment #

Install Flutter SDK and setup your environment

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 your project's dependencies:

                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

Creating the BarcodeScanner Widget #

Create a new widget lib/barkoder_flutter.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.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 requires a minimum SDK version of 21, but your Flutter project's Android SDK version is set to 20 or lower, you'll need to update the minSdkVersion in your build.gradle file to at least 21:

                android {
	compileSdkVersion 33

	defaultConfig {
	    applicationId "com.example.my_flutter_app"
	    minSdkVersion 21  // Updated from 19 to 21
	    targetSdkVersion 33
	    versionCode 1
	    versionName "1.0"
	
	    multiDexEnabled true
	}
	
	...
}
            

Page Contents

History:

close