Table.fromTextArray constructor

Table.fromTextArray({
  1. List<String>? headers,
  2. required List<List<String>> data,
  3. bool border = true,
  4. TableBorder? tableBorder,
  5. List<double>? columnWidths,
  6. Map<int, TableColumnWidth>? columnWidthSpec,
  7. TextStyle? headerStyle,
  8. TextStyle? cellStyle,
  9. TextStyle? oddCellStyle,
  10. AlignmentGeometry headerAlignment = Alignment.center,
  11. AlignmentGeometry cellAlignment = AlignmentDirectional.topStart,
  12. Map<int, AlignmentGeometry>? cellAlignments,
  13. Map<int, AlignmentGeometry>? headerAlignments,
  14. EdgeInsets cellPadding = const EdgeInsets.all(5),
  15. EdgeInsets? headerPadding,
  16. double cellHeight = 0,
  17. double? headerHeight,
  18. String? headerDecoration,
  19. String? headerCellDecoration,
  20. String? rowDecoration,
  21. String? oddRowDecoration,
  22. TableCellVerticalAlignment verticalAlignment = TableCellVerticalAlignment.full,
})

fromTextArray API — mirrors package:pdf TableHelper.fromTextArray.

Implementation

factory Table.fromTextArray({
  List<String>? headers,
  required List<List<String>> data,
  bool border = true,
  TableBorder? tableBorder,
  List<double>? columnWidths,
  Map<int, TableColumnWidth>? columnWidthSpec,
  TextStyle? headerStyle,
  TextStyle? cellStyle,
  TextStyle? oddCellStyle,
  AlignmentGeometry headerAlignment = Alignment.center,
  AlignmentGeometry cellAlignment = AlignmentDirectional.topStart,
  Map<int, AlignmentGeometry>? cellAlignments,
  Map<int, AlignmentGeometry>? headerAlignments,
  EdgeInsets cellPadding = const EdgeInsets.all(5),
  EdgeInsets? headerPadding,
  double cellHeight = 0,
  double? headerHeight,
  String? headerDecoration,
  String? headerCellDecoration,
  String? rowDecoration,
  String? oddRowDecoration,
  TableCellVerticalAlignment verticalAlignment =
      TableCellVerticalAlignment.full,
}) {
  headerPadding ??= cellPadding;
  headerHeight ??= cellHeight;
  // Match package:pdf: only zebra when the caller asks for it.
  final String? oddBand = oddRowDecoration;
  final String? evenBand = rowDecoration;
  oddCellStyle ??= cellStyle;
  final Map<int, AlignmentGeometry> cellAlign =
      cellAlignments ?? const <int, AlignmentGeometry>{};
  final Map<int, AlignmentGeometry> headerAlign =
      headerAlignments ?? cellAlign;

  final List<TableRow> rows = <TableRow>[];
  if (headers != null) {
    rows.add(
      TableRow(
        repeat: true,
        decoration: BoxDecoration(
          color: headerDecoration ?? '1A237E',
        ),
        verticalAlignment: verticalAlignment,
        children: <Widget>[
          for (int i = 0; i < headers.length; i++)
            Container(
              color: headerCellDecoration,
              padding: headerPadding,
              alignment: headerAlign[i] ?? headerAlignment,
              constraints: headerHeight > 0
                  ? BoxConstraints(minHeight: headerHeight)
                  : null,
              child: Text(
                headers[i],
                style:
                    headerStyle ??
                    const TextStyle(
                      color: 'FFFFFF',
                      fontWeight: FontWeight.bold,
                      fontSize: 10,
                    ),
              ),
            ),
        ],
      ),
    );
  }
  for (int r = 0; r < data.length; r++) {
    final bool odd = r.isOdd;
    final String? band = odd ? oddBand : evenBand;
    rows.add(
      TableRow(
        decoration: band == null ? null : BoxDecoration(color: band),
        verticalAlignment: verticalAlignment,
        children: <Widget>[
          for (int c = 0; c < data[r].length; c++)
            Container(
              padding: cellPadding,
              alignment: cellAlign[c] ?? cellAlignment,
              constraints: cellHeight > 0
                  ? BoxConstraints(minHeight: cellHeight)
                  : null,
              child: Text(
                data[r][c],
                style: (odd ? oddCellStyle : cellStyle) ??
                    const TextStyle(fontSize: 10),
              ),
            ),
        ],
      ),
    );
  }
  return Table(
    children: rows,
    border: border,
    tableBorder: tableBorder,
    columnWidths: columnWidths,
    columnWidthSpec: columnWidthSpec,
    borderColor: '90A4AE',
    defaultVerticalAlignment: verticalAlignment,
  );
}