import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:vnote_device_plugin/devices/sugar.dart'; import 'package:vnote_device_plugin/models/exams/sugar.dart'; import 'package:vnoteapp/components/alert_dialog.dart'; import 'package:vnoteapp/components/button.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'; class ExamBloodSugar extends StatefulWidget { const ExamBloodSugar({ super.key, required this.currentFormObject, required this.currentInputValue, this.specialInput, }); final FormObject currentFormObject; final String currentInputValue; final Function(String value)? specialInput; @override State createState() => _ExamBloodSugarState(); } class _ExamBloodSugarState extends State { var permissionManager = Get.find(); @override void initState() { permissionManager.requestLocationPermission(); super.initState(); } @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 _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( BloodSugar( currentFormObject: widget.currentFormObject, bloodSugar: widget.currentInputValue, ), barrierDismissible: false, ); widget.specialInput?.call(result); print(result); } } class BloodSugar extends StatefulWidget { const BloodSugar({ super.key, required this.currentFormObject, this.bloodSugar, }); final FormObject currentFormObject; final String? bloodSugar; @override State createState() => _BloodSugarState(); } class _BloodSugarState extends State { late final SugarDeviceWorker worker = SugarDeviceWorker( mac: '60:98:66:C4:0E:51', model: 'i-sens630', ); late TextEditingController specialInputController = TextEditingController(text: widget.bloodSugar ?? '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 connect() async { connectFailState = false; isConnect = true; connectSuccessState = false; setState(() {}); await worker.connect(); } Future disconnect() async { worker.connectErrorEvent.removeListener(_onConnectFail); worker.connectedEvent.removeListener(_onConnectSuccess); await worker.disconnect(); } @override void dispose() { super.dispose(); } void _onSuccess(_, SugarExamData e) { setState(() { specialInputController.text = e.sugar.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(), ), ], ), ); } }