input_text.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/element_tag.dart';
  5. import 'package:fis_lib_report/report/inputText.dart';
  6. import 'package:fis_lib_report/report_info/element_tag_names.dart';
  7. import 'package:fis_lib_report/report_info/input_text_info.dart';
  8. import 'package:fis_lib_report/report_info/report_info.dart';
  9. import 'package:flutter/material.dart';
  10. class RInputText extends StatefulWidget {
  11. final InputText inputText;
  12. const RInputText({Key? key, required this.inputText}) : super(key: key);
  13. @override
  14. State<StatefulWidget> createState() {
  15. return _RInputTextState();
  16. }
  17. }
  18. class _RInputTextState extends State<RInputText> {
  19. _RInputTextState();
  20. final _controller = TextEditingController();
  21. final _focusNode = FocusNode();
  22. double _lineWidth = 136.0;
  23. double _fontSize = 22.0;
  24. double _height = 22.0;
  25. double _allHeight = 0.0;
  26. bool _textWrap = false;
  27. Color _fontColor = const Color.fromARGB(255, 0, 0, 0);
  28. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  29. int _lineLength = 1;
  30. TextStyle? _textStyle;
  31. InputTextInfo? _inputTextInfo;
  32. bool _isReadOnly = false;
  33. bool _widthLock = false;
  34. int _rowCount = 1;
  35. @override
  36. initState() {
  37. super.initState();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. _checkInputTextInfo();
  42. _initStyle();
  43. return Column(
  44. children: [
  45. Container(
  46. width: _lineWidth,
  47. height: _allHeight,
  48. padding: const EdgeInsets.only(right: 5, left: 5),
  49. decoration: BoxDecoration(
  50. border: Border.all(
  51. width: 1,
  52. color: _isReadOnly ? Colors.transparent : Colors.grey,
  53. ),
  54. color: _backgroundColor,
  55. ),
  56. child: TextField(
  57. focusNode: _focusNode,
  58. readOnly: _isReadOnly,
  59. decoration: null,
  60. textAlignVertical: AlignmentConvert.verticalAlignmentConvert(
  61. widget.inputText.verticalAlignment),
  62. minLines: 1,
  63. maxLines: _rowCount,
  64. controller: _controller,
  65. textAlign: TextAlign.start,
  66. style: _textStyle,
  67. onChanged: (v) {
  68. // _onInputChanged(_textStyle!, _controller.text);
  69. },
  70. onEditingComplete: () {
  71. _onInputChanged(_textStyle!, _controller.text);
  72. }),
  73. ),
  74. const SizedBox(height: 3),
  75. if (_inputTextInfo!.tag != null &&
  76. _inputTextInfo!.tag!.elementTagType ==
  77. ElementTagType.DiagnosticEntry) ...[
  78. Row(
  79. mainAxisAlignment: MainAxisAlignment.end,
  80. children: [
  81. ElevatedButton(
  82. onPressed: () {
  83. ReportInfo.instance.onDiagnosticTap
  84. .emit(this, _inputTextInfo!.tag!.name ?? '');
  85. },
  86. child: const Text('选择词条'),
  87. ),
  88. const SizedBox(width: 10),
  89. ElevatedButton(
  90. onPressed: () {
  91. setState(() {
  92. _inputTextInfo!.text = '';
  93. });
  94. },
  95. child: const Text('撤销'),
  96. ),
  97. const SizedBox(width: 5),
  98. ],
  99. ),
  100. ],
  101. ],
  102. );
  103. }
  104. //onchange 事件
  105. void _onInputChanged(TextStyle _textStyle, String value) {
  106. if (_inputTextInfo != null) {
  107. _inputTextInfo!.text = value;
  108. }
  109. }
  110. @override
  111. void dispose() {
  112. _inputTextInfo!.onTextChange.dispose();
  113. _controller.dispose();
  114. super.dispose();
  115. }
  116. void _initDatas() {
  117. final inputText = widget.inputText;
  118. _controller.text = '';
  119. _focusNode.addListener(() {
  120. if (!_focusNode.hasFocus) {
  121. _onInputChanged(_textStyle!, _controller.text);
  122. }
  123. });
  124. final inputTextInfo = ReportInfo.instance.getElementInfo(inputText);
  125. _inputTextInfo = inputTextInfo as InputTextInfo;
  126. _inputTextInfo!.onTextChange.addListener((sender, e) {
  127. setState(() {
  128. _controller.text = e;
  129. _syncWidth(
  130. inputText,
  131. e,
  132. );
  133. });
  134. });
  135. if (_inputTextInfo!.isReadOnly!) {
  136. if (_inputTextInfo!.tag!.name == TagNames.RPHYSICIAN) {
  137. _controller.text = ReportInfo.instance.reporter ?? '';
  138. }
  139. }
  140. final fontColor = inputText.fontColor;
  141. if (fontColor != null) {
  142. _fontColor = Color.fromARGB(
  143. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  144. }
  145. final backgroundColor = inputText.background;
  146. if (backgroundColor != null) {
  147. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  148. backgroundColor.g!, backgroundColor.b!);
  149. }
  150. ReportInfo.instance.onReloadFinsh.addListener((sender, e) {
  151. _initDatas();
  152. });
  153. setState(() {});
  154. }
  155. void _syncWidth(InputText inputText, String value) {
  156. final width = TextSizeConvert.getTextSize(value, _textStyle!).width + 15;
  157. if (inputText.tag!.name == TagNames.HOSPITALNAME) {
  158. if (_lineWidth < width) {
  159. setState(() {
  160. if (width < 480) {
  161. _lineWidth = width;
  162. _widthLock = true;
  163. }
  164. });
  165. } else if (_controller.text.isEmpty) {
  166. setState(() {
  167. //重置长度
  168. _lineWidth = PtToPxConverter.ptToPx(inputText.lineWidth);
  169. _widthLock = false;
  170. });
  171. } else if (TextSizeConvert.getTextSize(_controller.text, _textStyle!)
  172. .width >
  173. inputText.lineWidth!) {
  174. setState(() {
  175. if (width < 480) {
  176. _lineWidth = width;
  177. _widthLock = true;
  178. }
  179. });
  180. }
  181. }
  182. }
  183. void _initStyle() {
  184. final inputText = widget.inputText;
  185. _textStyle ??= TextStyle(
  186. fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
  187. color: _fontColor,
  188. );
  189. _isReadOnly = inputText.isReadOnly!;
  190. //TODO(Loki):set FontName in TextField
  191. final fontName = inputText.fontName;
  192. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  193. final fontStyles = inputText.fontStyles;
  194. _lineLength = inputText.lineLength ?? 7;
  195. if (!_widthLock) {
  196. _lineWidth = PtToPxConverter.ptToPx(inputText.lineWidth);
  197. }
  198. _widthLock = false;
  199. _textWrap = inputText.textWrap ?? false;
  200. _rowCount = _textWrap ? 6 : 1;
  201. if (_inputTextInfo != null && _inputTextInfo!.text.isNotEmpty) {
  202. final text = _inputTextInfo!.text;
  203. if (text.contains('\r\n')) {
  204. final rowList = text.split('\r\n');
  205. _rowCount = rowList.length > _rowCount ? rowList.length : _rowCount;
  206. }
  207. }
  208. _fontSize = PtToPxConverter.ptToPx(inputText.fontSize);
  209. _height = _fontSize > 30 ? 44 : 22;
  210. _allHeight = _textWrap
  211. ? _height * (_rowCount - 1)
  212. : _fontSize > 30
  213. ? (_height - 2)
  214. : _height;
  215. if (_inputTextInfo != null && _textWrap) {
  216. final height =
  217. TextSizeConvert.getTextSize(_controller.text, _textStyle!).height + 5;
  218. if (height > _allHeight) {
  219. _allHeight = height;
  220. }
  221. }
  222. }
  223. void _checkInputTextInfo() {
  224. try {
  225. final inputTextInfo =
  226. ReportInfo.instance.getElementInfo(widget.inputText);
  227. if (_inputTextInfo != inputTextInfo) {
  228. if (_inputTextInfo != null) _inputTextInfo!.onTextChange.dispose();
  229. _inputTextInfo = inputTextInfo as InputTextInfo;
  230. }
  231. if (!_inputTextInfo!.isListening!) {
  232. _initDatas();
  233. _inputTextInfo!.isListening = true;
  234. }
  235. if (_inputTextInfo!.text != _controller.text) {
  236. _controller.text = _inputTextInfo!.text;
  237. }
  238. } catch (e) {
  239. print(e);
  240. }
  241. }
  242. }