# Generate qr code

## Need to be added in pubspec.yaml: first

Qr_flutter: ^ 1.1.3

Second, introduce code:

import 'package:qr_flutter/qr_flutter.dart';

The core code is as follows:

        child: QrImage(
          data: "Here's the data to generate the QR code.",
          size: 100.0,
          onError: (ex) {
            print("[QR] ERROR - $ex");
          },
Copy the code

The overall code and effect are as follows:

# Identify qr code

Here, too, a third-party package is used to add library links to pubspec.yaml;

Barcode_scan: ^ 0.0.4

The main codes identified are as follows:

  Future scan() async {
    try {
      String barcode = await BarcodeScanner.scan();
      setState(() {
        return this.barcode = barcode;
      });
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          return this.barcode = 'The user did not grant the camera permission! ';
        });
      } else {
        setState(() {
          return this.barcode = 'Unknown error: $e';
        });
      }
    } on FormatException{
      setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e'); }}Copy the code

The overall code is as follows:

import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'dart:async';
import 'package:flutter/services.dart';

class scanqr extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: sacnBody(),
    );
  }
}

class sacnBody extends StatefulWidget {
@override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyScanState();
  }
}

class _MyScanState extends State<sacnBody> {
  String barcode = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: new AppBar(
        title: new Text('QR Code'), ), body: new Center( child: new Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment. Stretch, children: < widgets > [Padding (Padding: EdgeInsets symmetric (horizontal: 15.0, vertical: 8.0), child: RaisedButton(color: colors.orange, textColor: colors.white, splashColor: colors.blueGrey, onPressed: scan, child: const Text('START CAMERA SCAN'Symmetric (Horizontal: 16.0, vertical: 8.0), Child: Text(barcode, textAlign: barcode), symmetric(horizontal: 16.0, vertical: 8.0), symmetric(horizontal: 16.0, vertical: 8.0), TextAlign.center,), ) ], ), ), ); } Future scan() async { try { String barcode = await BarcodeScanner.scan();setState(() {
        return this.barcode = barcode;
      });
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          return this.barcode = 'The user did not grant the camera permission! ';
        });
      } else {
        setState(() {
          return this.barcode = 'Unknown error: $e';
        });
      }
    } on FormatException{
      setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e'); }}}Copy the code

Android and iOS engineering Settings are as follows:

Android For Android, you must do the following before you can use the plugin:

Add the camera permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />

Add the Barcode activity to your AndroidManifest.xml

<activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>
Copy the code

iOS To use on iOS, you must add the the camera usage description to your Info.plist

<key>NSCameraUsageDescription</key>
<string>Camera permission is required for barcode scanning.</string>
Copy the code