Files
zhiwen/example/lib/main.dart
2026-03-31 08:51:45 +08:00

131 lines
4.1 KiB
Dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:zhiwen/zhiwen.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';
final _zhiwenPlugin = Zhiwen();
@override
void initState() {
super.initState();
initPlatformState();
}
// 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 _zhiwenPlugin.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;
});
}
@override
Widget build(BuildContext context) {
return ScaffoldMessenger(
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
ElevatedButton(
onPressed: () async {
bool result = await _zhiwenPlugin.openDevice("USB", 9600);
},
child: const Text('打开设备'),
),
ElevatedButton(
onPressed: () async {
await _zhiwenPlugin.closeDevice();
},
child: const Text('关闭设备'),
),
ElevatedButton(
onPressed: () async {
var random = Random();
int randomNumber = random.nextInt(100); // 生成 0 到 99 的随机整数
Map<String, dynamic> result = await _zhiwenPlugin.enrollFingerprint(randomNumber);
print(result);
},
child: const Text('录入指纹'),
),
ElevatedButton(
onPressed: () async {
int userId = 24;
Map<String, dynamic> result = await _zhiwenPlugin.verifyFingerprint(userId);
print(result);
},
child: const Text('验证指纹'),
),
ElevatedButton(
onPressed: () async {
Map<String, dynamic> result = await _zhiwenPlugin.deleteAllFingerprint();
print(result);
},
child: const Text('删除所有指纹'),
),
ElevatedButton(
onPressed: () async {
int userId = 2;
Map<String, dynamic> result = await _zhiwenPlugin.deleteOneFingerprint(userId);
print(result);
},
child: const Text('删除单个指纹'),
),
ElevatedButton(
onPressed: () async {
Map<String, dynamic> result = await _zhiwenPlugin.identifyFingerprint();
print(result);
},
child: const Text('识别指纹'),
),
ElevatedButton(
onPressed: () async {
Map<String, dynamic> result = await _zhiwenPlugin.getUserCount();
print(result);
},
child: const Text('获取指纹总数'),
),
],
),
),
),
));
}
}