exam_single_option.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'package:fis_common/index.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:vitalapp/consts/styles.dart';
  4. import 'package:vitalapp/pages/check/models/form.dart';
  5. import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_radio_and_select.dart';
  6. import 'package:vitalapp/pages/check/widgets/exam_title.dart';
  7. import 'package:vitalapp/pages/form/form_info.dart';
  8. class ExamSingleOption extends StatefulWidget {
  9. const ExamSingleOption({
  10. super.key,
  11. required this.currentFormObject,
  12. required this.option,
  13. required this.isSelected,
  14. });
  15. final FormObject currentFormObject;
  16. final Option option;
  17. final bool isSelected;
  18. @override
  19. State<StatefulWidget> createState() {
  20. return ExamSingleOptionState();
  21. }
  22. }
  23. class ExamSingleOptionState extends State<ExamSingleOption> {
  24. FormObject get currentFormObject => widget.currentFormObject;
  25. bool _isSelected = false;
  26. bool _isDisabledValue = false;
  27. @override
  28. void initState() {
  29. _isSelected = widget.isSelected;
  30. if (FormInfo.instance.formValue.containsKey(currentFormObject.parentKey!)) {
  31. var formValue = FormInfo.instance.formValue[currentFormObject.parentKey!];
  32. if (formValue is List) {
  33. _isSelected = formValue.contains(currentFormObject.key);
  34. if (_isSelected)
  35. Future.delayed(Duration(milliseconds: 200), () {
  36. FormInfo.instance.onValueChange.emit(
  37. this,
  38. UpdateFormArgs(
  39. sourceKey: widget.currentFormObject.parentKey ?? '',
  40. sourceValue: currentFormObject.key,
  41. type: UpdateFormType.Add,
  42. ),
  43. );
  44. });
  45. }
  46. }
  47. super.initState();
  48. FormInfo.instance.onChangeTargetValue.addListener(_onChangeTargetValue);
  49. }
  50. @override
  51. void dispose() {
  52. FormInfo.instance.onChangeTargetValue.removeListener(_onChangeTargetValue);
  53. super.dispose();
  54. }
  55. @override
  56. Widget build(BuildContext context) {
  57. return Container(
  58. padding: const EdgeInsets.all(7),
  59. child: InkWell(
  60. onTap: () => selectRaidoChange(),
  61. borderRadius: BorderRadius.circular(8),
  62. child: Card(
  63. elevation: 6,
  64. shape: RoundedRectangleBorder(
  65. borderRadius: GlobalStyles.borderRadius,
  66. ),
  67. child: Material(
  68. color: _isDisabledValue ? Colors.grey[300] : Colors.white,
  69. borderRadius: GlobalStyles.borderRadius,
  70. child: Ink(
  71. decoration: BoxDecoration(
  72. border: Border.all(
  73. color: _isSelected ? Colors.blue : Colors.black26,
  74. ),
  75. borderRadius: const BorderRadius.all(
  76. Radius.circular(8),
  77. ),
  78. color: _isSelected ? Colors.blue : Colors.transparent,
  79. ),
  80. child: Container(
  81. padding: const EdgeInsets.all(15),
  82. alignment: Alignment.center,
  83. width: 250,
  84. child: FittedBox(
  85. child: Text(
  86. widget.option.label ?? '',
  87. style: TextStyle(
  88. fontSize: 20,
  89. color: _isSelected ? Colors.white : Colors.black54,
  90. ),
  91. ),
  92. ),
  93. ),
  94. ),
  95. ),
  96. ),
  97. ),
  98. );
  99. }
  100. void selectRaidoChange() {
  101. if (_isDisabledValue) {
  102. return;
  103. }
  104. setState(() {
  105. _isSelected = !_isSelected;
  106. });
  107. onValueChange();
  108. }
  109. ///当前选中的值改变
  110. void onValueChange() {
  111. UpdateFormType type = UpdateFormType.Add;
  112. if (_isSelected) {
  113. if (FormInfo.instance.formValue
  114. .containsKey(currentFormObject.parentKey!)) {
  115. var sourceValue =
  116. FormInfo.instance.formValue[currentFormObject.parentKey!] as List;
  117. sourceValue.add(currentFormObject.key!);
  118. } else {
  119. FormInfo.instance.formValue[currentFormObject.parentKey!] = [
  120. currentFormObject.key
  121. ];
  122. }
  123. type = UpdateFormType.Add;
  124. } else if (FormInfo.instance.formValue
  125. .containsKey(currentFormObject.parentKey!)) {
  126. FormInfo.instance.formValue.remove(currentFormObject.parentKey!);
  127. type = UpdateFormType.Remove;
  128. }
  129. FormInfo.instance.onValueChange.emit(
  130. this,
  131. UpdateFormArgs(
  132. sourceKey: widget.currentFormObject.parentKey ?? '',
  133. sourceValue: currentFormObject.key,
  134. type: type,
  135. ),
  136. );
  137. }
  138. ///其他控件的改变、通知到本控件
  139. void _onChangeTargetValue(Object sender, TargetFormArgs e) {
  140. if (e.targetKey != widget.currentFormObject.key) {
  141. return;
  142. }
  143. if (e.isDisabledValue) {
  144. setState(() {
  145. _isSelected = false;
  146. _isDisabledValue = true;
  147. });
  148. } else if (!e.isDisabledValue) {
  149. setState(() {
  150. _isDisabledValue = false;
  151. });
  152. }
  153. if (e.targetValue == "cancelSelection" && _isSelected) {
  154. setState(() {
  155. _isSelected = false;
  156. });
  157. }
  158. }
  159. }