exam_text_input.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import 'package:fis_common/index.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:vitalapp/components/dialog_input.dart';
  5. import 'package:vitalapp/pages/check/models/form.dart';
  6. import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
  7. import 'package:vitalapp/pages/form/form_info.dart';
  8. ///输入组件,和ExamText的弹窗放在了内部
  9. class ExamTextInput extends StatefulWidget {
  10. const ExamTextInput({
  11. super.key,
  12. required this.currentFormObject,
  13. this.isNumber = false,
  14. });
  15. final FormObject currentFormObject;
  16. final bool isNumber;
  17. @override
  18. State<ExamTextInput> createState() => _ExamInputState();
  19. }
  20. class _ExamInputState extends State<ExamTextInput> {
  21. String _currentValue = '';
  22. bool _isDisabledValue = false;
  23. bool _isHidden = false;
  24. @override
  25. void initState() {
  26. if (FormInfo.instance.formValue.containsKey(widget.currentFormObject.key)) {
  27. _currentValue = FormInfo.instance.formValue[widget.currentFormObject.key];
  28. }
  29. super.initState();
  30. FormInfo.instance.onChangeTargetValue.addListener(_onChangeTargetValue);
  31. }
  32. @override
  33. void dispose() {
  34. FormInfo.instance.onChangeTargetValue.removeListener(_onChangeTargetValue);
  35. super.dispose();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. if (_isHidden) {
  40. return SizedBox();
  41. }
  42. return ExamCard(
  43. title: widget.currentFormObject.label ?? '',
  44. color: _isDisabledValue ? Colors.grey[300]! : Colors.white,
  45. clickCard: _clickCard,
  46. content: Container(
  47. alignment: Alignment.bottomRight,
  48. padding: const EdgeInsets.only(
  49. right: 30,
  50. left: 40,
  51. ),
  52. child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
  53. RichText(
  54. text: TextSpan(
  55. style: TextStyle(
  56. fontSize: 26,
  57. color: Colors.black,
  58. fontFamily: "NotoSansSC",
  59. fontFamilyFallback: const ["NotoSansSC"],
  60. ),
  61. text: _currentValue,
  62. children: [
  63. TextSpan(
  64. text: widget.currentFormObject.append ?? '',
  65. style: const TextStyle(
  66. fontSize: 26,
  67. fontFamily: "NotoSansSC",
  68. fontFamilyFallback: const ["NotoSansSC"],
  69. ),
  70. )
  71. ],
  72. ),
  73. ),
  74. ])),
  75. );
  76. }
  77. Future<void> _clickCard() async {
  78. if (_isDisabledValue) {
  79. return;
  80. }
  81. List<TextInputFormatter>? inputFormatters;
  82. TextInputType? keyboardType;
  83. if (widget.isNumber) {
  84. inputFormatters = [
  85. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*')),
  86. ];
  87. keyboardType = TextInputType.numberWithOptions(decimal: true);
  88. }
  89. String? result = await VDialogInput(
  90. title: widget.currentFormObject.label,
  91. initialValue: _currentValue,
  92. inputFormatters: inputFormatters,
  93. keyboardType: keyboardType,
  94. ).show();
  95. if (result?.isNotEmpty ?? false) {
  96. FormInfo.instance.formValue[widget.currentFormObject.key!] = result;
  97. setState(() {
  98. _currentValue = result!;
  99. });
  100. FormInfo.instance.onValueChange.emit(
  101. this,
  102. UpdateFormArgs(
  103. sourceKey: widget.currentFormObject.key ?? '',
  104. sourceValue: result,
  105. ),
  106. );
  107. }
  108. }
  109. void _onChangeTargetValue(Object sender, TargetFormArgs e) {
  110. if (e.targetKey != widget.currentFormObject.key) {
  111. return;
  112. }
  113. if (e.isHidden) {
  114. setState(() {
  115. _isHidden = true;
  116. });
  117. } else {
  118. setState(() {
  119. _isHidden = false;
  120. });
  121. }
  122. if (e.isDisabledValue) {
  123. setState(() {
  124. _isDisabledValue = true;
  125. _currentValue = e.targetValue;
  126. });
  127. } else if (!e.isDisabledValue && _isDisabledValue) {
  128. setState(() {
  129. _isDisabledValue = false;
  130. });
  131. }
  132. if (e.targetValue.isNotNullOrEmpty) {
  133. setState(() {
  134. _currentValue = e.targetValue;
  135. });
  136. }
  137. }
  138. }