SpanningTable.fromTextArray constructor

SpanningTable.fromTextArray({
  1. Context? context,
  2. required List<List> data,
  3. EdgeInsetsGeometry cellPadding = const EdgeInsets.all(5),
  4. double cellHeight = 0,
  5. AlignmentGeometry cellAlignment = Alignment.topLeft,
  6. Map<int, AlignmentGeometry>? cellAlignments,
  7. TextStyle? cellStyle,
  8. TextStyle? oddCellStyle,
  9. OnCellFormat? cellFormat,
  10. OnCellDecoration? cellDecoration,
  11. int headerCount = 1,
  12. List? headers,
  13. EdgeInsetsGeometry? headerPadding,
  14. double? headerHeight,
  15. AlignmentGeometry headerAlignment = Alignment.center,
  16. Map<int, AlignmentGeometry>? headerAlignments,
  17. TextStyle? headerStyle,
  18. OnCellFormat? headerFormat,
  19. TableBorder? border = const TableBorder(left: BorderSide(), right: BorderSide(), top: BorderSide(), bottom: BorderSide(), horizontalInside: BorderSide(), verticalInside: BorderSide()),
  20. Map<int, TableColumnWidth>? columnWidths,
  21. TableColumnWidth defaultColumnWidth = const IntrinsicColumnWidth(),
  22. TableWidth tableWidth = TableWidth.max,
  23. BoxDecoration? headerDecoration,
  24. BoxDecoration? headerCellDecoration,
  25. BoxDecoration? rowDecoration,
  26. BoxDecoration? oddRowDecoration,
  27. TextDirection? headerDirection,
  28. TextDirection? tableDirection,
  29. SpanningTableCellBuilder? cellBuilder,
  30. SpanningTableCellTextStyleBuilder? textStyleBuilder,
  31. TableCellVerticalAlignment defaultVerticalAlignment = TableCellVerticalAlignment.full,
  32. SpanningTableOverflowMode overflow = SpanningTableOverflowMode.strict,
  33. TextDirection? textDirection,
  34. bool repeatHeaderRows = true,
})

Implementation

factory SpanningTable.fromTextArray({
  Context? context,
  required List<List<dynamic>> data,
  EdgeInsetsGeometry cellPadding = const EdgeInsets.all(5),
  double cellHeight = 0,
  AlignmentGeometry cellAlignment = Alignment.topLeft,
  Map<int, AlignmentGeometry>? cellAlignments,
  TextStyle? cellStyle,
  TextStyle? oddCellStyle,
  OnCellFormat? cellFormat,
  OnCellDecoration? cellDecoration,
  int headerCount = 1,
  List<dynamic>? headers,
  EdgeInsetsGeometry? headerPadding,
  double? headerHeight,
  AlignmentGeometry headerAlignment = Alignment.center,
  Map<int, AlignmentGeometry>? headerAlignments,
  TextStyle? headerStyle,
  OnCellFormat? headerFormat,
  TableBorder? border = const TableBorder(
    left: BorderSide(),
    right: BorderSide(),
    top: BorderSide(),
    bottom: BorderSide(),
    horizontalInside: BorderSide(),
    verticalInside: BorderSide(),
  ),
  Map<int, TableColumnWidth>? columnWidths,
  TableColumnWidth defaultColumnWidth = const IntrinsicColumnWidth(),
  TableWidth tableWidth = TableWidth.max,
  BoxDecoration? headerDecoration,
  BoxDecoration? headerCellDecoration,
  BoxDecoration? rowDecoration,
  BoxDecoration? oddRowDecoration,
  TextDirection? headerDirection,
  TextDirection? tableDirection,
  SpanningTableCellBuilder? cellBuilder,
  SpanningTableCellTextStyleBuilder? textStyleBuilder,
  TableCellVerticalAlignment defaultVerticalAlignment =
      TableCellVerticalAlignment.full,
  SpanningTableOverflowMode overflow = SpanningTableOverflowMode.strict,
  TextDirection? textDirection,
  bool repeatHeaderRows = true,
}) {
  assert(headerCount >= 0);

  if (context != null) {
    final theme = Theme.of(context);
    headerStyle ??= theme.tableHeader;
    cellStyle ??= theme.tableCell;
  }

  headerPadding ??= cellPadding;
  headerHeight ??= cellHeight;
  oddRowDecoration ??= rowDecoration;
  oddCellStyle ??= cellStyle;
  cellAlignments ??= const <int, AlignmentGeometry>{};
  headerAlignments ??= cellAlignments;

  final rows = <SpanningTableRow>[];
  final effectiveDirection = textDirection ??
      context?.dependsOn<InheritedDirectionality>()?.textDirection ??
      TextDirection.ltr;

  SpanningTableCell buildCell({
    required dynamic cell,
    required int index,
    required int rowNum,
    required bool header,
    required bool odd,
  }) {
    if (cell is SpanningTableCell) {
      return cell;
    }

    final align = header
        ? headerAlignments![index] ?? headerAlignment
        : cellAlignments![index] ?? cellAlignment;
    final resolvedAlign = align.resolve(effectiveDirection);
    final textAlign = _textAlign(resolvedAlign);
    final padding = header ? headerPadding! : cellPadding;
    final minHeight = header ? headerHeight! : cellHeight;
    final decoration = header
        ? headerCellDecoration
        : cellDecoration?.call(index, cell, rowNum);
    final child = cell is Widget
        ? cell
        : cellBuilder?.call(index, cell, rowNum) ??
            Text(
              header
                  ? headerFormat == null
                      ? cell.toString()
                      : headerFormat(index, cell)
                  : cellFormat == null
                      ? cell.toString()
                      : cellFormat(index, cell),
              style: header
                  ? headerStyle
                  : textStyleBuilder?.call(index, cell, rowNum) ??
                      (odd ? oddCellStyle : cellStyle),
              textAlign: textAlign,
              textDirection: header ? headerDirection : tableDirection,
            );

    return SpanningTableCell(
      alignment: align,
      padding: padding,
      decoration: decoration,
      child: minHeight > 0
          ? ConstrainedBox(
              constraints: BoxConstraints(minHeight: minHeight),
              child: child,
            )
          : child,
    );
  }

  var rowNum = 0;
  if (headers != null) {
    rows.add(
      SpanningTableRow(
        repeat: repeatHeaderRows,
        decoration: headerDecoration,
        children: <SpanningTableCell>[
          for (var index = 0; index < headers.length; index++)
            buildCell(
              cell: headers[index],
              index: index,
              rowNum: rowNum,
              header: true,
              odd: false,
            ),
        ],
      ),
    );
    rowNum++;
  }

  for (final row in data) {
    final isHeader = rowNum < headerCount;
    final isOdd = (rowNum - headerCount) % 2 != 0;
    rows.add(
      SpanningTableRow(
        repeat: repeatHeaderRows && isHeader,
        decoration: isHeader
            ? headerDecoration
            : isOdd
                ? oddRowDecoration
                : rowDecoration,
        children: <SpanningTableCell>[
          for (var index = 0; index < row.length; index++)
            buildCell(
              cell: row[index],
              index: index,
              rowNum: rowNum,
              header: isHeader,
              odd: isOdd,
            ),
        ],
      ),
    );
    rowNum++;
  }

  return SpanningTable(
    border: border,
    tableWidth: tableWidth,
    children: rows,
    columnWidths: columnWidths,
    defaultColumnWidth: defaultColumnWidth,
    defaultVerticalAlignment: defaultVerticalAlignment,
    overflow: overflow,
    textDirection: textDirection,
  );
}