fromTextArray static method
Table
fromTextArray({
- Context? context,
- required List<
List> data, - EdgeInsetsGeometry cellPadding = const pw.EdgeInsets.all(5),
- double cellHeight = 0,
- AlignmentGeometry cellAlignment = pw.Alignment.topLeft,
- Map<
int, AlignmentGeometry> ? cellAlignments, - TextStyle? cellStyle,
- TextStyle? oddCellStyle,
- OnCellFormat? cellFormat,
- OnCellDecoration? cellDecoration,
- int headerCount = 1,
- List? headers,
- EdgeInsetsGeometry? headerPadding,
- double? headerHeight,
- AlignmentGeometry headerAlignment = pw.Alignment.center,
- Map<
int, AlignmentGeometry> ? headerAlignments, - TextStyle? headerStyle,
- OnCellFormat? headerFormat,
- 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, TableColumnWidth> ? columnWidths, - TableColumnWidth defaultColumnWidth = const pw.IntrinsicColumnWidth(),
- TableWidth tableWidth = pw.TableWidth.max,
- BoxDecoration? headerDecoration,
- BoxDecoration? headerCellDecoration,
- BoxDecoration? rowDecoration,
- BoxDecoration? oddRowDecoration,
- TextDirection? headerDirection,
- TextDirection? tableDirection,
- OnCell? cellBuilder,
- 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,
);
}