import 'package:fis_common/index.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:vitalapp/components/dialog_input.dart'; import 'package:vitalapp/pages/check/models/form.dart'; import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart'; import 'package:vitalapp/pages/form/form_info.dart'; ///输入组件,和ExamText的弹窗放在了内部 class ExamTextInput extends StatefulWidget { const ExamTextInput({ super.key, required this.currentFormObject, this.isNumber = false, }); final FormObject currentFormObject; final bool isNumber; @override State createState() => _ExamInputState(); } class _ExamInputState extends State { String _currentValue = ''; bool _isDisabledValue = false; bool _isHidden = false; @override void initState() { if (FormInfo.instance.formValue.containsKey(widget.currentFormObject.key)) { _currentValue = FormInfo.instance.formValue[widget.currentFormObject.key]; } super.initState(); FormInfo.instance.onChangeTargetValue.addListener(_onChangeTargetValue); } @override void dispose() { FormInfo.instance.onChangeTargetValue.removeListener(_onChangeTargetValue); super.dispose(); } @override Widget build(BuildContext context) { if (_isHidden) { return SizedBox(); } return ExamCard( title: widget.currentFormObject.label ?? '', color: _isDisabledValue ? Colors.grey[300]! : Colors.white, clickCard: _clickCard, content: Container( alignment: Alignment.bottomRight, padding: const EdgeInsets.only( right: 30, left: 40, ), child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ RichText( text: TextSpan( style: TextStyle( fontSize: 26, color: Colors.black, fontFamily: "NotoSansSC", fontFamilyFallback: const ["NotoSansSC"], ), text: _currentValue, children: [ TextSpan( text: widget.currentFormObject.append ?? '', style: const TextStyle( fontSize: 26, fontFamily: "NotoSansSC", fontFamilyFallback: const ["NotoSansSC"], ), ) ], ), ), ])), ); } Future _clickCard() async { if (_isDisabledValue) { return; } List? inputFormatters; TextInputType? keyboardType; if (widget.isNumber) { inputFormatters = [ FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*')), ]; keyboardType = TextInputType.numberWithOptions(decimal: true); } String? result = await VDialogInput( title: widget.currentFormObject.label, initialValue: _currentValue, inputFormatters: inputFormatters, keyboardType: keyboardType, ).show(); if (result?.isNotEmpty ?? false) { FormInfo.instance.formValue[widget.currentFormObject.key!] = result; setState(() { _currentValue = result!; }); FormInfo.instance.onValueChange.emit( this, UpdateFormArgs( sourceKey: widget.currentFormObject.key ?? '', sourceValue: result, ), ); } } void _onChangeTargetValue(Object sender, TargetFormArgs e) { if (e.targetKey != widget.currentFormObject.key) { return; } if (e.isHidden) { setState(() { _isHidden = true; }); } else { setState(() { _isHidden = false; }); } if (e.isDisabledValue) { setState(() { _isDisabledValue = true; _currentValue = e.targetValue; }); } else if (!e.isDisabledValue && _isDisabledValue) { setState(() { _isDisabledValue = false; }); } if (e.targetValue.isNotNullOrEmpty) { setState(() { _currentValue = e.targetValue; }); } } }