fromTextArray static method

Table fromTextArray({
  1. Context? context,
  2. required List<List> data,
  3. EdgeInsetsGeometry cellPadding = const pw.EdgeInsets.all(5),
  4. double cellHeight = 0,
  5. AlignmentGeometry cellAlignment = pw.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 = pw.Alignment.center,
  16. Map<int, AlignmentGeometry>? headerAlignments,
  17. TextStyle? headerStyle,
  18. OnCellFormat? headerFormat,
  19. TableBorder? border = const pw.TableBorder(left: pw.BorderSide(), right: pw.BorderSide(), top: pw.BorderSide(), bottom: pw.BorderSide(), horizontalInside: pw.BorderSide(), verticalInside: pw.BorderSide()),
  20. Map<int, TableColumnWidth>? columnWidths,
  21. TableColumnWidth defaultColumnWidth = const pw.IntrinsicColumnWidth(),
  22. TableWidth tableWidth = pw.TableWidth.max,
  23. BoxDecoration? headerDecoration,
  24. BoxDecoration? headerCellDecoration,
  25. BoxDecoration? rowDecoration,
  26. BoxDecoration? oddRowDecoration,
  27. TextDirection? headerDirection,
  28. TextDirection? tableDirection,
  29. OnCell? cellBuilder,
  30. OnCellTextStyle? textStyleBuilder,
})

Builds a table from data, mirroring pw.TableHelper.fromTextArray.

Implementation

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

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

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

  final rows = <pw.TableRow>[];
  var rowNum = 0;

  if (headers != null) {
    final tableRow = <pw.Widget>[];
    for (final dynamic cell in headers) {
      tableRow.add(
        pw.Container(
          alignment: headerAlignments[tableRow.length] ?? headerAlignment,
          padding: headerPadding,
          decoration: headerCellDecoration,
          constraints: pw.BoxConstraints(minHeight: headerHeight),
          child: cell is pw.Widget
              ? cell
              : BanglaAwareText(
                  headerFormat == null
                      ? cell.toString()
                      : headerFormat(tableRow.length, cell),
                  style: headerStyle,
                  textDirection: headerDirection,
                ),
        ),
      );
    }
    rows.add(
      pw.TableRow(
        repeat: true,
        decoration: headerDecoration,
        children: tableRow,
      ),
    );
    rowNum++;
  }

  final textDirection =
      context == null ? pw.TextDirection.ltr : pw.Directionality.of(context);

  for (final row in data) {
    final tableRow = <pw.Widget>[];
    final isOdd = (rowNum - headerCount) % 2 != 0;

    if (rowNum < headerCount) {
      for (final dynamic cell in row) {
        final align = headerAlignments[tableRow.length] ?? headerAlignment;
        tableRow.add(
          pw.Container(
            alignment: align,
            padding: headerPadding,
            constraints: pw.BoxConstraints(minHeight: headerHeight),
            child: cell is pw.Widget
                ? cell
                : BanglaAwareText(
                    headerFormat == null
                        ? cell.toString()
                        : headerFormat(tableRow.length, cell),
                    style: headerStyle,
                    textAlign: _textAlign(align.resolve(textDirection)),
                    textDirection: headerDirection,
                  ),
          ),
        );
      }
    } else {
      for (final dynamic cell in row) {
        final align = cellAlignments[tableRow.length] ?? cellAlignment;
        tableRow.add(
          pw.Container(
            alignment: align,
            padding: cellPadding,
            constraints: pw.BoxConstraints(minHeight: cellHeight),
            decoration: cellDecoration == null
                ? null
                : cellDecoration(tableRow.length, cell, rowNum),
            child: cell is pw.Widget
                ? cell
                : cellBuilder?.call(tableRow.length, cell, rowNum) ??
                    BanglaAwareText(
                      cellFormat == null
                          ? cell.toString()
                          : cellFormat(tableRow.length, cell),
                      style: textStyleBuilder?.call(
                            tableRow.length,
                            cell,
                            rowNum,
                          ) ??
                          (isOdd ? oddCellStyle : cellStyle),
                      textAlign: _textAlign(align.resolve(textDirection)),
                      textDirection: tableDirection,
                    ),
          ),
        );
      }
    }

    var decoration = isOdd ? oddRowDecoration : rowDecoration;
    if (rowNum < headerCount) decoration = headerDecoration;

    rows.add(
      pw.TableRow(
        repeat: rowNum < headerCount,
        decoration: decoration,
        children: tableRow,
      ),
    );
    rowNum++;
  }

  return pw.Table(
    border: border,
    tableWidth: tableWidth,
    columnWidths: columnWidths,
    defaultColumnWidth: defaultColumnWidth,
    defaultVerticalAlignment: pw.TableCellVerticalAlignment.full,
    children: rows,
  );
}