controller.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/managers/interfaces/exam.dart';
  6. import 'package:vitalapp/pages/medical/controller.dart';
  7. import 'package:vitalapp/store/store.dart';
  8. class TableInputDialogController extends GetxController {
  9. TableInputDialogController({
  10. required this.tableDataConfig,
  11. });
  12. final _examManager = Get.find<IExamManager>();
  13. final List<TableElementConfig> tableDataConfig;
  14. final List<TableElementData> tableData = [];
  15. Future<void> onConfirm(
  16. String? physicalExamNumber,
  17. String? keyValue,
  18. ) async {
  19. final result = <String, String>{};
  20. tableData.forEach((element) {
  21. String vlaue = element.textController.text;
  22. if (vlaue.isEmpty) {
  23. vlaue = '-';
  24. }
  25. result[element.config.id] = vlaue;
  26. });
  27. await createCheckup(physicalExamNumber, keyValue, result);
  28. Get.find<MedicalController>().onSelectExam.emit(this, true);
  29. // Get.back(result: TableInputResult(data: result));
  30. }
  31. /// 体检 检查提交
  32. Future<void> createCheckup(
  33. String? physicalExamNumber,
  34. String? keyValue,
  35. Map<String, dynamic> diagnosisDataValue,
  36. ) async {
  37. // Map<String, dynamic> input = diagnosisDataValue;
  38. // Map<String, dynamic> output = {};
  39. // input.forEach((key, value) {
  40. // value.forEach((innerKey, innerValue) {
  41. // output[innerKey] = innerValue;
  42. // });
  43. // });
  44. var result = await _examManager.createExam(CreateExamRequest(
  45. key: keyValue ?? "HEIBasic",
  46. examData: jsonEncode(diagnosisDataValue),
  47. physicalExamNumber:
  48. Store.user.currentSelectRegisterPersonInfo?.physicalExamNumber,
  49. ));
  50. if (result == true) {
  51. Get.back();
  52. }
  53. print(result);
  54. }
  55. void focusPre() {
  56. int index = getCurrFocusIndex();
  57. if (index != -1) {
  58. if (index > 0) {
  59. moveFocus(index - 1);
  60. }
  61. if (index == 0) {
  62. moveFocus(tableData.length - 1);
  63. }
  64. }
  65. }
  66. void focusNext() {
  67. int index = getCurrFocusIndex();
  68. if (index != -1) {
  69. if (index < tableData.length - 1) {
  70. moveFocus(index + 1);
  71. }
  72. if (index == tableData.length - 1) {
  73. moveFocus(0);
  74. }
  75. }
  76. }
  77. int getCurrFocusIndex() {
  78. for (int i = 0; i < tableData.length; i++) {
  79. final FocusNode focusNode = tableData[i].focusNode;
  80. if (focusNode.hasFocus) {
  81. return i;
  82. }
  83. }
  84. return -1;
  85. }
  86. void moveFocus(int index) {
  87. print('moveFocus: $index');
  88. if (index >= 0 && index < tableData.length) {
  89. tableData[index].focusNode.requestFocus();
  90. // 如果该行有值,下一帧执行,选择全部
  91. if (tableData[index].textController.text.isNotEmpty) {
  92. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  93. Future.delayed(Duration(milliseconds: 30), () {
  94. tableData[index].textController.selection = TextSelection(
  95. baseOffset: 0,
  96. extentOffset: tableData[index].textController.text.length,
  97. );
  98. });
  99. });
  100. }
  101. }
  102. }
  103. @override
  104. void onInit() {
  105. super.onInit();
  106. // 根据 tableDataConfig 初始化 tableData
  107. tableDataConfig.forEach((element) {
  108. tableData.add(TableElementData(element));
  109. });
  110. }
  111. @override
  112. void onReady() {
  113. super.onReady();
  114. }
  115. @override
  116. void onClose() {
  117. super.onClose();
  118. }
  119. }
  120. /// 表格配置项 (入参)
  121. class TableElementConfig {
  122. final String id;
  123. final String name;
  124. final String unit;
  125. final String? initValue;
  126. TableElementConfig(
  127. {required this.id,
  128. required this.name,
  129. required this.unit,
  130. this.initValue});
  131. }
  132. /// 表格输入结果 (出参)
  133. class TableInputResult {
  134. Map<String, String> data;
  135. TableInputResult({required this.data});
  136. }
  137. /// 表格元素数据(UI数据)
  138. class TableElementData {
  139. final TableElementConfig config;
  140. final TextEditingController textController;
  141. FocusNode focusNode = FocusNode();
  142. TableElementData(this.config)
  143. : textController = TextEditingController(text: config.initValue);
  144. }