input_text.dart 4.7 KB

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