rt_table.dart 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import 'dart:math';
  2. import 'package:fis_lib_report/converts/alignment_convert.dart';
  3. import 'package:fis_lib_report/converts/color_convert.dart';
  4. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  5. import 'package:fis_lib_report/pages/components/static_text.dart';
  6. import 'package:fis_lib_report/pages/rt_cell.dart';
  7. import 'package:fis_lib_report/report/cell_postion.dart';
  8. import 'package:fis_lib_report/report/element_type.dart';
  9. import 'package:fis_lib_report/report/interfaces/cell.dart';
  10. import 'package:fis_lib_report/report/paragraph.dart';
  11. import 'package:fis_lib_report/report/rt_Cell.dart';
  12. import 'package:fis_lib_report/report/rt_border.dart';
  13. import 'package:fis_lib_report/report/rt_table.dart';
  14. import 'package:fis_lib_report/report/static_image.dart';
  15. import 'package:fis_ui/index.dart';
  16. import 'package:flutter/cupertino.dart';
  17. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  18. class FRTTablePage extends StatefulWidget implements FWidget {
  19. const FRTTablePage({required this.element, Key? key}) : super(key: key);
  20. final RTTable element;
  21. @override
  22. State<StatefulWidget> createState() {
  23. return _RTTableState();
  24. }
  25. }
  26. class _RTTableState extends State<FRTTablePage> {
  27. Map<CellPostion, ICell>? _cells;
  28. List<ICell>? _values = [];
  29. int? _columnCount;
  30. int? _rowCount;
  31. double _width = 0;
  32. double _height = 0;
  33. @override
  34. initState() {
  35. initCells();
  36. super.initState();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. ///这个Map用于表格结构
  41. Map<CellPostion, FWidget> gridList = {};
  42. ///这个children 用于普通RTTable
  43. List<FWidget> children = [];
  44. for (int i = 0; i < _rowCount!; i++) {
  45. final rowChildren =
  46. _cells!.keys.where((element) => element.row! == i).toList();
  47. // FWidget row = _buildRowWidget(i);
  48. // rowWidgets.add(row);
  49. var rowItems = rowChildren.toList();
  50. ///排序:按照所在列从0开始排
  51. rowItems.sort(((a, b) => (a.column ?? 0).compareTo(b.column ?? 0)));
  52. List<FWidget> rowNormalChildren = [];
  53. for (var cellPostion in rowItems) {
  54. ///一行元素集合中的一个元素
  55. FWidget childrenItem = _buildChildrenItem(cellPostion, rowItems, i);
  56. gridList[cellPostion] = childrenItem;
  57. rowNormalChildren.add(FContainer(
  58. child: childrenItem,
  59. ));
  60. }
  61. children.add(
  62. FRow(
  63. children: rowNormalChildren,
  64. ),
  65. );
  66. }
  67. var columnCount = _columnCount ?? 3;
  68. if (_width <= 0) {
  69. initCells();
  70. }
  71. // var keys = gridList.keys.toList();
  72. // keys.sort(((a, b) {
  73. // return a.row!.compareTo(b.row!);
  74. // }));
  75. // List<CellPostion> sortKeys = [];
  76. // for (int i = 0; i < _rowCount! - 1; i++) {
  77. // final columnItems = keys.where((element) => element.row == i).toList();
  78. // columnItems.sort((a, b) => a.column!.compareTo(b.column!));
  79. // sortKeys.addAll(columnItems);
  80. // }
  81. // print('sssssstart:');
  82. // if (_columnCount! == 12) {
  83. // for (var s in sortKeys) {
  84. // print('row:${s.row},column:${s.column}');
  85. // }
  86. // }
  87. // print('eeeeeeeend');
  88. const int expansionFactor = 1000;
  89. Widget table = StaggeredGrid.count(
  90. ///水平方向
  91. crossAxisCount: columnCount * expansionFactor,
  92. mainAxisSpacing: 0,
  93. children: gridList.keys.map((e) {
  94. if (gridList.keys.length == 24) {
  95. print('');
  96. }
  97. var currentItemWidth =
  98. widget.element.columnDefinitions![e.column!].width ?? 0;
  99. if (e.columnSpan! > 1) {
  100. for (int i = e.column! + 1; i < e.column! + e.columnSpan!; i++) {
  101. currentItemWidth += widget.element.columnDefinitions![i].width ?? 0;
  102. }
  103. }
  104. var currentItemHeight =
  105. widget.element.rowDefinitions![e.row!].height ?? 0;
  106. if (e.rowSpan! > 1 &&
  107. e.row! + 1 < widget.element.rowDefinitions!.length) {
  108. for (int i = e.row! + 1; i < e.row! + e.rowSpan!; i++) {
  109. var targetHeight = widget.element.rowDefinitions![i].height ?? 0;
  110. currentItemHeight += targetHeight;
  111. }
  112. }
  113. double currentColumnRatio = 0;
  114. double currentRowRatio = 0;
  115. if (_width > 0) {
  116. currentColumnRatio =
  117. currentItemWidth / _width * expansionFactor * columnCount;
  118. } else {
  119. currentColumnRatio = e.columnSpan! * expansionFactor.toDouble();
  120. }
  121. if (_height > 0) {
  122. currentRowRatio =
  123. currentItemHeight / _width * expansionFactor * columnCount;
  124. } else {
  125. currentRowRatio = e.rowSpan!.toDouble() * expansionFactor;
  126. }
  127. print('width:$currentColumnRatio,currentRowRatio:$currentRowRatio');
  128. return StaggeredGridTile.count(
  129. ///水平方向
  130. crossAxisCellCount: currentColumnRatio.toInt(),
  131. ///竖直方向
  132. mainAxisCellCount: currentRowRatio,
  133. child: Container(
  134. child: gridList[e]!,
  135. ),
  136. );
  137. }).toList(),
  138. );
  139. return Container(
  140. alignment: AlignmentConvert.horizontalConvert(
  141. widget.element.horizontalAlignment),
  142. child: table,
  143. );
  144. }
  145. void initCells() {
  146. _cells = widget.element.cells ?? {};
  147. List<CellPostion> cellPostions = _cells!.keys.toList();
  148. cellPostions.sort(((a, b) => (b.column!).compareTo(a.column!)));
  149. // +1 为列数
  150. _columnCount = cellPostions.first.column! + 1;
  151. _values = _cells!.values.toList();
  152. for (int i = 0; i < _columnCount!; i++) {
  153. _width += widget.element.columnDefinitions![i].width!;
  154. }
  155. cellPostions.sort(((a, b) => (b.row!).compareTo(a.row!)));
  156. // +1 为行数
  157. _rowCount = cellPostions.first.row! + 1;
  158. for (int i = 0; i < _rowCount!; i++) {
  159. _height += widget.element.rowDefinitions![i].height!;
  160. }
  161. }
  162. FWidget _buildChildrenItem(
  163. CellPostion cellPostion,
  164. List<CellPostion> rowItems,
  165. int index,
  166. ) {
  167. double width =
  168. widget.element.columnDefinitions![cellPostion.column!].width ?? 0.0;
  169. double height =
  170. widget.element.rowDefinitions![cellPostion.row!].height ?? 0.0;
  171. ///columnSpan是横向合并列数的
  172. if (cellPostion.columnSpan! > 1) {
  173. for (int i = 1; i < cellPostion.columnSpan!; i++) {
  174. width +=
  175. widget.element.columnDefinitions![cellPostion.column! + i].width ??
  176. 0.0;
  177. }
  178. }
  179. ///rowSpan是竖向合并的行数
  180. if (cellPostion.rowSpan! > 1) {
  181. for (int i = 1; i < cellPostion.rowSpan!; i++) {
  182. height +=
  183. widget.element.rowDefinitions![cellPostion.row! + i - 1].height ??
  184. 0.0;
  185. }
  186. }
  187. final cell = _cells![cellPostion];
  188. RTCell rtCell = cell as RTCell;
  189. final borders = rtCell.borders!;
  190. final background = rtCell.background;
  191. var childBlock = rtCell.blocks ?? [];
  192. if (childBlock.isNotEmpty) {
  193. for (var element in childBlock) {
  194. if (element.elementType!.name == ElementType.paragraph.name) {
  195. var para = element as Paragraph;
  196. var paraEle = para.elements ?? [];
  197. if (paraEle.isNotEmpty) {
  198. for (var ele in paraEle) {
  199. ///这里是修正静态图像的尺寸的
  200. if (ele is StaticImage) {
  201. if (ele.imageWidth > width) {
  202. width = ele.imageWidth;
  203. }
  204. if (height < ele.imageHeight) {
  205. height = ele.imageHeight;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. var realWidth = PtToPxConverter.ptToPx(width);
  214. var realHeight = PtToPxConverter.ptToPx(height);
  215. FWidget cellItem = FContainer(
  216. decoration: BoxDecoration(
  217. color: ColorConvert.colorConvert(background),
  218. border: Border(
  219. left: BorderSide(
  220. style: borders.left.borderStyle == RTBorderStyle.None
  221. ? BorderStyle.none
  222. : BorderStyle.solid,
  223. width: borders.left.thickness,
  224. color: ColorConvert.colorConvert(borders.left.color)),
  225. right: BorderSide(
  226. style: borders.right.borderStyle == RTBorderStyle.None
  227. ? BorderStyle.none
  228. : BorderStyle.solid,
  229. width: borders.right.thickness,
  230. color: ColorConvert.colorConvert(borders.right.color)),
  231. top: BorderSide(
  232. style: borders.top.borderStyle == RTBorderStyle.None
  233. ? BorderStyle.none
  234. : BorderStyle.solid,
  235. width: borders.top.thickness,
  236. color: ColorConvert.colorConvert(borders.top.color)),
  237. bottom: BorderSide(
  238. style: borders.bottom.borderStyle == RTBorderStyle.None
  239. ? BorderStyle.none
  240. : BorderStyle.solid,
  241. width: borders.bottom.thickness,
  242. color: ColorConvert.colorConvert(borders.bottom.color)),
  243. ),
  244. ),
  245. //height: realHeight,
  246. width: realWidth,
  247. child: FRTCellPage(
  248. cell: rtCell,
  249. width: realWidth,
  250. ),
  251. );
  252. return cellItem;
  253. }
  254. FWidget _buildRowWidget(int index) {
  255. ///当前行所有元素
  256. final rowChildren = _cells!.keys.where((element) => element.row! == index);
  257. List<FWidget> childwidgets = [];
  258. var rowItems = rowChildren.toList();
  259. ///排序:按照所在列从0开始排
  260. rowItems.sort(((a, b) => (a.column ?? 0).compareTo(b.column ?? 0)));
  261. for (var cellPostion in rowItems) {
  262. ///一行元素集合中的一个元素
  263. FWidget childrenItem = _buildChildrenItem(cellPostion, rowItems, index);
  264. childwidgets.add(childrenItem);
  265. }
  266. final row = FRow(
  267. children: childwidgets,
  268. );
  269. return row;
  270. }
  271. }
  272. class Element {
  273. int row;
  274. int column;
  275. Element(this.row, this.column);
  276. }