David PHAM-VAN

Move Table.fromTextArray to TableHelper.fromTextArray

... ... @@ -256,22 +256,25 @@ Future<Uint8List> generateDocument(
pw.Paragraph(
text:
'The PDF file format has changed several times, and continues to evolve, along with the release of new versions of Adobe Acrobat. There have been nine versions of PDF and the corresponding version of the software:'),
pw.Table.fromTextArray(context: context, data: const <List<String>>[
<String>['Date', 'PDF Version', 'Acrobat Version'],
<String>['1993', 'PDF 1.0', 'Acrobat 1'],
<String>['1994', 'PDF 1.1', 'Acrobat 2'],
<String>['1996', 'PDF 1.2', 'Acrobat 3'],
<String>['1999', 'PDF 1.3', 'Acrobat 4'],
<String>['2001', 'PDF 1.4', 'Acrobat 5'],
<String>['2003', 'PDF 1.5', 'Acrobat 6'],
<String>['2005', 'PDF 1.6', 'Acrobat 7'],
<String>['2006', 'PDF 1.7', 'Acrobat 8'],
<String>['2008', 'PDF 1.7', 'Acrobat 9'],
<String>['2009', 'PDF 1.7', 'Acrobat 9.1'],
<String>['2010', 'PDF 1.7', 'Acrobat X'],
<String>['2012', 'PDF 1.7', 'Acrobat XI'],
<String>['2017', 'PDF 2.0', 'Acrobat DC'],
]),
pw.TableHelper.fromTextArray(
context: context,
data: const <List<String>>[
<String>['Date', 'PDF Version', 'Acrobat Version'],
<String>['1993', 'PDF 1.0', 'Acrobat 1'],
<String>['1994', 'PDF 1.1', 'Acrobat 2'],
<String>['1996', 'PDF 1.2', 'Acrobat 3'],
<String>['1999', 'PDF 1.3', 'Acrobat 4'],
<String>['2001', 'PDF 1.4', 'Acrobat 5'],
<String>['2003', 'PDF 1.5', 'Acrobat 6'],
<String>['2005', 'PDF 1.6', 'Acrobat 7'],
<String>['2006', 'PDF 1.7', 'Acrobat 8'],
<String>['2008', 'PDF 1.7', 'Acrobat 9'],
<String>['2009', 'PDF 1.7', 'Acrobat 9.1'],
<String>['2010', 'PDF 1.7', 'Acrobat X'],
<String>['2012', 'PDF 1.7', 'Acrobat XI'],
<String>['2017', 'PDF 2.0', 'Acrobat DC'],
],
),
pw.Padding(padding: const pw.EdgeInsets.all(10)),
pw.Paragraph(
text:
... ...
... ... @@ -451,7 +451,7 @@ class Invoice {
'Total'
];
return pw.Table.fromTextArray(
return pw.TableHelper.fromTextArray(
border: null,
cellAlignment: pw.Alignment.centerLeft,
headerDecoration: pw.BoxDecoration(
... ...
... ... @@ -150,7 +150,7 @@ Future<Uint8List> generateReport(
);
// Data table
final table = pw.Table.fromTextArray(
final table = pw.TableHelper.fromTextArray(
border: null,
headers: tableHeaders,
data: List<List<dynamic>>.generate(
... ...
... ... @@ -9,6 +9,7 @@
- Improve verbose output
- Allow saving an unmodified document
- Table cell: dynamic widget [Shahriyar Aghajani]
- Move Table.fromTextArray to TableHelper.fromTextArray
## 3.10.1
... ...
... ... @@ -20,16 +20,7 @@ import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
import '../../pdf.dart';
import 'box_border.dart';
import 'container.dart';
import 'decoration.dart';
import 'flex.dart';
import 'geometry.dart';
import 'multi_page.dart';
import 'text.dart';
import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';
import '../../widgets.dart';
/// A horizontal group of cells in a [Table].
@immutable
... ... @@ -244,6 +235,7 @@ class Table extends Widget with SpanningWidget {
this.tableWidth = TableWidth.max,
}) : super();
@Deprecated('Use TextHelper.fromTextArray() instead.')
factory Table.fromTextArray({
Context? context,
required List<List<dynamic>> data,
... ... @@ -278,121 +270,35 @@ class Table extends Widget with SpanningWidget {
BoxDecoration? headerCellDecoration,
BoxDecoration? rowDecoration,
BoxDecoration? oddRowDecoration,
}) {
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: Text(
headerFormat == null
? cell.toString()
: headerFormat(tableRow.length, cell),
style: headerStyle,
),
),
);
}
rows.add(TableRow(
children: tableRow,
repeat: true,
decoration: headerDecoration,
));
rowNum++;
}
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);
tableRow.add(
Container(
alignment: align,
padding: headerPadding,
constraints: BoxConstraints(minHeight: headerHeight),
child: Text(
headerFormat == null
? cell.toString()
: headerFormat(tableRow.length, cell),
style: headerStyle,
textAlign: textAlign,
),
),
);
}
} 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 String
? Text(
cellFormat == null
? cell.toString()
: cellFormat(tableRow.length, cell),
style: isOdd ? oddCellStyle : cellStyle,
textAlign: _textAlign(align))
: (cell is Widget ? cell : null),
),
);
}
}
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,
);
}
}) =>
TableHelper.fromTextArray(
context: context,
data: data,
cellPadding: cellPadding,
cellHeight: cellHeight,
cellAlignment: cellAlignment,
cellAlignments: cellAlignments,
cellStyle: cellStyle,
oddCellStyle: oddCellStyle,
cellFormat: cellFormat,
cellDecoration: cellDecoration,
headerCount: headerCount = 1,
headers: headers,
headerPadding: headerPadding,
headerHeight: headerHeight,
headerAlignment: headerAlignment,
headerAlignments: headerAlignments,
headerStyle: headerStyle,
headerFormat: headerFormat,
border: border,
columnWidths: columnWidths,
defaultColumnWidth: defaultColumnWidth,
tableWidth: tableWidth,
headerDecoration: headerDecoration,
headerCellDecoration: headerCellDecoration,
rowDecoration: rowDecoration,
oddRowDecoration: oddRowDecoration,
);
@override
bool get canSpan => true;
... ... @@ -672,14 +578,4 @@ class Table extends Widget with SpanningWidget {
? _heights[heightIndex]
: 0.0;
}
static TextAlign _textAlign(Alignment align) {
if (align.x == 0) {
return TextAlign.center;
} else if (align.x < 0) {
return TextAlign.left;
} else {
return TextAlign.right;
}
}
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'box_border.dart';
import 'container.dart';
import 'decoration.dart';
import 'geometry.dart';
import 'table.dart';
import 'text.dart';
import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';
mixin TableHelper {
static TextAlign _textAlign(Alignment align) {
if (align.x == 0) {
return TextAlign.center;
} else if (align.x < 0) {
return TextAlign.left;
} else {
return TextAlign.right;
}
}
static Table fromTextArray({
Context? context,
required List<List<dynamic>> data,
EdgeInsets cellPadding = const EdgeInsets.all(5),
double cellHeight = 0,
Alignment cellAlignment = Alignment.topLeft,
Map<int, Alignment>? cellAlignments,
TextStyle? cellStyle,
TextStyle? oddCellStyle,
OnCellFormat? cellFormat,
OnCellDecoration? cellDecoration,
int headerCount = 1,
List<dynamic>? headers,
EdgeInsets? headerPadding,
double? headerHeight,
Alignment headerAlignment = Alignment.center,
Map<int, Alignment>? 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,
}) {
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: Text(
headerFormat == null
? cell.toString()
: headerFormat(tableRow.length, cell),
style: headerStyle,
),
),
);
}
rows.add(TableRow(
children: tableRow,
repeat: true,
decoration: headerDecoration,
));
rowNum++;
}
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);
tableRow.add(
Container(
alignment: align,
padding: headerPadding,
constraints: BoxConstraints(minHeight: headerHeight),
child: Text(
headerFormat == null
? cell.toString()
: headerFormat(tableRow.length, cell),
style: headerStyle,
textAlign: textAlign,
),
),
);
}
} 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)),
),
);
}
}
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,
);
}
}
... ...
... ... @@ -54,6 +54,7 @@ export 'src/widgets/shape.dart';
export 'src/widgets/stack.dart';
export 'src/widgets/svg.dart';
export 'src/widgets/table.dart';
export 'src/widgets/table_helper.dart';
export 'src/widgets/text.dart';
export 'src/widgets/text_style.dart';
export 'src/widgets/theme.dart';
... ...
... ... @@ -190,7 +190,7 @@ void main() {
test('Table fromTextArray', () {
pdf.addPage(Page(
build: (Context context) => Table.fromTextArray(
build: (Context context) => TableHelper.fromTextArray(
context: context,
tableWidth: TableWidth.min,
data: <List<dynamic>>[
... ... @@ -204,7 +204,7 @@ void main() {
test('Table fromTextArray with formatting', () {
pdf.addPage(Page(
build: (Context context) => Table.fromTextArray(
build: (Context context) => TableHelper.fromTextArray(
border: null,
cellAlignment: Alignment.center,
headerDecoration: const BoxDecoration(
... ... @@ -242,7 +242,7 @@ void main() {
),
build: (Context context) => Directionality(
textDirection: TextDirection.rtl,
child: Table.fromTextArray(
child: TableHelper.fromTextArray(
headers: <dynamic>['ثلاثة', 'اثنان', 'واحد'],
cellAlignment: Alignment.centerRight,
data: <List<dynamic>>[
... ... @@ -257,7 +257,7 @@ void main() {
test('Table fromTextArray with alignment', () {
pdf.addPage(
Page(
build: (Context context) => Table.fromTextArray(
build: (Context context) => TableHelper.fromTextArray(
cellAlignment: Alignment.center,
data: <List<String>>[
<String>['line 1', 'Text\n\n\ntext'],
... ...
... ... @@ -149,7 +149,7 @@ void main() {
pageFormat: const PdfPageFormat(400, 200),
margin: const EdgeInsets.all(10),
build: (Context context) => <Widget>[
Table.fromTextArray(
TableHelper.fromTextArray(
context: context,
cellPadding: const EdgeInsets.all(3),
data: <List<String>>[
... ...
... ... @@ -86,7 +86,7 @@ void main() {
pdf.addPage(Page(
theme: theme,
build: (Context context) => Center(
child: Table.fromTextArray(context: context, data: <List<String>>[
child: TableHelper.fromTextArray(context: context, data: <List<String>>[
<String>['Header', '123'],
<String>['Cell', '456']
]),
... ...