PDA 扫描监听插件

效果

ezgif.com-video-to-gif.gif

使用

Installation

Add this to your package's pubspec.yaml file:

dependencies:
 pda_scanner: ^0.0.1

Usage example

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

import 'package:flutter/services.dart';

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

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

class _MyAppState extends State {
  static const scannerPlugin =
  const EventChannel('com.shinow.pda_scanner/plugin');

  StreamSubscription _subscription;

  var _code;

  @override
  void initState() {
    super.initState();
    /// 开启监听
    if (_subscription == null) {
      _subscription = scannerPlugin
          .receiveBroadcastStream()
          .listen(_onEvent, onError: _onError);
    }
  }

  @override
  void dispose() {
    super.dispose();
    /// 取消监听
    if (_subscription != null) {
      _subscription.cancel();
    }
  }

  void _onEvent(Object event) {
    if (!ModalRoute.of(context).isCurrent) return;
    setState(() {
      _code = event;
      print("ChannelPage: $event");
    });
  }

  void _onError(Object error) {
    setState(() {
      _code = "扫描异常";
      print(error);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Scanning result: $_code\n'),
        ),
      ),
    );
  }
}

你可能感兴趣的:(PDA 扫描监听插件)