input_text.dart 8.0 KB

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