Skip to content

Latest commit

 

History

History
125 lines (108 loc) · 3.52 KB

README.md

File metadata and controls

125 lines (108 loc) · 3.52 KB

flutter_scan_view

A scan code view that can be embedded in the Widget Tree on iOS and Android..

Installation

First, add flutter_scan_view as a dependency in your pubspec.yaml file.

iOS

Swift version:s.swift_version = '5'

Add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

  • NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.

Android

On Android 6 or higher, you need to request camera permissions first. you can use permission_handler to get permission.

Usage

First import the file

import 'package:flutter_scan_view/flutter_scan_view.dart';

Define a Controller

FlutterScanViewController _controller;

Add FlutterScanView to your Widget tree, and save the controller

FlutterScanView(
                width: 283,
                height: 120,
                onCreated: (controller) {
                  _controller = controller;
                },
              ),

You can call _controller.startScan() to start scanning and get results

 _controller.startScan().then((text) {
                          print("Scan Result:${text}");
                        }).catchError((_) {});

Example

import 'package:flutter/material.dart';
import 'package:flutter_scan_view/flutter_scan_view.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FlutterScanViewController _controller;
  String _code = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Container(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              FlutterScanView(
                width: 283,
                height: 120,
                onCreated: (controller) {
                  _controller = controller;
                },
              ),
              Padding(
                padding: const EdgeInsets.only(top: 30),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CupertinoButton(
                      child: Text('Start Scan'),
                      onPressed: () {
                        _controller.startScan().then((text) {
                          print("Scan Result:${_code ?? ""}");
                          setState(() {
                            _code = text;
                          });
                        }).catchError((_) {});
                      }),
                  CupertinoButton(
                      child: Text('Stop Scan'),
                      onPressed: () {
                        _controller.stopScan();
                      }),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(top: 30),
              ),
              Text("Scan Result:${_code ?? ""}")
            ],
          ))),
    );
  }
}