123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import 'dart:math';
- import 'package:fis_lib_report/converts/alignment_convert.dart';
- import 'package:fis_lib_report/converts/color_convert.dart';
- import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
- import 'package:fis_lib_report/pages/components/static_text.dart';
- import 'package:fis_lib_report/pages/rt_cell.dart';
- import 'package:fis_lib_report/report/cell_postion.dart';
- import 'package:fis_lib_report/report/element_type.dart';
- import 'package:fis_lib_report/report/interfaces/cell.dart';
- import 'package:fis_lib_report/report/paragraph.dart';
- import 'package:fis_lib_report/report/rt_Cell.dart';
- import 'package:fis_lib_report/report/rt_border.dart';
- import 'package:fis_lib_report/report/rt_table.dart';
- import 'package:fis_lib_report/report/static_image.dart';
- import 'package:fis_ui/index.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
- class FRTTablePage extends StatefulWidget implements FWidget {
- const FRTTablePage({required this.element, Key? key}) : super(key: key);
- final RTTable element;
- @override
- State<StatefulWidget> createState() {
- return _RTTableState();
- }
- }
- class _RTTableState extends State<FRTTablePage> {
- Map<CellPostion, ICell>? _cells;
- List<ICell>? _values = [];
- int? _columnCount;
- int? _rowCount;
- double _width = 0;
- double _height = 0;
- @override
- initState() {
- initCells();
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- ///这个Map用于表格结构
- Map<CellPostion, FWidget> gridList = {};
- ///这个children 用于普通RTTable
- List<FWidget> children = [];
- for (int i = 0; i < _rowCount!; i++) {
- final rowChildren =
- _cells!.keys.where((element) => element.row! == i).toList();
- // FWidget row = _buildRowWidget(i);
- // rowWidgets.add(row);
- var rowItems = rowChildren.toList();
- ///排序:按照所在列从0开始排
- rowItems.sort(((a, b) => (a.column ?? 0).compareTo(b.column ?? 0)));
- List<FWidget> rowNormalChildren = [];
- for (var cellPostion in rowItems) {
- ///一行元素集合中的一个元素
- FWidget childrenItem = _buildChildrenItem(cellPostion, rowItems, i);
- gridList[cellPostion] = childrenItem;
- rowNormalChildren.add(FContainer(
- child: childrenItem,
- ));
- }
- children.add(
- FRow(
- children: rowNormalChildren,
- ),
- );
- }
- var columnCount = _columnCount ?? 3;
- if (_width <= 0) {
- initCells();
- }
- // var keys = gridList.keys.toList();
- // keys.sort(((a, b) {
- // return a.row!.compareTo(b.row!);
- // }));
- // List<CellPostion> sortKeys = [];
- // for (int i = 0; i < _rowCount! - 1; i++) {
- // final columnItems = keys.where((element) => element.row == i).toList();
- // columnItems.sort((a, b) => a.column!.compareTo(b.column!));
- // sortKeys.addAll(columnItems);
- // }
- // print('sssssstart:');
- // if (_columnCount! == 12) {
- // for (var s in sortKeys) {
- // print('row:${s.row},column:${s.column}');
- // }
- // }
- // print('eeeeeeeend');
- const int expansionFactor = 1000;
- Widget table = StaggeredGrid.count(
- ///水平方向
- crossAxisCount: columnCount * expansionFactor,
- mainAxisSpacing: 0,
- children: gridList.keys.map((e) {
- if (gridList.keys.length == 24) {
- print('');
- }
- var currentItemWidth =
- widget.element.columnDefinitions![e.column!].width ?? 0;
- if (e.columnSpan! > 1) {
- for (int i = e.column! + 1; i < e.column! + e.columnSpan!; i++) {
- currentItemWidth += widget.element.columnDefinitions![i].width ?? 0;
- }
- }
- var currentItemHeight =
- widget.element.rowDefinitions![e.row!].height ?? 0;
- if (e.rowSpan! > 1 &&
- e.row! + 1 < widget.element.rowDefinitions!.length) {
- for (int i = e.row! + 1; i < e.row! + e.rowSpan!; i++) {
- var targetHeight = widget.element.rowDefinitions![i].height ?? 0;
- currentItemHeight += targetHeight;
- }
- }
- double currentColumnRatio = 0;
- double currentRowRatio = 0;
- if (_width > 0) {
- currentColumnRatio =
- currentItemWidth / _width * expansionFactor * columnCount;
- } else {
- currentColumnRatio = e.columnSpan! * expansionFactor.toDouble();
- }
- if (_height > 0) {
- currentRowRatio =
- currentItemHeight / _width * expansionFactor * columnCount;
- } else {
- currentRowRatio = e.rowSpan!.toDouble() * expansionFactor;
- }
- print('width:$currentColumnRatio,currentRowRatio:$currentRowRatio');
- return StaggeredGridTile.count(
- ///水平方向
- crossAxisCellCount: currentColumnRatio.toInt(),
- ///竖直方向
- mainAxisCellCount: currentRowRatio,
- child: Container(
- child: gridList[e]!,
- ),
- );
- }).toList(),
- );
- return Container(
- alignment: AlignmentConvert.horizontalConvert(
- widget.element.horizontalAlignment),
- child: table,
- );
- }
- void initCells() {
- _cells = widget.element.cells ?? {};
- List<CellPostion> cellPostions = _cells!.keys.toList();
- cellPostions.sort(((a, b) => (b.column!).compareTo(a.column!)));
- // +1 为列数
- _columnCount = cellPostions.first.column! + 1;
- _values = _cells!.values.toList();
- for (int i = 0; i < _columnCount!; i++) {
- _width += widget.element.columnDefinitions![i].width!;
- }
- cellPostions.sort(((a, b) => (b.row!).compareTo(a.row!)));
- // +1 为行数
- _rowCount = cellPostions.first.row! + 1;
- for (int i = 0; i < _rowCount!; i++) {
- _height += widget.element.rowDefinitions![i].height!;
- }
- }
- FWidget _buildChildrenItem(
- CellPostion cellPostion,
- List<CellPostion> rowItems,
- int index,
- ) {
- double width =
- widget.element.columnDefinitions![cellPostion.column!].width ?? 0.0;
- double height =
- widget.element.rowDefinitions![cellPostion.row!].height ?? 0.0;
- ///columnSpan是横向合并列数的
- if (cellPostion.columnSpan! > 1) {
- for (int i = 1; i < cellPostion.columnSpan!; i++) {
- width +=
- widget.element.columnDefinitions![cellPostion.column! + i].width ??
- 0.0;
- }
- }
- ///rowSpan是竖向合并的行数
- if (cellPostion.rowSpan! > 1) {
- for (int i = 1; i < cellPostion.rowSpan!; i++) {
- height +=
- widget.element.rowDefinitions![cellPostion.row! + i - 1].height ??
- 0.0;
- }
- }
- final cell = _cells![cellPostion];
- RTCell rtCell = cell as RTCell;
- final borders = rtCell.borders!;
- final background = rtCell.background;
- var childBlock = rtCell.blocks ?? [];
- if (childBlock.isNotEmpty) {
- for (var element in childBlock) {
- if (element.elementType!.name == ElementType.paragraph.name) {
- var para = element as Paragraph;
- var paraEle = para.elements ?? [];
- if (paraEle.isNotEmpty) {
- for (var ele in paraEle) {
- ///这里是修正静态图像的尺寸的
- if (ele is StaticImage) {
- if (ele.imageWidth > width) {
- width = ele.imageWidth;
- }
- if (height < ele.imageHeight) {
- height = ele.imageHeight;
- }
- }
- }
- }
- }
- }
- }
- var realWidth = PtToPxConverter.ptToPx(width);
- var realHeight = PtToPxConverter.ptToPx(height);
- FWidget cellItem = FContainer(
- decoration: BoxDecoration(
- color: ColorConvert.colorConvert(background),
- border: Border(
- left: BorderSide(
- style: borders.left.borderStyle == RTBorderStyle.None
- ? BorderStyle.none
- : BorderStyle.solid,
- width: borders.left.thickness,
- color: ColorConvert.colorConvert(borders.left.color)),
- right: BorderSide(
- style: borders.right.borderStyle == RTBorderStyle.None
- ? BorderStyle.none
- : BorderStyle.solid,
- width: borders.right.thickness,
- color: ColorConvert.colorConvert(borders.right.color)),
- top: BorderSide(
- style: borders.top.borderStyle == RTBorderStyle.None
- ? BorderStyle.none
- : BorderStyle.solid,
- width: borders.top.thickness,
- color: ColorConvert.colorConvert(borders.top.color)),
- bottom: BorderSide(
- style: borders.bottom.borderStyle == RTBorderStyle.None
- ? BorderStyle.none
- : BorderStyle.solid,
- width: borders.bottom.thickness,
- color: ColorConvert.colorConvert(borders.bottom.color)),
- ),
- ),
- //height: realHeight,
- width: realWidth,
- child: FRTCellPage(
- cell: rtCell,
- width: realWidth,
- ),
- );
- return cellItem;
- }
- FWidget _buildRowWidget(int index) {
- ///当前行所有元素
- final rowChildren = _cells!.keys.where((element) => element.row! == index);
- List<FWidget> childwidgets = [];
- var rowItems = rowChildren.toList();
- ///排序:按照所在列从0开始排
- rowItems.sort(((a, b) => (a.column ?? 0).compareTo(b.column ?? 0)));
- for (var cellPostion in rowItems) {
- ///一行元素集合中的一个元素
- FWidget childrenItem = _buildChildrenItem(cellPostion, rowItems, index);
- childwidgets.add(childrenItem);
- }
- final row = FRow(
- children: childwidgets,
- );
- return row;
- }
- }
- class Element {
- int row;
- int column;
- Element(this.row, this.column);
- }
|