static_text.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  2. import 'package:fis_lib_report/report/static_text.dart';
  3. import 'package:fis_lib_report/report_info/report_info.dart';
  4. import 'package:fis_lib_report/report_info/static_text_info.dart';
  5. import 'package:fis_ui/index.dart';
  6. import 'package:flutter/material.dart';
  7. class FStaticText extends StatefulWidget implements FWidget {
  8. const FStaticText(this.staticText, {Key? key}) : super(key: key);
  9. final StaticText staticText;
  10. @override
  11. State<StatefulWidget> createState() {
  12. return _FStaticTextState();
  13. }
  14. }
  15. class _FStaticTextState extends State<FStaticText> {
  16. _FStaticTextState();
  17. double _fontSize = 15.0;
  18. TextStyle _style = const TextStyle();
  19. Color _fontColor = Colors.black;
  20. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  21. EdgeInsets _margin = const EdgeInsets.all(0);
  22. StaticTextInfo? _staticTextInfo;
  23. @override
  24. initState() {
  25. super.initState();
  26. }
  27. @override
  28. FWidget build(BuildContext context) {
  29. initDatas();
  30. String text =
  31. _staticTextInfo?.text.replaceAll(RegExp(r'[\r\n]+'), '') ?? '';
  32. List<FWidget> emptySpaces = [];
  33. final containsSpace = text.contains(' ');
  34. RegExp regex = RegExp(r"^\s+");
  35. var match = regex.firstMatch(text);
  36. int preTextSpacesConut = 0;
  37. if (match != null) {
  38. String spaces = match.group(0) ?? '';
  39. preTextSpacesConut = spaces.length;
  40. }
  41. if (containsSpace) {
  42. int trailingSpaces = text.length - text.trimRight().length;
  43. if (trailingSpaces >= 30) {
  44. text = text.trim();
  45. emptySpaces.add(FText(text));
  46. emptySpaces.add(const FExpanded(child: FSizedBox()));
  47. } else if (preTextSpacesConut >= 5 && !text.contains('检查医师')) {
  48. ///这个检查医师是为了兼容“苏州阳光护理院”这个模板中的空格
  49. emptySpaces.add(const FExpanded(child: FSizedBox()));
  50. text = text.trim();
  51. emptySpaces.add(FText(text));
  52. } else {
  53. for (var i = 0; i < text.length; i++) {
  54. if (text[i] != ' ') {
  55. emptySpaces.add(
  56. FText(
  57. text[i],
  58. style: _style,
  59. ),
  60. );
  61. } else {
  62. emptySpaces.add(FSizedBox(
  63. width: _fontSize > 20 ? 15 : 7.5,
  64. ));
  65. }
  66. }
  67. }
  68. }
  69. var aliment = CrossAxisAlignment.start;
  70. double? width = widget.staticText.lineWidth! > 0
  71. ? PtToPxConverter.ptToPx(widget.staticText.lineWidth!) - 3
  72. : null;
  73. if (widget.staticText.lineLength != null &&
  74. widget.staticText.lineLength! < 5 &&
  75. width != null) {
  76. if (widget.staticText.lineLength! <= 3 &&
  77. _fontSize <= 10.5 &&
  78. width > 90) {
  79. width = 60;
  80. } else {
  81. width = width - 2;
  82. }
  83. }
  84. return FColumn(
  85. mainAxisAlignment: MainAxisAlignment.center,
  86. crossAxisAlignment: aliment,
  87. children: [
  88. FContainer(
  89. margin: _margin,
  90. width: width,
  91. child: containsSpace
  92. ? FRow(mainAxisSize: MainAxisSize.min, children: emptySpaces)
  93. : FText(
  94. (text),
  95. style: _style,
  96. ),
  97. ),
  98. ],
  99. );
  100. }
  101. void initDatas() {
  102. final staticTextInfo =
  103. FReportInfo.instance.getElementInfo(widget.staticText);
  104. if (staticTextInfo != null) {
  105. _staticTextInfo = staticTextInfo as StaticTextInfo;
  106. }
  107. _fontSize = widget.staticText.fontSize ?? 15.0;
  108. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  109. final fontColor = widget.staticText.fontColor;
  110. if (fontColor != null) {
  111. _fontColor = Color.fromARGB(
  112. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  113. }
  114. final backgroundColor = widget.staticText.background;
  115. if (backgroundColor != null) {
  116. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  117. backgroundColor.g!, backgroundColor.b!);
  118. if (_backgroundColor == Colors.white) {
  119. _backgroundColor = Colors.transparent;
  120. }
  121. }
  122. final margin = widget.staticText.margin;
  123. if (margin != null) {
  124. _margin = EdgeInsets.only(
  125. top: margin.top ?? 0,
  126. bottom: margin.bottom ?? 0,
  127. left: margin.left ?? 0,
  128. right: margin.right ?? 0);
  129. }
  130. _style = TextStyle(
  131. fontSize: PtToPxConverter.ptToPx(_fontSize < 10 ? 8 : _fontSize),
  132. color: _fontColor,
  133. backgroundColor: _backgroundColor,
  134. );
  135. }
  136. }