David PHAM-VAN

Implement table row vertical alignment

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 1.6.0 3 ## 1.6.0
4 4
5 - Improve Annotations 5 - Improve Annotations
  6 +- Implement table row vertical alignment
6 7
7 ## 1.5.0 8 ## 1.5.0
8 9
@@ -21,13 +21,19 @@ part of widget; @@ -21,13 +21,19 @@ part of widget;
21 /// A horizontal group of cells in a [Table]. 21 /// A horizontal group of cells in a [Table].
22 @immutable 22 @immutable
23 class TableRow { 23 class TableRow {
24 - const TableRow({this.children, this.repeat = false}); 24 + const TableRow({
  25 + this.children,
  26 + this.repeat = false,
  27 + this.verticalAlignment,
  28 + });
25 29
26 /// The widgets that comprise the cells in this row. 30 /// The widgets that comprise the cells in this row.
27 final List<Widget> children; 31 final List<Widget> children;
28 32
29 /// Repeat this row on all pages 33 /// Repeat this row on all pages
30 final bool repeat; 34 final bool repeat;
  35 +
  36 + final TableCellVerticalAlignment verticalAlignment;
31 } 37 }
32 38
33 enum TableCellVerticalAlignment { bottom, middle, top } 39 enum TableCellVerticalAlignment { bottom, middle, top }
@@ -189,6 +195,7 @@ class Table extends Widget implements SpanningWidget { @@ -189,6 +195,7 @@ class Table extends Widget implements SpanningWidget {
189 this.tableWidth = TableWidth.max}) 195 this.tableWidth = TableWidth.max})
190 : assert(children != null), 196 : assert(children != null),
191 assert(defaultColumnWidth != null), 197 assert(defaultColumnWidth != null),
  198 + assert(defaultVerticalAlignment != null),
192 super(); 199 super();
193 200
194 factory Table.fromTextArray({ 201 factory Table.fromTextArray({
@@ -355,22 +362,44 @@ class Table extends Widget implements SpanningWidget { @@ -355,22 +362,44 @@ class Table extends Widget implements SpanningWidget {
355 362
356 // Compute final y position 363 // Compute final y position
357 index = 0; 364 index = 0;
  365 + int heightIndex = 0;
358 for (TableRow row in children) { 366 for (TableRow row in children) {
359 if (index++ < _context.firstLine && !row.repeat) { 367 if (index++ < _context.firstLine && !row.repeat) {
360 continue; 368 continue;
361 } 369 }
362 370
  371 + final TableCellVerticalAlignment align =
  372 + row.verticalAlignment ?? defaultVerticalAlignment;
  373 +
363 for (Widget child in row.children) { 374 for (Widget child in row.children) {
  375 + double childY;
  376 +
  377 + switch (align) {
  378 + case TableCellVerticalAlignment.bottom:
  379 + childY = totalHeight - child.box.y - _heights[heightIndex];
  380 + break;
  381 + case TableCellVerticalAlignment.middle:
  382 + childY = totalHeight -
  383 + child.box.y -
  384 + (_heights[heightIndex] + child.box.height) / 2;
  385 + break;
  386 + case TableCellVerticalAlignment.top:
  387 + childY = totalHeight - child.box.y - child.box.height;
  388 + break;
  389 + }
  390 +
364 child.box = PdfRect( 391 child.box = PdfRect(
365 child.box.x, 392 child.box.x,
366 - totalHeight - child.box.y - child.box.height, 393 + childY,
367 child.box.width, 394 child.box.width,
368 - child.box.height); 395 + child.box.height,
  396 + );
369 } 397 }
370 398
371 if (index >= _context.lastLine) { 399 if (index >= _context.lastLine) {
372 break; 400 break;
373 } 401 }
  402 + heightIndex++;
374 } 403 }
375 404
376 box = PdfRect(0, 0, totalWidth, totalHeight); 405 box = PdfRect(0, 0, totalWidth, totalHeight);
@@ -145,6 +145,47 @@ void main() { @@ -145,6 +145,47 @@ void main() {
145 )); 145 ));
146 }); 146 });
147 147
  148 + test('Table Widget TableCellVerticalAlignment', () {
  149 + pdf.addPage(
  150 + MultiPage(
  151 + build: (Context context) {
  152 + return <Widget>[
  153 + Table(
  154 + defaultColumnWidth: const FixedColumnWidth(20),
  155 + children: List<TableRow>.generate(
  156 + TableCellVerticalAlignment.values.length,
  157 + (int index) {
  158 + final TableCellVerticalAlignment align =
  159 + TableCellVerticalAlignment.values[
  160 + index % TableCellVerticalAlignment.values.length];
  161 +
  162 + return TableRow(
  163 + verticalAlignment: align,
  164 + children: <Widget>[
  165 + Container(
  166 + child: Text('Vertical'),
  167 + color: PdfColors.red,
  168 + ),
  169 + Container(
  170 + child: Text('alignment $index'),
  171 + color: PdfColors.yellow,
  172 + height: 60,
  173 + ),
  174 + Container(
  175 + child: Text(align.toString().substring(27)),
  176 + color: PdfColors.green,
  177 + ),
  178 + ],
  179 + );
  180 + },
  181 + ),
  182 + ),
  183 + ];
  184 + },
  185 + ),
  186 + );
  187 + });
  188 +
148 tearDownAll(() { 189 tearDownAll(() {
149 final File file = File('widgets-table.pdf'); 190 final File file = File('widgets-table.pdf');
150 file.writeAsBytesSync(pdf.save()); 191 file.writeAsBytesSync(pdf.save());