urovo_scanner_plus 0.0.1
urovo_scanner_plus: ^0.0.1 copied to clipboard
A Flutter plugin for Urovo barcode scanner Android
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:urovo_scanner_plus/urovo_scanner_plus.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _initScannerResult = 'Not initialized';
String _openScannerResult = 'Not opened';
String _stopScannerResult = 'Not stopped';
final _urovoScannerPlusPlugin = UrovoScannerPlus();
@override
void initState() {
super.initState();
initPlatformState();
listenToScanner();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _urovoScannerPlusPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _initScanner() async {
String result;
try {
result =
await _urovoScannerPlusPlugin.initScanner() ??
'Failed to initialize scanner';
} on PlatformException {
result = 'Error initializing scanner';
}
if (!mounted) return;
setState(() {
_initScannerResult = result;
});
}
Future<void> _openScanner() async {
String result;
try {
result =
await _urovoScannerPlusPlugin.openScanner() ??
'Failed to open scanner';
} on PlatformException {
result = 'Error opening scanner';
}
if (!mounted) return;
setState(() {
_openScannerResult = result;
});
}
Future<void> _stopScanner() async {
String result;
try {
result =
await _urovoScannerPlusPlugin.stopScanner() ??
'Failed to stop scanner';
} on PlatformException {
result = 'Error stopping scanner';
}
if (!mounted) return;
setState(() {
_stopScannerResult = result;
});
}
void listenToScanner() {
const eventChannel = EventChannel('urovo_scanner_plus/event');
eventChannel.receiveBroadcastStream().listen(
(dynamic data) {
print('scan result: $data');
// data 是 String 类型(根据 Kotlin 中发送的内容)
},
onError: (Object error) {
print('scan error: $error');
},
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
Text('Scanner status: $_initScannerResult\n'),
Text('Open result: $_openScannerResult\n'),
Text('Stop result: $_stopScannerResult\n'),
ElevatedButton(
onPressed: _initScanner,
child: const Text('Initialize Scanner'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _openScanner,
child: const Text('Open Scanner'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _stopScanner,
child: const Text('Stop Scanner'),
),
],
),
),
),
);
}
}