qr_code_tools 0.2.0 copy "qr_code_tools: ^0.2.0" to clipboard
qr_code_tools: ^0.2.0 copied to clipboard

The Flutter plugin for iOS and Android to decoding QR codes.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_code_tools/qr_code_tools.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final picker = ImagePicker();
  String _qrcodeFile = '';
  String _data = '';

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

    String filename = '1559788943.png';
    getTemporaryDirectory()
        .then((dir) => File('${dir.path}/$filename'))
        .then((file) => file.existsSync()
            ? Future.value(file)
            : rootBundle
                .load("images/$filename")
                .then((bytes) => file.writeAsBytes(bytes.buffer.asUint8List())))
        .then((file) {
          setState(() {
            _qrcodeFile = file.path;
          });
          return file;
        })
        .then((file) => decodeQR(file.path))
        .then((data) {
          setState(() {
            _data = data ?? 'Decode is failed';
          });
        })
        .catchError((e) {
          setState(() {
            _data = 'Failed to load this file';
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              _qrcodeFile.isEmpty
                  ? Image.asset(
                      'images/1559788943.png',
                    )
                  : Image.file(File(_qrcodeFile)),
              ElevatedButton(
                onPressed: _getPhotoByGallery,
                child: const Text("Select file"),
              ),
              Text('Qr Code data: $_data\n'),
            ],
          ),
        ),
      ),
    );
  }

  void _getPhotoByGallery() {
    picker
        .pickImage(source: ImageSource.gallery)
        .then((xfile) => xfile?.path)
        .then((path) {
      if (path != null) {
        setState(() {
          _qrcodeFile = path;
        });
        decodeQR(path).then((data) => setState(() => _data = data ?? 'Decode is failed'));
      } else {
        setState(() {
          _data = 'Failed to load this file';
        });
      }
    });
  }

  Future<String?> decodeQR(String filePath) {
    return QrCodeToolsPlugin.decodeFrom(filePath);
  }
}
28
likes
160
points
1.2k
downloads

Publisher

verified publisheraifeii.com

Weekly Downloads

The Flutter plugin for iOS and Android to decoding QR codes.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on qr_code_tools