static_text.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:fis_lib_report/converts/alignment_convert.dart';
  4. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  5. import 'package:fis_lib_report/converts/text_size_converter.dart';
  6. import 'package:fis_lib_report/report/staticText.dart';
  7. import 'package:flutter/material.dart';
  8. class RStaticText extends StatefulWidget {
  9. RStaticText(this.staticText);
  10. final StaticText? staticText;
  11. @override
  12. State<StatefulWidget> createState() {
  13. return _RStaticTextState(staticText!);
  14. }
  15. }
  16. class _RStaticTextState extends State<RStaticText> {
  17. final StaticText staticText;
  18. _RStaticTextState(this.staticText);
  19. double _fontSize = 15.0;
  20. TextStyle _style = const TextStyle();
  21. Color _fontColor = Colors.black;
  22. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  23. EdgeInsets _margin = const EdgeInsets.all(0);
  24. @override
  25. initState() {
  26. _fontSize = staticText.fontSize ?? 15.0;
  27. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  28. //final fontStyles = staticText.fontStyles;
  29. final fontColor = staticText.fontColor;
  30. if (fontColor != null) {
  31. _fontColor = Color.fromARGB(
  32. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  33. }
  34. final backgroundColor = staticText.background;
  35. if (backgroundColor != null) {
  36. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  37. backgroundColor.g!, backgroundColor.b!);
  38. }
  39. final margin = staticText.margin;
  40. if (margin != null) {
  41. _margin = EdgeInsets.only(
  42. top: margin.top ?? 0,
  43. bottom: margin.bottom ?? 0,
  44. left: margin.left ?? 0,
  45. right: margin.right ?? 0);
  46. }
  47. _style = TextStyle(
  48. fontSize: PtToPxConverter.ptToPx(_fontSize),
  49. color: _fontColor,
  50. backgroundColor: _backgroundColor,
  51. );
  52. super.initState();
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. final text = staticText.text!;
  57. return Container(
  58. margin: _margin,
  59. child: Text(
  60. _syncText(text),
  61. style: _style,
  62. ),
  63. );
  64. }
  65. String _syncText(String value) {
  66. try {
  67. if (value.contains(' ')) {
  68. String result = '';
  69. for (var i = 0; i < value.length; i++) {
  70. if (value[i] == ' ') {
  71. result += value[i] + ' ';
  72. } else {
  73. result += value[i];
  74. }
  75. }
  76. return result;
  77. } else {
  78. return value;
  79. }
  80. } catch (e) {
  81. print(e);
  82. return value;
  83. }
  84. }
  85. }