Flutter apps are really good at performance and using Flutter we can build highly scaled apps. Flutter is found to be really sensational at various native features and services. Camera access and scanning are those native features that most of the highly scaled application needs. Barcodes and QR Codes are the official codes for product recognition and are used in various industrial aspects as well. So, working with barcodes and QR codes can be really handy for the management of data by means of unique codes.
Flutter framework helps us working with codes as it is having a package called barcode_scan. You are just required to add this package to your project by adding below line to your pubspec.yaml file.
After that you should type the command below in your terminal:
flutter pub get
Now, create a new file home.dart in the lib folder of the project and write the below code inside that file.
Now, we are required to create a new file scan.dart inside the lib folder and create a StatefulWidget named ScanScreen like given below:
The build() of the _ScanScreen() class will be defined like below:
In the above build method() for StatefulWidget, we have defined a RaisedButton() widget in the center and the UI, we are going to get will be:
In the above code, we have referred the scan method in the onPressed() method of the RaisedButton() which will be defined like:
In the above scan() method, we have called the scan method from the BarcodeScanner class which is defined in the barcode_scan package. After calling that method, we are storing the value of scanned code to a variable and using setState() method of StatefulWidget, we are initialising that variable. And accordingly the error handling is done.
At last after all thecode in place, we will be seeing the camera screen after clicking the button:
This will automatically detect the code and we will be getting the output in the screen.
This is an example of a QR Code or Barcode scanning and this can be really useful as a part of any big application. I feel that this article will be really helpful for the readers. So, if you find this article useful, do clap as much as you can.