fromTextArray static method

Table 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,
})

Implementation

static Table 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,
}) {
  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, Alignment>{};
  headerAlignments ??= cellAlignments;

  final rows = <TableRow>[];

  var rowNum = 0;
  if (headers != null) {
    final tableRow = <Widget>[];

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

  final textDirection =
      context == null ? TextDirection.ltr : Directionality.of(context);
  for (final row in data) {
    final tableRow = <Widget>[];
    final isOdd = (rowNum - headerCount) % 2 != 0;
    if (rowNum < headerCount) {
      for (final dynamic cell in row) {
        final align = headerAlignments[tableRow.length] ?? headerAlignment;
        final textAlign = _textAlign(align.resolve(textDirection));

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

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

    rows.add(TableRow(
      children: tableRow,
      repeat: rowNum < headerCount,
      decoration: decoration,
    ));
    rowNum++;
  }
  return Table(
    border: border,
    tableWidth: tableWidth,
    children: rows,
    columnWidths: columnWidths,
    defaultColumnWidth: defaultColumnWidth,
    defaultVerticalAlignment: TableCellVerticalAlignment.full,
  );
}