123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vnote_device_plugin/devices/weight.dart';
- import 'package:vnoteapp/managers/interfaces/permission.dart';
- import 'package:vnoteapp/pages/check/models/form.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_configurable/exam_card.dart';
- import 'package:vnoteapp/components/dialog_number.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_disconnected.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_fail.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_success.dart';
- // ignore: must_be_immutable
- class BodyWeight extends StatefulWidget {
- const BodyWeight({
- super.key,
- });
- @override
- State<BodyWeight> createState() => _ExamBodyWeightState();
- }
- class _ExamBodyWeightState extends State<BodyWeight> {
- var permissionManager = Get.find<IPermissionManager>();
- late final WeightDeviceWorker worker = WeightDeviceWorker(
- mac: 'CF:E4:2C:22:01:39',
- model: 'CF398BLE',
- );
- /// 设备连接失败的状态
- bool _connectFailStatus = false;
- /// 设备连接中断的状态
- bool _connectDisconnectedStatus = false;
- /// 设备连接成功的状态
- bool _connectSuccessStatus = false;
- /// 设备是否连接中
- bool _isConnect = false;
- String _value = '';
- @override
- void initState() {
- getPermission();
- worker.successEvent.addListener(_onSuccess);
- worker.connectErrorEvent.addListener(_onConnectFail);
- worker.connectedEvent.addListener(_onConnectSuccess);
- worker.disconnectedEvent.addListener(_onDisconnected);
- connect();
- super.initState();
- }
- Future<void> connect() async {
- _connectFailStatus = false;
- _isConnect = true;
- _connectSuccessStatus = false;
- _connectDisconnectedStatus = false;
- setState(() {});
- await worker.connect();
- }
- Future<void> disconnect() async {
- worker.connectErrorEvent.removeListener(_onConnectFail);
- worker.connectedEvent.removeListener(_onConnectSuccess);
- worker.successEvent.removeListener(_onSuccess);
- worker.disconnectedEvent.removeListener(_onDisconnected);
- await worker.disconnect();
- }
- @override
- void dispose() {
- disconnect();
- super.dispose();
- }
- void _onSuccess(_, double e) {
- setState(() {
- _value = e.toString();
- _isConnect = false;
- });
- }
- void _onConnectFail(sender, e) {
- print('连接设备失败');
- _connectFailStatus = true;
- _connectSuccessStatus = false;
- _connectDisconnectedStatus = false;
- _isConnect = false;
- setState(() {});
- }
- void _onConnectSuccess(sender, e) {
- _connectSuccessStatus = true;
- _isConnect = false;
- _connectFailStatus = false;
- _connectDisconnectedStatus = false;
- setState(() {});
- }
- void _onDisconnected(sender, e) {
- print('设备连接中断');
- _connectDisconnectedStatus = true;
- _connectSuccessStatus = false;
- _connectFailStatus = false;
- _isConnect = false;
- setState(() {});
- }
- Future<void> getPermission() async {
- await permissionManager.requestLocationPermission();
- await permissionManager.requestBluetoothConnectPermission();
- await permissionManager.requestBluetoothAdvertisePermission();
- await permissionManager.requestBluetoothScanPermission();
- }
- @override
- Widget build(BuildContext context) {
- return Stack(
- children: [
- _buildTemperature(),
- if (_connectFailStatus)
- DeviceConnectFail(
- connect: () => connect(),
- ),
- if (_connectSuccessStatus) const DeviceConnectSuccess(),
- if (_isConnect) const DeviceConnect(),
- if (_connectDisconnectedStatus)
- DeviceConnectDisconnected(
- connect: () => connect(),
- ),
- ],
- );
- }
- Widget _buildTemperature() {
- return ExamCard(
- title: '体重',
- clickCard: () {
- _inputTemperature();
- },
- content: Container(
- alignment: Alignment.bottomRight,
- padding: const EdgeInsets.only(
- bottom: 20,
- right: 30,
- left: 40,
- ),
- constraints: const BoxConstraints(minHeight: 150),
- child: FittedBox(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- RichText(
- text: TextSpan(
- text: _value,
- style: const TextStyle(
- fontSize: 80,
- color: Colors.black,
- ),
- children: const [
- TextSpan(
- text: 'kg',
- style: TextStyle(fontSize: 25),
- )
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- Future<void> _inputTemperature() async {
- String? result = await VDialogNumber(
- title: '体重',
- initialValue: _value,
- ).show();
- if (result?.isNotEmpty ?? false) {
- _value = result ?? '';
- }
- }
- }
- // class BodyWeight extends StatefulWidget {
- // const BodyWeight({
- // super.key,
- // required this.currentFormObject,
- // required this.currentInputValue,
- // this.specialInput,
- // });
- // final FormObject currentFormObject;
- // final String currentInputValue;
- // final Function(String value)? specialInput;
- // @override
- // State<ExamBodyWeight> createState() => _ExamBodyWeightState();
- // }
- // class _ExamBodyWeightState extends State<ExamBodyWeight> {
- // var permissionManager = Get.find<IPermissionManager>();
- // @override
- // void initState() {
- // getPermission();
- // super.initState();
- // }
- // Future<void> getPermission() async {
- // await permissionManager.requestLocationPermission();
- // await permissionManager.requestBluetoothConnectPermission();
- // await permissionManager.requestBluetoothAdvertisePermission();
- // await permissionManager.requestBluetoothScanPermission();
- // }
- // @override
- // Widget build(BuildContext context) {
- // return ExamCard(
- // title: widget.currentFormObject.label ?? '',
- // clickCard: () {
- // _buildTempertureInput(widget.currentFormObject);
- // },
- // content: Container(
- // alignment: Alignment.bottomRight,
- // padding: const EdgeInsets.only(
- // bottom: 20,
- // right: 30,
- // left: 40,
- // ),
- // constraints: const BoxConstraints(minHeight: 150),
- // child: FittedBox(
- // child: Row(
- // mainAxisAlignment: MainAxisAlignment.end,
- // crossAxisAlignment: CrossAxisAlignment.end,
- // children: [
- // RichText(
- // text: TextSpan(
- // text: widget.currentInputValue,
- // style: const TextStyle(
- // fontSize: 80,
- // color: Colors.black,
- // ),
- // children: [
- // TextSpan(
- // text: widget.currentFormObject.append ?? '',
- // style: const TextStyle(fontSize: 25),
- // )
- // ],
- // ),
- // ),
- // ],
- // ),
- // ),
- // ),
- // );
- // }
- // Future<void> _buildTempertureInput(FormObject currentFormObject) async {
- // // Future.delayed(const Duration(milliseconds: 3000), () {
- // // specialInputController.text = generateRandomNumber().toString();
- // // widget.specialInput?.call(specialInputController.text);
- // // setState(() {});
- // // });
- // final result = await Get.dialog(
- // Weight(
- // currentFormObject: widget.currentFormObject,
- // weight: widget.currentInputValue,
- // ),
- // barrierDismissible: false,
- // );
- // widget.specialInput?.call(result);
- // print('object');
- // print(result);
- // print('object');
- // }
- // }
- // class Weight extends StatefulWidget {
- // const Weight({
- // super.key,
- // required this.currentFormObject,
- // this.weight,
- // });
- // final FormObject currentFormObject;
- // final String? weight;
- // @override
- // State<Weight> createState() => _WeightState();
- // }
- // class _WeightState extends State<Weight> {
- // late final WeightDeviceWorker worker = WeightDeviceWorker(
- // mac: 'CF:E4:2C:22:01:39',
- // model: 'CF398BLE',
- // );
- // late TextEditingController specialInputController =
- // TextEditingController(text: widget.weight ?? '00.0');
- // bool connectFailState = false;
- // bool connectSuccessState = false;
- // bool isConnect = false;
- // @override
- // void initState() {
- // connect();
- // worker.successEvent.addListener(_onSuccess);
- // worker.connectErrorEvent.addListener(_onConnectFail);
- // worker.connectedEvent.addListener(_onConnectSuccess);
- // super.initState();
- // }
- // Future<void> connect() async {
- // connectFailState = false;
- // isConnect = true;
- // connectSuccessState = false;
- // setState(() {});
- // await worker.connect();
- // }
- // Future<void> disconnect() async {
- // worker.connectErrorEvent.removeListener(_onConnectFail);
- // worker.connectedEvent.removeListener(_onConnectSuccess);
- // worker.successEvent.removeListener(_onSuccess);
- // await worker.disconnect();
- // }
- // @override
- // void dispose() {
- // super.dispose();
- // }
- // void _onSuccess(_, double e) {
- // setState(() {
- // specialInputController.text = e.toString();
- // // connectFailState = false;
- // // connectSuccessState = false;
- // isConnect = false;
- // // disconnect();
- // });
- // }
- // void _onConnectFail(sender, e) {
- // print('连接设备失败');
- // connectFailState = true;
- // connectSuccessState = false;
- // isConnect = false;
- // disconnect();
- // setState(() {});
- // }
- // void _onConnectSuccess(sender, e) {
- // connectSuccessState = true;
- // connectFailState = false;
- // isConnect = false;
- // setState(() {});
- // }
- // @override
- // Widget build(BuildContext context) {
- // return VAlertDialog(
- // title: widget.currentFormObject.label ?? '',
- // width: 600,
- // contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
- // content: buildMainWidget(),
- // showCancel: true,
- // onConfirm: () {
- // disconnect();
- // Get.back(result: specialInputController.text);
- // },
- // onCanceled: () {
- // disconnect();
- // },
- // );
- // }
- // Widget buildInputField() {
- // return Container(
- // width: 350,
- // padding: const EdgeInsets.only(left: 15),
- // child: TextFormField(
- // keyboardType: TextInputType.number,
- // style: const TextStyle(
- // fontSize: 100,
- // ),
- // showCursor: false,
- // controller: specialInputController,
- // decoration: const InputDecoration(
- // labelStyle: TextStyle(
- // fontSize: 100,
- // ),
- // ),
- // ),
- // );
- // }
- // Widget buildConnectFailText() {
- // return const Text(
- // '设备连接失败',
- // style: TextStyle(
- // color: Colors.red,
- // fontSize: 25,
- // ),
- // textAlign: TextAlign.left,
- // );
- // }
- // Widget buildConnectSuccessText() {
- // return const Text(
- // '设备连接成功',
- // style: TextStyle(
- // color: Colors.green,
- // fontSize: 25,
- // ),
- // textAlign: TextAlign.left,
- // );
- // }
- // Widget buildConnectingText() {
- // return const Row(
- // children: [
- // Expanded(
- // child: Text(
- // '设备连接中',
- // style: TextStyle(
- // fontSize: 40,
- // color: Colors.blue,
- // ),
- // ),
- // ),
- // CircularProgressIndicator(
- // valueColor: AlwaysStoppedAnimation(
- // Colors.blue,
- // ),
- // ),
- // SizedBox(
- // width: 20,
- // ),
- // ],
- // );
- // }
- // Widget buildReconnectButton() {
- // return Container(
- // margin: const EdgeInsets.only(top: 4),
- // width: 134,
- // child: VButton(
- // onTap: () async {
- // /// TODO
- // await connect();
- // worker.connectErrorEvent.addListener(_onConnectFail);
- // worker.connectedEvent.addListener(_onConnectSuccess);
- // /// TODO 后面需要改,这边暂时演示用
- // Future.delayed(const Duration(milliseconds: 8000), () {
- // if (!connectSuccessState) {
- // connectFailState = true;
- // }
- // });
- // },
- // child: const Row(
- // mainAxisAlignment: MainAxisAlignment.center,
- // children: [
- // Icon(Icons.connected_tv_rounded, size: 24),
- // SizedBox(
- // width: 8,
- // ),
- // Text("重连", style: TextStyle(fontSize: 20)),
- // ],
- // ),
- // ),
- // );
- // }
- // Widget buildConnectStateWidgets() {
- // return Column(
- // children: [
- // if (connectFailState) buildConnectFailText(),
- // if (connectSuccessState) buildConnectSuccessText(),
- // if (isConnect)
- // buildConnectingText()
- // else if (connectFailState)
- // buildReconnectButton(),
- // ],
- // );
- // }
- // Widget buildMainWidget() {
- // return SizedBox(
- // height: 100,
- // child: Row(
- // children: [
- // buildInputField(),
- // Expanded(
- // child: buildConnectStateWidgets(),
- // ),
- // ],
- // ),
- // );
- // }
- // }
|