scan_plugin 0.0.2 scan_plugin: ^0.0.2 copied to clipboard
A new Flutter project.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:scan/scan.dart';
void main() => runApp(const MyApp());
// 发布使用
// flutter packages pub publish --server=https://pub.dartlang.org
// 即可
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription? _streamSubscription;
String? _appTestResult;
static const EventChannel _eventChannel =
EventChannel('com.example.scan/event_channel_event');
void _enableEventReceiver() {
// 这里的 data 就是原生端发送过来的数据
_streamSubscription =
_eventChannel.receiveBroadcastStream().listen((dynamic event) {
setState(() {
_appTestResult = event;
});
}, onError: (dynamic error) {}, cancelOnError: true);
}
void _disableEventReceiver() {
if (_streamSubscription != null) {
_streamSubscription?.cancel();
_streamSubscription = null;
}
}
@override
void initState() {
super.initState();
getResult();
_enableEventReceiver();
}
@override
void dispose() {
super.dispose();
_disableEventReceiver();
}
Future<void> getResult() async {
String appTestResult;
appTestResult = await Scan.getResult;
setState(() {
_appTestResult = appTestResult;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
//child: Text('Running on: $_platformVersion\n'),
child: Text('Result = $_appTestResult\n'),
),
),
);
}
}