input_text.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/element_info.dart';
  6. import 'package:fis_lib_report/report_info/input_text_info.dart';
  7. import 'package:fis_lib_report/report_info/report_info.dart';
  8. import 'package:flutter/material.dart';
  9. class RInputText extends StatefulWidget {
  10. final InputText inputText;
  11. const RInputText({Key? key, required this.inputText}) : super(key: key);
  12. @override
  13. State<StatefulWidget> createState() {
  14. return _RInputTextState();
  15. }
  16. }
  17. class _RInputTextState extends State<RInputText> {
  18. _RInputTextState();
  19. late final InputText inputText;
  20. final _controller = TextEditingController();
  21. final _focusNode = FocusNode();
  22. double? _lineWidth = 136.0;
  23. double? _fontSize = 22.0;
  24. double? _height = 22.0;
  25. bool? _textWrap = false;
  26. Color _fontColor = const Color.fromARGB(255, 0, 0, 0);
  27. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  28. int? _lineLength = 1;
  29. TextStyle? _textStyle;
  30. InputTextInfo? _inputTextInfo;
  31. @override
  32. initState() {
  33. inputText = widget.inputText;
  34. final inputTextInfo = ReportInfo.instance.getElementInfo(inputText);
  35. _inputTextInfo = inputTextInfo as InputTextInfo;
  36. if (inputText.isReadOnly!) {
  37. _controller.text = 'test';
  38. }
  39. final fontColor = inputText.fontColor;
  40. if (fontColor != null) {
  41. _fontColor = Color.fromARGB(
  42. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  43. }
  44. final backgroundColor = inputText.background;
  45. if (backgroundColor != null) {
  46. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  47. backgroundColor.g!, backgroundColor.b!);
  48. }
  49. _textStyle = TextStyle(
  50. fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
  51. color: _fontColor,
  52. );
  53. //TODO(Loki):set FontName in TextField
  54. final fontName = inputText.fontName;
  55. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  56. final fontStyles = inputText.fontStyles;
  57. _lineLength = inputText.lineLength;
  58. _lineWidth = inputText.lineWidth;
  59. _textWrap = inputText.textWrap;
  60. _fontSize = PtToPxConverter.ptToPx(inputText.fontSize);
  61. _height = _fontSize! > 30 ? 36.5 : 22;
  62. super.initState();
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. final _textStyle = TextStyle(
  67. fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
  68. color: _fontColor,
  69. );
  70. return Container(
  71. width: PtToPxConverter.ptToPx(_lineWidth!),
  72. height: _textWrap! ? _height! * 4 : (_height!),
  73. padding: EdgeInsets.only(top: _textWrap! ? 10 : 5, right: 5, left: 5),
  74. decoration: BoxDecoration(
  75. border: Border.all(
  76. width: 1,
  77. color: inputText.isReadOnly! ? Colors.transparent : Colors.grey,
  78. ),
  79. color: _backgroundColor,
  80. ),
  81. child: TextField(
  82. focusNode: _focusNode,
  83. readOnly: inputText.isReadOnly ?? false,
  84. // cursorHeight: _height!,
  85. decoration: InputDecoration.collapsed(
  86. hintText: '',
  87. hintStyle: TextStyle(
  88. fontSize: _fontSize,
  89. color: Colors.black54,
  90. ),
  91. fillColor: _backgroundColor,
  92. filled: false,
  93. ).copyWith(
  94. contentPadding: EdgeInsets.symmetric(vertical: _textWrap! ? 2.5 : 4),
  95. ),
  96. textAlignVertical: AlignmentConvert.verticalAlignmentConvert(
  97. inputText.verticalAlignment),
  98. minLines: 1,
  99. maxLines: _textWrap! ? 6 : 1,
  100. controller: _controller,
  101. textAlign: TextAlign.start,
  102. style: _textStyle,
  103. onChanged: (v) {
  104. _onInputChanged(_textStyle, v);
  105. },
  106. ),
  107. );
  108. }
  109. //onchange 事件
  110. void _onInputChanged(TextStyle _textStyle, String value) {
  111. if (_inputTextInfo != null) {
  112. _inputTextInfo!.text = value;
  113. }
  114. final width = TextSizeConvert.getTextSize(value, _textStyle).width;
  115. // ignore: todo
  116. //TODO(LOki):此处需要区分不同的输入框
  117. if (inputText.tag!.name == 'HospitalName') {
  118. if (_lineWidth! < width) {
  119. setState(() {
  120. if (width < 480) {
  121. _lineWidth = width;
  122. }
  123. });
  124. } else if (_controller.text.isEmpty) {
  125. setState(() {
  126. //重置长度
  127. _lineWidth = inputText.lineWidth;
  128. });
  129. } else if (TextSizeConvert.getTextSize(_controller.text, _textStyle)
  130. .width >
  131. inputText.lineWidth!) {
  132. setState(() {
  133. if (width < 480) {
  134. _lineWidth = width;
  135. }
  136. });
  137. }
  138. }
  139. }
  140. }