123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status_position.dart';
- import 'package:vnote_device_plugin/consts/types.dart';
- import 'package:vnote_device_plugin/devices/weight.dart';
- import 'package:vitalapp/managers/interfaces/models/device.dart';
- import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
- import 'package:vitalapp/components/dialog_number.dart';
- import 'package:vitalapp/pages/medical/controller.dart';
- import 'package:vitalapp/pages/medical/controllers/bmi.dart';
- import 'package:vitalapp/pages/medical/models/worker.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status.dart';
- import 'package:vitalapp/pages/medical/widgets/side_bar.dart';
- class BodyWeight extends StatefulWidget {
- const BodyWeight({
- super.key,
- });
- @override
- State<BodyWeight> createState() => _ExamBodyWeightState();
- }
- class _ExamBodyWeightState extends State<BodyWeight> {
- var controller = Get.find<MedicalController>();
- late BmiDeviceController bmi;
- WeightDeviceWorker? worker;
- int errorCount = 0; //设备重连失败次数
- WorkerStatus _connectStatus = WorkerStatus.connecting;
- late String _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
- late String _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
- late String _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
- @override
- void initState() {
- initBmi();
- super.initState();
- }
- Future<void> connect() async {
- await worker!.connect();
- }
- Future<void> disconnect() async {
- if (worker != null) {
- await worker!.disconnect();
- releaseListeners();
- }
- }
- void releaseListeners() {
- worker!.connectErrorEvent.removeListener(_onConnectFail);
- worker!.connectedEvent.removeListener(_onConnectSuccess);
- worker!.successEvent.removeListener(_onSuccess);
- worker!.disconnectedEvent.removeListener(_onDisconnected);
- }
- @override
- void dispose() {
- disconnect();
- worker?.dispose();
- super.dispose();
- }
- void loadListeners() {
- worker!.successEvent.addListener(_onSuccess);
- worker!.connectErrorEvent.addListener(_onConnectFail);
- worker!.connectedEvent.addListener(_onConnectSuccess);
- worker!.disconnectedEvent.addListener(_onDisconnected);
- worker!.connect();
- }
- Future<void> currentDevice() async {
- DeviceModel? device = await controller.getDevice(DeviceTypes.WEIGHT);
- if (device.isNull) {
- _connectStatus = WorkerStatus.unboundDevice;
- worker = null;
- setState(() {});
- return;
- }
- bmi = BmiDeviceController(device?.model ?? '', device?.mac ?? '');
- worker = bmi.worker;
- _connectStatus = bmi.connectStatus;
- loadListeners();
- }
- Future<void> initBmi() async {
- currentDevice();
- await initData();
- }
- Future<void> initData() async {
- await controller.readCachedRecord();
- if (controller.diagnosisDataValue['BMI'] == null) {
- controller.diagnosisDataValue['BMI'] = {};
- }
- _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
- _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
- _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
- setState(() {});
- }
- void _onSuccess(_, double e) {
- setState(() {
- _weight = e.toString();
- controller.saveCachedRecord();
- _connectStatus = WorkerStatus.connected;
- });
- }
- void _onDeviceCloseSuccess(sender, e) {
- if (e) {
- connect();
- }
- }
- void _onConnectFail(sender, e) {
- print('连接设备失败');
- if (errorCount < 3) {
- worker?.connect();
- }
- setState(() {
- errorCount++;
- _connectStatus = WorkerStatus.connectionFailed;
- });
- }
- void _onDisconnected(sender, e) {
- print('设备连接中断');
- if (errorCount < 3) {
- worker?.connect();
- }
- setState(() {
- errorCount++;
- _connectStatus = WorkerStatus.disconnected;
- });
- }
- void _onConnectSuccess(sender, e) {
- _connectStatus = WorkerStatus.connected;
- setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return ExamCard(
- titleText: const SizedBox(),
- // clickCard: () {},
- content: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- SideBar(
- title: '身高',
- value: _height.isEmpty ? '--' : _height,
- unit: 'cm',
- onTap: _inputHeight,
- ),
- const Divider(indent: 30),
- Stack(
- children: [
- SideBar(
- title: '体重',
- value: _weight.isEmpty ? '--' : _weight,
- hasDevice: true,
- unit: 'kg',
- onTap: _inputWeight,
- ),
- if (errorCount < 3)
- DeviceStatusPosition(
- deviceStatus: DeviceStatus(connectStatus: _connectStatus),
- ),
- if (errorCount >= 3) _buildErrorButton(),
- ],
- ),
- const Divider(indent: 30),
- SideBar(
- title: 'BMI',
- value: _bmi.isEmpty ? '--' : _bmi,
- unit: 'kg/m²',
- ),
- ],
- ));
- }
- Widget _buildErrorButton() {
- return DeviceStatusPosition(
- deviceStatus: Row(
- children: [
- const Text(
- '请确认设备是否启动',
- style: TextStyle(fontSize: 24, color: Colors.red),
- ),
- const SizedBox(
- width: 8,
- ),
- IconButton(
- onPressed: () {
- worker?.connect();
- setState(() {
- _connectStatus = WorkerStatus.connecting;
- errorCount = 0;
- });
- },
- icon: const Icon(Icons.refresh),
- iconSize: 32,
- ),
- const SizedBox(
- width: 32,
- ),
- ],
- ),
- );
- }
- Future<void> _inputWeight() async {
- String? result = await VDialogNumber(
- title: '体重',
- initialValue: _weight,
- ).show();
- if (result?.isNotEmpty ?? false) {
- _weight = result ?? '';
- getBmi();
- controller.diagnosisDataValue['BMI']?['Weight'] = _weight;
- controller.diagnosisDataValue['BMI']?['Bmi'] = _bmi;
- controller.saveCachedRecord();
- }
- setState(() {});
- }
- void getBmi() {
- if (_weight.isNotEmpty && _height.isNotEmpty) {
- _bmi = (double.parse(_weight) /
- ((double.parse(_height) / 100) * (double.parse(_height) / 100)))
- .toStringAsFixed(2);
- controller.saveCachedRecord();
- }
- }
- Future<void> _inputHeight() async {
- String? result = await VDialogNumber(
- title: '身高',
- initialValue: _height,
- ).show();
- if (result?.isNotEmpty ?? false) {
- _height = result ?? '';
- getBmi();
- controller.diagnosisDataValue['BMI']?['Height'] = _height;
- controller.diagnosisDataValue['BMI']?['Bmi'] = _bmi;
- controller.saveCachedRecord();
- }
- setState(() {});
- }
- }
|