aclas_scale 0.0.1 aclas_scale: ^0.0.1 copied to clipboard
顶尖电子秤 适用于:联迪 LIANDI AECR J10
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:aclas_scale/aclas_scale.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 _aclasScalePlugin = AclasScale();
String weight = "0.0";
String trareWeight = "";
@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 _aclasScalePlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
_aclasScalePlugin.setCallback(
(v) => {
print(v),
// 输出秤的数据到控制台
setState(() {
weight = v.m_fWeight.toString() + v.m_strUnit;
})
},
(v) => {
print(v),
setState(() {
trareWeight = v.toString();
})
});
// 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 MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(children: [
Row(children: [
Text('Running on: $_platformVersion\n'),
]),
Row(children: [
ElevatedButton(
onPressed: () async {
await _aclasScalePlugin.openScale();
},
child: const Text('打开秤'),
),
ElevatedButton(
onPressed: () async {
await _aclasScalePlugin.closeScale();
},
child: const Text('关闭称'),
),
]),
Row(children: [
Text('重量: $weight\n'),
Text('皮重: $trareWeight\n'),
]),
Row(children: [
ElevatedButton(
onPressed: () async {
await _aclasScalePlugin.tare();
},
child: const Text('去皮'),
),
ElevatedButton(
onPressed: () async {
await _aclasScalePlugin.setZero();
},
child: const Text('归零'),
),
]),
Row(children: [
ElevatedButton(
onPressed: () async {
await _aclasScalePlugin.readTare();
},
child: const Text('读皮重'),
),
]),
]),
),
);
}
}