123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/components/dialog_number.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/nibp.dart';
- import 'package:vnote_device_plugin/models/exams/nibp.dart';
- import 'package:vitalapp/components/alert_dialog.dart';
- import 'package:vitalapp/managers/interfaces/models/device.dart';
- import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
- import 'package:vitalapp/pages/medical/controller.dart';
- import 'package:vitalapp/pages/medical/controllers/nibp.dart';
- import 'package:vitalapp/pages/medical/models/item.dart';
- import 'package:vitalapp/pages/medical/models/worker.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status.dart';
- import 'package:vitalapp/pages/medical/widgets/switch_button.dart';
- /// TODO 需要优化
- /// 小弹窗输入
- class VDialogBloodPressure extends StatelessWidget {
- /// 标题
- final String? title;
- /// 描述
- final String? description;
- /// 输入占位符
- final String? placeholder;
- /// 初始值
- final List<String>? initialValue;
- const VDialogBloodPressure({
- super.key,
- this.title,
- this.description,
- this.placeholder,
- this.initialValue,
- });
- Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
- @override
- Widget build(BuildContext context) {
- final controller1 = TextEditingController(text: initialValue?.first);
- final controller2 = TextEditingController(text: initialValue?.last);
- return VAlertDialog(
- title: title,
- width: 440,
- contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
- content: _buildContent(context, controller1, controller2),
- showCancel: true,
- onConfirm: () {
- Get.back(result: json.encode([controller1.text, controller2.text]));
- },
- );
- }
- Widget _buildContent(
- BuildContext context,
- TextEditingController controller1,
- TextEditingController controller2,
- ) {
- final children = <Widget>[];
- if (description != null) {
- children.add(
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 4),
- child: Text(
- description!,
- style: const TextStyle(color: Colors.black87, fontSize: 18),
- ),
- ),
- );
- children.add(const SizedBox(height: 8));
- } else {
- children.add(const SizedBox(height: 12));
- }
- children.add(_buildInputWidget(context, controller1, controller2));
- return SingleChildScrollView(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: children,
- ),
- );
- }
- Widget _buildInputWidget(
- BuildContext context,
- TextEditingController controller1,
- TextEditingController controller2,
- ) {
- const fontSize = 20.0;
- return Row(
- children: [
- _buildItem(context, controller1, '收缩压'),
- const Text(
- '/',
- style: TextStyle(fontSize: fontSize),
- ),
- _buildItem(context, controller2, '舒张压'),
- ],
- );
- }
- Widget _buildItem(
- BuildContext context,
- TextEditingController controller,
- String hintText,
- ) {
- const fontSize = 20.0;
- const height = 56.0;
- return Expanded(
- child: SizedBox(
- height: height,
- child: TextField(
- controller: controller,
- readOnly: false,
- autofocus: true,
- keyboardType: const TextInputType.numberWithOptions(
- decimal: true), // 允许输入数字和小数点
- inputFormatters: [
- FilteringTextInputFormatter.allow(
- RegExp(r'^\d+\.?\d{0,2}'),
- ), // 只允许输入数字和小数点,俩位小数
- ],
- style: const TextStyle(fontSize: fontSize),
- decoration: InputDecoration(
- border: const UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(),
- ),
- enabledBorder: const UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(
- color: Colors.black54,
- ),
- ),
- focusedBorder: UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(
- color: Theme.of(context).primaryColor.withOpacity(.4),
- ),
- ),
- filled: true,
- fillColor: Colors.white,
- contentPadding: const EdgeInsets.symmetric(
- vertical: (height - fontSize * 1.2) / 2,
- horizontal: 8,
- ),
- hintStyle: const TextStyle(fontSize: fontSize),
- labelStyle: const TextStyle(fontSize: fontSize),
- hintText: hintText,
- isCollapsed: true,
- ),
- onSubmitted: (value) {
- print(value);
- Get.back(result: value);
- },
- ),
- ),
- );
- }
- }
- // ignore: must_be_immutable
- class BloodPressure extends StatefulWidget {
- const BloodPressure({
- super.key,
- // required this.currentValue,
- // required this.bloodPressure,
- });
- // Map<String, dynamic> currentValue;
- // Function(Map<String, dynamic>) bloodPressure;
- @override
- State<BloodPressure> createState() => _ExamBloodPressureState();
- }
- class _ExamBloodPressureState extends State<BloodPressure> {
- var controller = Get.find<MedicalController>();
- PressureStatus pressureStatus = PressureStatus.left;
- late NibpDeviceController nibp;
- NibpDeviceWorker? worker;
- int liveValue = 0;
- int errorCount = 0; //设备重连失败次数
- // late NibpExamValue? value = NibpExamValue(
- // diastolicPressure: int.parse(
- // controller.diagnosisDataValue['NIBP']?['Sbp']?.toString() ?? '',
- // ),
- // systolicPressure: int.parse(
- // controller.diagnosisDataValue['NIBP']?['Dbp']?.toString() ?? '',
- // ),
- // pulse: 0,
- // );
- WorkerStatus _connectStatus = WorkerStatus.connecting;
- @override
- void initState() {
- currentDevice();
- super.initState();
- }
- Future<void> currentDevice() async {
- DeviceModel? device = await controller.getDevice(DeviceTypes.NIBP);
- if (device.isNull) {
- _connectStatus = WorkerStatus.unboundDevice;
- worker = null;
- setState(() {});
- return;
- }
- nibp = NibpDeviceController(device?.model ?? '', device?.mac ?? '');
- worker = nibp.worker;
- _connectStatus = nibp.connectStatus;
- loadListeners();
- }
- void loadListeners() {
- worker!.liveUpdateEvent.addListener(_onLiveUpdate);
- worker!.resultUpdateEvent.addListener(_onSuccess);
- worker!.connectErrorEvent.addListener(_onConnectFail);
- worker!.connectedEvent.addListener(_onConnectSuccess);
- worker!.connect();
- }
- Future<void> disconnect() async {
- try {
- if (worker != null) {
- await worker!.disconnect();
- worker!.connectErrorEvent.removeListener(_onConnectFail);
- worker!.connectedEvent.removeListener(_onConnectSuccess);
- worker!.liveUpdateEvent.removeListener(_onLiveUpdate);
- worker!.resultUpdateEvent.removeListener(_onSuccess);
- worker!.disconnectedEvent.removeListener(_onDisconnected);
- }
- } catch (err) {
- print(err);
- }
- }
- @override
- void dispose() {
- disconnect();
- worker?.dispose();
- super.dispose();
- }
- void _onLiveUpdate(_, int e) {
- setState(() {
- liveValue = e;
- });
- }
- /// TODO 需求不清,检测的数据需要传给体检,但是检测又不区分左右侧血压
- void _onSuccess(_, NibpExamValue e) {
- setState(() {
- /// 这是第三方需要的数据
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp': e.systolicPressure.toString(),
- 'Dbp': e.diastolicPressure.toString(),
- 'Pulse_Beat': e.pulse.toString(),
- };
- controller.saveCachedRecord();
- });
- }
- 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(() {});
- }
- Widget _buildValue() {
- if (controller.diagnosisDataValue['NIBP']?['Sbp'] != null) {
- return _buildResultWidget();
- }
- return _buildLiveWidget();
- }
- @override
- Widget build(BuildContext context) {
- return Stack(
- children: [
- ExamCard(
- title: '血压',
- content: Container(
- padding: const EdgeInsets.only(top: 50),
- child: Column(
- children: [
- InkWell(
- child: _SideBar(
- value: _buildValue(),
- unit: 'mmHg',
- ),
- onTap: () async {
- String? result = await VDialogBloodPressure(
- title: '血压',
- initialValue: [
- controller.diagnosisDataValue['NIBP']?['Sbp']
- .toString() ??
- '',
- controller.diagnosisDataValue['NIBP']?['Dbp']
- .toString() ??
- '',
- ],
- ).show();
- if (result != null &&
- jsonDecode(result)?.first != '' &&
- jsonDecode(result)?.last != '') {
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp': jsonDecode(result)?.first,
- 'Dbp': jsonDecode(result)?.last,
- 'Pulse_Beat': '',
- };
- print(result);
- controller.saveCachedRecord();
- }
- setState(() {});
- },
- ),
- InkWell(
- onTap: () async {
- String? result = await VDialogNumber(
- title: '脉率',
- initialValue: controller.diagnosisDataValue['NIBP']
- ?['Pulse_Beat'] ??
- '',
- ).show();
- if (result != null && result.isNotEmpty) {
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp': controller.diagnosisDataValue['NIBP']['Sbp'],
- 'Dbp': controller.diagnosisDataValue['NIBP']['Dbp'],
- 'Pulse_Beat': result
- };
- controller.saveCachedRecord();
- }
- setState(() {});
- },
- child: Container(
- padding: const EdgeInsets.symmetric(horizontal: 28),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- '脉率',
- style: TextStyle(fontSize: 25),
- ),
- Text(
- controller.diagnosisDataValue['NIBP']?['Pulse_Beat']
- .toString() ??
- '',
- style: const TextStyle(
- fontSize: 48,
- ),
- )
- ],
- ),
- ),
- )
- ],
- ),
- ),
- ),
- if (errorCount < 3)
- DeviceStatusPosition(
- deviceStatus: DeviceStatus(connectStatus: _connectStatus),
- ),
- if (errorCount >= 3) _buildErrorButton(),
- ],
- );
- }
- /// 需要封装一下
- 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,
- ),
- ],
- ),
- );
- }
- Widget _buildLeftOrRightBloodPressure() {
- return Positioned(
- top: 130,
- left: 38,
- child: AnimatedToggle(
- values: const ['左侧', '右侧'],
- onToggleCallback: (value) {
- pressureStatus = value;
- setState(() {});
- },
- statusValue: pressureStatus,
- buttonColor: Theme.of(context).primaryColor,
- backgroundColor: const Color(0xFFB5C1CC),
- textColor: const Color(0xFFFFFFFF),
- ),
- // child: Text('Toggle Value : $_toggleValue'),
- );
- }
- Widget _buildLiveWidget() {
- return Center(
- child: RichText(
- text: TextSpan(
- text: liveValue.toString() == '0' ? '--' : liveValue.toString(),
- style: const TextStyle(
- fontSize: 80,
- color: Colors.black,
- ),
- children: const [
- TextSpan(text: ' '),
- TextSpan(
- text: 'mmHg',
- style: TextStyle(fontSize: 25),
- )
- ],
- ),
- ),
- );
- }
- Widget _buildResultWidget() {
- const textStyle = TextStyle(
- fontSize: 48,
- );
- return Stack(
- children: [
- Column(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Align(
- alignment: Alignment.centerRight,
- child: Text(
- controller.diagnosisDataValue['NIBP']['Sbp'],
- style: textStyle,
- ),
- ),
- Align(
- alignment: Alignment.centerLeft,
- child: Text(
- controller.diagnosisDataValue['NIBP']['Dbp'],
- style: textStyle,
- ),
- ),
- ],
- ),
- const Positioned.fill(
- child: Center(
- child: Text(
- " /",
- style: TextStyle(fontSize: 24),
- ),
- ),
- ),
- ],
- );
- }
- }
- class _SideBar extends StatelessWidget {
- final Widget value;
- final String unit;
- const _SideBar({
- required this.value,
- required this.unit,
- });
- @override
- Widget build(BuildContext context) {
- return Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Container(
- alignment: Alignment.bottomRight,
- padding: const EdgeInsets.only(
- bottom: 20,
- right: 30,
- left: 40,
- ),
- child: FittedBox(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- value,
- ],
- ),
- ),
- ),
- ],
- );
- }
- }
|