David PHAM-VAN

Move Table.fromTextArray to TableHelper.fromTextArray

@@ -256,7 +256,9 @@ Future<Uint8List> generateDocument( @@ -256,7 +256,9 @@ Future<Uint8List> generateDocument(
256 pw.Paragraph( 256 pw.Paragraph(
257 text: 257 text:
258 '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:'), 258 '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:'),
259 - pw.Table.fromTextArray(context: context, data: const <List<String>>[ 259 + pw.TableHelper.fromTextArray(
  260 + context: context,
  261 + data: const <List<String>>[
260 <String>['Date', 'PDF Version', 'Acrobat Version'], 262 <String>['Date', 'PDF Version', 'Acrobat Version'],
261 <String>['1993', 'PDF 1.0', 'Acrobat 1'], 263 <String>['1993', 'PDF 1.0', 'Acrobat 1'],
262 <String>['1994', 'PDF 1.1', 'Acrobat 2'], 264 <String>['1994', 'PDF 1.1', 'Acrobat 2'],
@@ -271,7 +273,8 @@ Future<Uint8List> generateDocument( @@ -271,7 +273,8 @@ Future<Uint8List> generateDocument(
271 <String>['2010', 'PDF 1.7', 'Acrobat X'], 273 <String>['2010', 'PDF 1.7', 'Acrobat X'],
272 <String>['2012', 'PDF 1.7', 'Acrobat XI'], 274 <String>['2012', 'PDF 1.7', 'Acrobat XI'],
273 <String>['2017', 'PDF 2.0', 'Acrobat DC'], 275 <String>['2017', 'PDF 2.0', 'Acrobat DC'],
274 - ]), 276 + ],
  277 + ),
275 pw.Padding(padding: const pw.EdgeInsets.all(10)), 278 pw.Padding(padding: const pw.EdgeInsets.all(10)),
276 pw.Paragraph( 279 pw.Paragraph(
277 text: 280 text:
@@ -451,7 +451,7 @@ class Invoice { @@ -451,7 +451,7 @@ class Invoice {
451 'Total' 451 'Total'
452 ]; 452 ];
453 453
454 - return pw.Table.fromTextArray( 454 + return pw.TableHelper.fromTextArray(
455 border: null, 455 border: null,
456 cellAlignment: pw.Alignment.centerLeft, 456 cellAlignment: pw.Alignment.centerLeft,
457 headerDecoration: pw.BoxDecoration( 457 headerDecoration: pw.BoxDecoration(
@@ -150,7 +150,7 @@ Future<Uint8List> generateReport( @@ -150,7 +150,7 @@ Future<Uint8List> generateReport(
150 ); 150 );
151 151
152 // Data table 152 // Data table
153 - final table = pw.Table.fromTextArray( 153 + final table = pw.TableHelper.fromTextArray(
154 border: null, 154 border: null,
155 headers: tableHeaders, 155 headers: tableHeaders,
156 data: List<List<dynamic>>.generate( 156 data: List<List<dynamic>>.generate(
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 - Improve verbose output 9 - Improve verbose output
10 - Allow saving an unmodified document 10 - Allow saving an unmodified document
11 - Table cell: dynamic widget [Shahriyar Aghajani] 11 - Table cell: dynamic widget [Shahriyar Aghajani]
  12 +- Move Table.fromTextArray to TableHelper.fromTextArray
12 13
13 ## 3.10.1 14 ## 3.10.1
14 15
@@ -20,16 +20,7 @@ import 'package:meta/meta.dart'; @@ -20,16 +20,7 @@ import 'package:meta/meta.dart';
20 import 'package:vector_math/vector_math_64.dart'; 20 import 'package:vector_math/vector_math_64.dart';
21 21
22 import '../../pdf.dart'; 22 import '../../pdf.dart';
23 -import 'box_border.dart';  
24 -import 'container.dart';  
25 -import 'decoration.dart';  
26 -import 'flex.dart';  
27 -import 'geometry.dart';  
28 -import 'multi_page.dart';  
29 -import 'text.dart';  
30 -import 'text_style.dart';  
31 -import 'theme.dart';  
32 -import 'widget.dart'; 23 +import '../../widgets.dart';
33 24
34 /// A horizontal group of cells in a [Table]. 25 /// A horizontal group of cells in a [Table].
35 @immutable 26 @immutable
@@ -244,6 +235,7 @@ class Table extends Widget with SpanningWidget { @@ -244,6 +235,7 @@ class Table extends Widget with SpanningWidget {
244 this.tableWidth = TableWidth.max, 235 this.tableWidth = TableWidth.max,
245 }) : super(); 236 }) : super();
246 237
  238 + @Deprecated('Use TextHelper.fromTextArray() instead.')
247 factory Table.fromTextArray({ 239 factory Table.fromTextArray({
248 Context? context, 240 Context? context,
249 required List<List<dynamic>> data, 241 required List<List<dynamic>> data,
@@ -278,121 +270,35 @@ class Table extends Widget with SpanningWidget { @@ -278,121 +270,35 @@ class Table extends Widget with SpanningWidget {
278 BoxDecoration? headerCellDecoration, 270 BoxDecoration? headerCellDecoration,
279 BoxDecoration? rowDecoration, 271 BoxDecoration? rowDecoration,
280 BoxDecoration? oddRowDecoration, 272 BoxDecoration? oddRowDecoration,
281 - }) {  
282 - assert(headerCount >= 0);  
283 -  
284 - if (context != null) {  
285 - final theme = Theme.of(context);  
286 - headerStyle ??= theme.tableHeader;  
287 - cellStyle ??= theme.tableCell;  
288 - }  
289 -  
290 - headerPadding ??= cellPadding;  
291 - headerHeight ??= cellHeight;  
292 - oddRowDecoration ??= rowDecoration;  
293 - oddCellStyle ??= cellStyle;  
294 - cellAlignments ??= const <int, Alignment>{};  
295 - headerAlignments ??= cellAlignments;  
296 -  
297 - final rows = <TableRow>[];  
298 -  
299 - var rowNum = 0;  
300 - if (headers != null) {  
301 - final tableRow = <Widget>[];  
302 -  
303 - for (final dynamic cell in headers) {  
304 - tableRow.add(  
305 - Container(  
306 - alignment: headerAlignments[tableRow.length] ?? headerAlignment,  
307 - padding: headerPadding,  
308 - decoration: headerCellDecoration,  
309 - constraints: BoxConstraints(minHeight: headerHeight),  
310 - child: Text(  
311 - headerFormat == null  
312 - ? cell.toString()  
313 - : headerFormat(tableRow.length, cell),  
314 - style: headerStyle,  
315 - ),  
316 - ),  
317 - );  
318 - }  
319 - rows.add(TableRow(  
320 - children: tableRow,  
321 - repeat: true,  
322 - decoration: headerDecoration,  
323 - ));  
324 - rowNum++;  
325 - }  
326 -  
327 - for (final row in data) {  
328 - final tableRow = <Widget>[];  
329 - final isOdd = (rowNum - headerCount) % 2 != 0;  
330 -  
331 - if (rowNum < headerCount) {  
332 - for (final dynamic cell in row) {  
333 - final align = headerAlignments[tableRow.length] ?? headerAlignment;  
334 - final textAlign = _textAlign(align);  
335 -  
336 - tableRow.add(  
337 - Container(  
338 - alignment: align,  
339 - padding: headerPadding,  
340 - constraints: BoxConstraints(minHeight: headerHeight),  
341 - child: Text(  
342 - headerFormat == null  
343 - ? cell.toString()  
344 - : headerFormat(tableRow.length, cell),  
345 - style: headerStyle,  
346 - textAlign: textAlign,  
347 - ),  
348 - ),  
349 - );  
350 - }  
351 - } else {  
352 - for (final dynamic cell in row) {  
353 - final align = cellAlignments[tableRow.length] ?? cellAlignment;  
354 - tableRow.add(  
355 - Container(  
356 - alignment: align,  
357 - padding: cellPadding,  
358 - constraints: BoxConstraints(minHeight: cellHeight),  
359 - decoration: cellDecoration == null  
360 - ? null  
361 - : cellDecoration(tableRow.length, cell, rowNum),  
362 - child: cell is String  
363 - ? Text(  
364 - cellFormat == null  
365 - ? cell.toString()  
366 - : cellFormat(tableRow.length, cell),  
367 - style: isOdd ? oddCellStyle : cellStyle,  
368 - textAlign: _textAlign(align))  
369 - : (cell is Widget ? cell : null),  
370 - ),  
371 - );  
372 - }  
373 - }  
374 -  
375 - var decoration = isOdd ? oddRowDecoration : rowDecoration;  
376 - if (rowNum < headerCount) {  
377 - decoration = headerDecoration;  
378 - }  
379 -  
380 - rows.add(TableRow(  
381 - children: tableRow,  
382 - repeat: rowNum < headerCount,  
383 - decoration: decoration,  
384 - ));  
385 - rowNum++;  
386 - }  
387 - return Table( 273 + }) =>
  274 + TableHelper.fromTextArray(
  275 + context: context,
  276 + data: data,
  277 + cellPadding: cellPadding,
  278 + cellHeight: cellHeight,
  279 + cellAlignment: cellAlignment,
  280 + cellAlignments: cellAlignments,
  281 + cellStyle: cellStyle,
  282 + oddCellStyle: oddCellStyle,
  283 + cellFormat: cellFormat,
  284 + cellDecoration: cellDecoration,
  285 + headerCount: headerCount = 1,
  286 + headers: headers,
  287 + headerPadding: headerPadding,
  288 + headerHeight: headerHeight,
  289 + headerAlignment: headerAlignment,
  290 + headerAlignments: headerAlignments,
  291 + headerStyle: headerStyle,
  292 + headerFormat: headerFormat,
388 border: border, 293 border: border,
389 - tableWidth: tableWidth,  
390 - children: rows,  
391 columnWidths: columnWidths, 294 columnWidths: columnWidths,
392 defaultColumnWidth: defaultColumnWidth, 295 defaultColumnWidth: defaultColumnWidth,
393 - defaultVerticalAlignment: TableCellVerticalAlignment.full, 296 + tableWidth: tableWidth,
  297 + headerDecoration: headerDecoration,
  298 + headerCellDecoration: headerCellDecoration,
  299 + rowDecoration: rowDecoration,
  300 + oddRowDecoration: oddRowDecoration,
394 ); 301 );
395 - }  
396 302
397 @override 303 @override
398 bool get canSpan => true; 304 bool get canSpan => true;
@@ -672,14 +578,4 @@ class Table extends Widget with SpanningWidget { @@ -672,14 +578,4 @@ class Table extends Widget with SpanningWidget {
672 ? _heights[heightIndex] 578 ? _heights[heightIndex]
673 : 0.0; 579 : 0.0;
674 } 580 }
675 -  
676 - static TextAlign _textAlign(Alignment align) {  
677 - if (align.x == 0) {  
678 - return TextAlign.center;  
679 - } else if (align.x < 0) {  
680 - return TextAlign.left;  
681 - } else {  
682 - return TextAlign.right;  
683 - }  
684 - }  
685 } 581 }
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +import 'box_border.dart';
  18 +import 'container.dart';
  19 +import 'decoration.dart';
  20 +import 'geometry.dart';
  21 +import 'table.dart';
  22 +import 'text.dart';
  23 +import 'text_style.dart';
  24 +import 'theme.dart';
  25 +import 'widget.dart';
  26 +
  27 +mixin TableHelper {
  28 + static TextAlign _textAlign(Alignment align) {
  29 + if (align.x == 0) {
  30 + return TextAlign.center;
  31 + } else if (align.x < 0) {
  32 + return TextAlign.left;
  33 + } else {
  34 + return TextAlign.right;
  35 + }
  36 + }
  37 +
  38 + static Table fromTextArray({
  39 + Context? context,
  40 + required List<List<dynamic>> data,
  41 + EdgeInsets cellPadding = const EdgeInsets.all(5),
  42 + double cellHeight = 0,
  43 + Alignment cellAlignment = Alignment.topLeft,
  44 + Map<int, Alignment>? cellAlignments,
  45 + TextStyle? cellStyle,
  46 + TextStyle? oddCellStyle,
  47 + OnCellFormat? cellFormat,
  48 + OnCellDecoration? cellDecoration,
  49 + int headerCount = 1,
  50 + List<dynamic>? headers,
  51 + EdgeInsets? headerPadding,
  52 + double? headerHeight,
  53 + Alignment headerAlignment = Alignment.center,
  54 + Map<int, Alignment>? headerAlignments,
  55 + TextStyle? headerStyle,
  56 + OnCellFormat? headerFormat,
  57 + TableBorder? border = const TableBorder(
  58 + left: BorderSide(),
  59 + right: BorderSide(),
  60 + top: BorderSide(),
  61 + bottom: BorderSide(),
  62 + horizontalInside: BorderSide(),
  63 + verticalInside: BorderSide(),
  64 + ),
  65 + Map<int, TableColumnWidth>? columnWidths,
  66 + TableColumnWidth defaultColumnWidth = const IntrinsicColumnWidth(),
  67 + TableWidth tableWidth = TableWidth.max,
  68 + BoxDecoration? headerDecoration,
  69 + BoxDecoration? headerCellDecoration,
  70 + BoxDecoration? rowDecoration,
  71 + BoxDecoration? oddRowDecoration,
  72 + }) {
  73 + assert(headerCount >= 0);
  74 +
  75 + if (context != null) {
  76 + final theme = Theme.of(context);
  77 + headerStyle ??= theme.tableHeader;
  78 + cellStyle ??= theme.tableCell;
  79 + }
  80 +
  81 + headerPadding ??= cellPadding;
  82 + headerHeight ??= cellHeight;
  83 + oddRowDecoration ??= rowDecoration;
  84 + oddCellStyle ??= cellStyle;
  85 + cellAlignments ??= const <int, Alignment>{};
  86 + headerAlignments ??= cellAlignments;
  87 +
  88 + final rows = <TableRow>[];
  89 +
  90 + var rowNum = 0;
  91 + if (headers != null) {
  92 + final tableRow = <Widget>[];
  93 +
  94 + for (final dynamic cell in headers) {
  95 + tableRow.add(
  96 + Container(
  97 + alignment: headerAlignments[tableRow.length] ?? headerAlignment,
  98 + padding: headerPadding,
  99 + decoration: headerCellDecoration,
  100 + constraints: BoxConstraints(minHeight: headerHeight),
  101 + child: Text(
  102 + headerFormat == null
  103 + ? cell.toString()
  104 + : headerFormat(tableRow.length, cell),
  105 + style: headerStyle,
  106 + ),
  107 + ),
  108 + );
  109 + }
  110 + rows.add(TableRow(
  111 + children: tableRow,
  112 + repeat: true,
  113 + decoration: headerDecoration,
  114 + ));
  115 + rowNum++;
  116 + }
  117 +
  118 + for (final row in data) {
  119 + final tableRow = <Widget>[];
  120 + final isOdd = (rowNum - headerCount) % 2 != 0;
  121 +
  122 + if (rowNum < headerCount) {
  123 + for (final dynamic cell in row) {
  124 + final align = headerAlignments[tableRow.length] ?? headerAlignment;
  125 + final textAlign = _textAlign(align);
  126 +
  127 + tableRow.add(
  128 + Container(
  129 + alignment: align,
  130 + padding: headerPadding,
  131 + constraints: BoxConstraints(minHeight: headerHeight),
  132 + child: Text(
  133 + headerFormat == null
  134 + ? cell.toString()
  135 + : headerFormat(tableRow.length, cell),
  136 + style: headerStyle,
  137 + textAlign: textAlign,
  138 + ),
  139 + ),
  140 + );
  141 + }
  142 + } else {
  143 + for (final dynamic cell in row) {
  144 + final align = cellAlignments[tableRow.length] ?? cellAlignment;
  145 + tableRow.add(
  146 + Container(
  147 + alignment: align,
  148 + padding: cellPadding,
  149 + constraints: BoxConstraints(minHeight: cellHeight),
  150 + decoration: cellDecoration == null
  151 + ? null
  152 + : cellDecoration(tableRow.length, cell, rowNum),
  153 + child: cell is Widget
  154 + ? cell
  155 + : Text(
  156 + cellFormat == null
  157 + ? cell.toString()
  158 + : cellFormat(tableRow.length, cell),
  159 + style: isOdd ? oddCellStyle : cellStyle,
  160 + textAlign: _textAlign(align)),
  161 + ),
  162 + );
  163 + }
  164 + }
  165 +
  166 + var decoration = isOdd ? oddRowDecoration : rowDecoration;
  167 + if (rowNum < headerCount) {
  168 + decoration = headerDecoration;
  169 + }
  170 +
  171 + rows.add(TableRow(
  172 + children: tableRow,
  173 + repeat: rowNum < headerCount,
  174 + decoration: decoration,
  175 + ));
  176 + rowNum++;
  177 + }
  178 + return Table(
  179 + border: border,
  180 + tableWidth: tableWidth,
  181 + children: rows,
  182 + columnWidths: columnWidths,
  183 + defaultColumnWidth: defaultColumnWidth,
  184 + defaultVerticalAlignment: TableCellVerticalAlignment.full,
  185 + );
  186 + }
  187 +}
@@ -54,6 +54,7 @@ export 'src/widgets/shape.dart'; @@ -54,6 +54,7 @@ export 'src/widgets/shape.dart';
54 export 'src/widgets/stack.dart'; 54 export 'src/widgets/stack.dart';
55 export 'src/widgets/svg.dart'; 55 export 'src/widgets/svg.dart';
56 export 'src/widgets/table.dart'; 56 export 'src/widgets/table.dart';
  57 +export 'src/widgets/table_helper.dart';
57 export 'src/widgets/text.dart'; 58 export 'src/widgets/text.dart';
58 export 'src/widgets/text_style.dart'; 59 export 'src/widgets/text_style.dart';
59 export 'src/widgets/theme.dart'; 60 export 'src/widgets/theme.dart';
@@ -190,7 +190,7 @@ void main() { @@ -190,7 +190,7 @@ void main() {
190 190
191 test('Table fromTextArray', () { 191 test('Table fromTextArray', () {
192 pdf.addPage(Page( 192 pdf.addPage(Page(
193 - build: (Context context) => Table.fromTextArray( 193 + build: (Context context) => TableHelper.fromTextArray(
194 context: context, 194 context: context,
195 tableWidth: TableWidth.min, 195 tableWidth: TableWidth.min,
196 data: <List<dynamic>>[ 196 data: <List<dynamic>>[
@@ -204,7 +204,7 @@ void main() { @@ -204,7 +204,7 @@ void main() {
204 204
205 test('Table fromTextArray with formatting', () { 205 test('Table fromTextArray with formatting', () {
206 pdf.addPage(Page( 206 pdf.addPage(Page(
207 - build: (Context context) => Table.fromTextArray( 207 + build: (Context context) => TableHelper.fromTextArray(
208 border: null, 208 border: null,
209 cellAlignment: Alignment.center, 209 cellAlignment: Alignment.center,
210 headerDecoration: const BoxDecoration( 210 headerDecoration: const BoxDecoration(
@@ -242,7 +242,7 @@ void main() { @@ -242,7 +242,7 @@ void main() {
242 ), 242 ),
243 build: (Context context) => Directionality( 243 build: (Context context) => Directionality(
244 textDirection: TextDirection.rtl, 244 textDirection: TextDirection.rtl,
245 - child: Table.fromTextArray( 245 + child: TableHelper.fromTextArray(
246 headers: <dynamic>['ثلاثة', 'اثنان', 'واحد'], 246 headers: <dynamic>['ثلاثة', 'اثنان', 'واحد'],
247 cellAlignment: Alignment.centerRight, 247 cellAlignment: Alignment.centerRight,
248 data: <List<dynamic>>[ 248 data: <List<dynamic>>[
@@ -257,7 +257,7 @@ void main() { @@ -257,7 +257,7 @@ void main() {
257 test('Table fromTextArray with alignment', () { 257 test('Table fromTextArray with alignment', () {
258 pdf.addPage( 258 pdf.addPage(
259 Page( 259 Page(
260 - build: (Context context) => Table.fromTextArray( 260 + build: (Context context) => TableHelper.fromTextArray(
261 cellAlignment: Alignment.center, 261 cellAlignment: Alignment.center,
262 data: <List<String>>[ 262 data: <List<String>>[
263 <String>['line 1', 'Text\n\n\ntext'], 263 <String>['line 1', 'Text\n\n\ntext'],
@@ -149,7 +149,7 @@ void main() { @@ -149,7 +149,7 @@ void main() {
149 pageFormat: const PdfPageFormat(400, 200), 149 pageFormat: const PdfPageFormat(400, 200),
150 margin: const EdgeInsets.all(10), 150 margin: const EdgeInsets.all(10),
151 build: (Context context) => <Widget>[ 151 build: (Context context) => <Widget>[
152 - Table.fromTextArray( 152 + TableHelper.fromTextArray(
153 context: context, 153 context: context,
154 cellPadding: const EdgeInsets.all(3), 154 cellPadding: const EdgeInsets.all(3),
155 data: <List<String>>[ 155 data: <List<String>>[
@@ -86,7 +86,7 @@ void main() { @@ -86,7 +86,7 @@ void main() {
86 pdf.addPage(Page( 86 pdf.addPage(Page(
87 theme: theme, 87 theme: theme,
88 build: (Context context) => Center( 88 build: (Context context) => Center(
89 - child: Table.fromTextArray(context: context, data: <List<String>>[ 89 + child: TableHelper.fromTextArray(context: context, data: <List<String>>[
90 <String>['Header', '123'], 90 <String>['Header', '123'],
91 <String>['Cell', '456'] 91 <String>['Cell', '456']
92 ]), 92 ]),