input_text.dart 4.3 KB

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