flutter_ysdk 0.1.0
flutter_ysdk: ^0.1.0 copied to clipboard
A Flutter plugin for integrating with MoreFun POS YSDK (Android). Supports Device Info, Printer, and NFC (Mifare/TypeA, NTAG, CPU).
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_ysdk/flutter_ysdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _ysdk = FlutterYsdk();
String _status = 'Not Connected';
String _deviceInfo = 'Unknown';
final _textController = TextEditingController(text: 'Hello from Flutter YSDK!');
@override
void initState() {
super.initState();
// Verify platform version
initPlatformState();
}
Future<void> initPlatformState() async {
String? version;
try {
version = await _ysdk.getPlatformVersion();
} catch (e) {
version = 'Failed to get platform version.';
}
if (!mounted) return;
print('Running on: $version');
}
Future<void> _bindService() async {
setState(() {
_status = 'Binding...';
});
try {
final success = await _ysdk.bind();
setState(() {
_status = success ? 'Connected' : 'Bind Failed';
});
} catch (e) {
setState(() {
_status = 'Error: $e';
});
}
}
Future<void> _getDeviceInfo() async {
if (_status != 'Connected') {
setState(() => _deviceInfo = 'Please connect first');
return;
}
try {
final info = await _ysdk.getDeviceInfo();
setState(() {
_deviceInfo = info.toString();
});
} catch (e) {
setState(() {
_deviceInfo = 'Error: $e';
});
}
}
Future<void> _printText() async {
if (_status != 'Connected') {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please connect first')),
);
return;
}
try {
await _ysdk.printText(_textController.text);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Print command sent')),
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Print error: $e')),
);
}
}
// NFC Status State
String _nfcMessage = 'Ready to Scan';
StreamSubscription? _nfcSubscription;
Future<void> _startNfcListener() async {
_nfcSubscription = _ysdk.nfcStatus.listen((status) {
setState(() {
_nfcMessage = status;
});
});
}
@override
void dispose() {
_nfcSubscription?.cancel();
_textController.dispose();
super.dispose();
}
Future<void> _runNfcScan(String mode) async {
if (_status != 'Connected') {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please connect first')),
);
return;
}
// Ensure listener is active
if (_nfcSubscription == null) _startNfcListener();
setState(() => _nfcMessage = 'Starting $mode scan...');
try {
String? result;
if (mode == 'CpuRF') {
result = await _ysdk.searchCpuRfCard();
} else if (mode == 'TypeA') {
result = await _ysdk.searchCpuTypeA();
} else if (mode == 'NTAG') {
result = await _ysdk.searchNtag();
}
// Result success handled by stream updates, but we can log unique ID here too if returned
print('Scan Result: $result');
} catch (e) {
setState(() => _nfcMessage = 'Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter YSDK Example'),
actions: [
IconButton(
icon: const Icon(Icons.stop_circle_outlined),
onPressed: () => _ysdk.stopSearch(),
tooltip: 'Stop NFC Search',
)
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Status: $_status', style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _bindService,
child: const Text('Connect to Device Service'),
),
const Divider(),
Text('Device Info:', style: Theme.of(context).textTheme.titleMedium),
Text(_deviceInfo),
ElevatedButton(
onPressed: _getDeviceInfo,
child: const Text('Get Device Info'),
),
const Divider(),
Text('NFC Status:', style: Theme.of(context).textTheme.titleMedium),
Container(
padding: const EdgeInsets.all(8),
color: Colors.grey[200],
child: Text(_nfcMessage, style: const TextStyle(fontWeight: FontWeight.bold))
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
children: [
ElevatedButton(
onPressed: () => _runNfcScan('CpuRF'),
child: const Text('Scan (Generic)'),
),
ElevatedButton(
onPressed: () => _runNfcScan('TypeA'),
child: const Text('Scan (Type A)'),
),
ElevatedButton(
onPressed: () => _runNfcScan('NTAG'),
child: const Text('Scan (NTAG)'),
),
]
),
const Divider(),
TextField(
controller: _textController,
decoration: const InputDecoration(
labelText: 'Text to Print',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _printText,
child: const Text('Print Text'),
),
],
),
),
);
}
}