input_text.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  2. import 'package:fis_lib_report/converts/text_size_converter.dart';
  3. import 'package:fis_lib_report/converts/alignment_convert.dart';
  4. import 'package:fis_lib_report/report/inputText.dart';
  5. import 'package:fis_lib_report/report_info/input_text_info.dart';
  6. import 'package:fis_lib_report/report_info/report_info.dart';
  7. import 'package:flutter/material.dart';
  8. class RInputText extends StatefulWidget {
  9. final InputText inputText;
  10. const RInputText({Key? key, required this.inputText}) : super(key: key);
  11. @override
  12. State<StatefulWidget> createState() {
  13. return _RInputTextState();
  14. }
  15. }
  16. class _RInputTextState extends State<RInputText> {
  17. _RInputTextState();
  18. final _controller = TextEditingController();
  19. final _focusNode = FocusNode();
  20. double? _lineWidth = 136.0;
  21. double? _fontSize = 22.0;
  22. double? _height = 22.0;
  23. bool? _textWrap = false;
  24. Color _fontColor = const Color.fromARGB(255, 0, 0, 0);
  25. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  26. int? _lineLength = 1;
  27. TextStyle? _textStyle;
  28. InputTextInfo? _inputTextInfo;
  29. bool _isReadOnly = false;
  30. String _text = '';
  31. @override
  32. initState() {
  33. ReportInfo.instance.onReloadFinsh.addListener((sender, e) {
  34. _initDatas();
  35. });
  36. super.initState();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. final inputTextInfo = ReportInfo.instance.getElementInfo(widget.inputText);
  41. if (_inputTextInfo != inputTextInfo) {
  42. if (_inputTextInfo != null) _inputTextInfo!.onTextChange.dispose();
  43. _inputTextInfo = inputTextInfo as InputTextInfo;
  44. }
  45. if (!_inputTextInfo!.isListening!) {
  46. _initDatas();
  47. _inputTextInfo!.isListening = true;
  48. }
  49. try {
  50. if (_inputTextInfo!.text != _controller.text) {
  51. _controller.text = _inputTextInfo!.text;
  52. if (_inputTextInfo!.isReadOnly! &&
  53. _inputTextInfo!.tag!.name == 'ReportPhysician') {
  54. _controller.text = ReportInfo.instance.reporter ?? '';
  55. }
  56. }
  57. } catch (e) {
  58. print(e);
  59. }
  60. return Container(
  61. //constraints: BoxConstraints(minWidth: _lineWidth!, maxWidth: 480),
  62. width: _lineWidth,
  63. height: _textWrap! ? _height! * 4 : (_height! - 2),
  64. padding: const EdgeInsets.only(right: 5, left: 5),
  65. decoration: BoxDecoration(
  66. border: Border.all(
  67. width: 1,
  68. color: _isReadOnly ? Colors.transparent : Colors.grey,
  69. ),
  70. color: _backgroundColor,
  71. ),
  72. child: TextField(
  73. focusNode: _focusNode,
  74. readOnly: _isReadOnly,
  75. decoration: null,
  76. textAlignVertical: AlignmentConvert.verticalAlignmentConvert(
  77. widget.inputText.verticalAlignment),
  78. minLines: 1,
  79. maxLines: _textWrap! ? 6 : 1,
  80. controller: _controller,
  81. textAlign: TextAlign.start,
  82. style: _textStyle,
  83. onChanged: (v) {
  84. // _onInputChanged(_textStyle!, v);
  85. },
  86. onEditingComplete: () {
  87. _onInputChanged(_textStyle!, _controller.text);
  88. }),
  89. );
  90. }
  91. //onchange 事件
  92. void _onInputChanged(TextStyle _textStyle, String value) {
  93. final inputText = widget.inputText;
  94. if (_inputTextInfo != null) {
  95. _inputTextInfo!.text = value;
  96. }
  97. _syncWidth(
  98. inputText,
  99. value,
  100. );
  101. }
  102. @override
  103. void dispose() {
  104. _inputTextInfo!.onTextChange.dispose();
  105. _controller.dispose();
  106. super.dispose();
  107. }
  108. void _initDatas() {
  109. final inputText = widget.inputText;
  110. _textStyle = TextStyle(
  111. fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
  112. color: _fontColor,
  113. );
  114. _controller.text = '';
  115. _focusNode.addListener(() {
  116. if (!_focusNode.hasFocus) {
  117. _onInputChanged(_textStyle!, _controller.text);
  118. }
  119. });
  120. _isReadOnly = inputText.isReadOnly!;
  121. final inputTextInfo = ReportInfo.instance.getElementInfo(inputText);
  122. _inputTextInfo = inputTextInfo as InputTextInfo;
  123. _inputTextInfo!.onTextChange.addListener((sender, e) {
  124. setState(() {
  125. _controller.text = e;
  126. _syncWidth(
  127. inputText,
  128. e,
  129. );
  130. });
  131. });
  132. if (_inputTextInfo!.isReadOnly!) {
  133. if (_inputTextInfo!.tag!.name == 'ReportPhysician') {
  134. _controller.text = ReportInfo.instance.reporter ?? '';
  135. }
  136. }
  137. final fontColor = inputText.fontColor;
  138. if (fontColor != null) {
  139. _fontColor = Color.fromARGB(
  140. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  141. }
  142. final backgroundColor = inputText.background;
  143. if (backgroundColor != null) {
  144. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  145. backgroundColor.g!, backgroundColor.b!);
  146. }
  147. //TODO(Loki):set FontName in TextField
  148. final fontName = inputText.fontName;
  149. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  150. final fontStyles = inputText.fontStyles;
  151. _lineLength = inputText.lineLength;
  152. _lineWidth = PtToPxConverter.ptToPx(inputText.lineWidth);
  153. _textWrap = inputText.textWrap;
  154. _fontSize = PtToPxConverter.ptToPx(inputText.fontSize);
  155. _height = _fontSize! > 30 ? 41 : 22;
  156. ReportInfo.instance.onReloadFinsh.addListener((sender, e) {
  157. _initDatas();
  158. });
  159. setState(() {});
  160. }
  161. void _syncWidth(InputText inputText, String value) {
  162. final width = TextSizeConvert.getTextSize(value, _textStyle!).width + 15;
  163. if (inputText.tag!.name == 'HospitalName') {
  164. if (_lineWidth! < width) {
  165. setState(() {
  166. if (width < 480) {
  167. _lineWidth = width;
  168. }
  169. });
  170. } else if (_controller.text.isEmpty) {
  171. setState(() {
  172. //重置长度
  173. _lineWidth = inputText.lineWidth;
  174. });
  175. } else if (TextSizeConvert.getTextSize(_controller.text, _textStyle!)
  176. .width >
  177. inputText.lineWidth!) {
  178. setState(() {
  179. if (width < 480) {
  180. _lineWidth = width;
  181. }
  182. });
  183. }
  184. }
  185. }
  186. }