Skip to content

Commit 940a54d

Browse files
committed
center align fix
1 parent 32acae6 commit 940a54d

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

lib/src/inner.dart

+23-18
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,30 @@ class Sort {
4141
final bool emptyFirst;
4242
}
4343

44-
String alignText(String text, int targetWidth, Side align) {
45-
switch (align) {
46-
case Side.start:
47-
return text.padRight(targetWidth);
48-
case Side.end:
49-
return text.padLeft(targetWidth);
50-
case Side.center:
51-
return alignTextCenter(text, targetWidth);
44+
class Aligner {
45+
46+
String alignText(String text, int targetWidth, Side align) {
47+
switch (align) {
48+
case Side.start:
49+
return text.padRight(targetWidth);
50+
case Side.end:
51+
return text.padLeft(targetWidth);
52+
case Side.center:
53+
return alignTextCenter(text, targetWidth);
54+
}
5255
}
53-
}
5456

55-
bool centerOdd = true;
57+
bool centerOdd = true; // that's why we need the object
5658

57-
String alignTextCenter(String text, int targetWidth) {
58-
final halfD = (targetWidth - text.length)/2;
59+
String alignTextCenter(String text, int targetWidth) {
60+
final halfD = (targetWidth - text.length) / 2;
5961

60-
final half = centerOdd ? halfD.ceil() : halfD.floor(); //>> 1;
61-
centerOdd = !centerOdd;
62-
text = text.padLeft(text.length + half);
63-
text = text.padRight(targetWidth);
64-
return text;
62+
final half = centerOdd ? halfD.ceil() : halfD.floor(); //>> 1;
63+
centerOdd = !centerOdd;
64+
text = text.padLeft(text.length + half);
65+
text = text.padRight(targetWidth);
66+
return text;
67+
}
6568
}
6669

6770
/// Specifies how to convert the source value of the cell to a [String].
@@ -435,6 +438,8 @@ String tabular(List<List<dynamic>> rows,
435438

436439
final formattedRows = <String>[];
437440

441+
final aligner = Aligner();
442+
438443
var iRow = -1;
439444
for (var row in matrix.rows) {
440445
iRow++;
@@ -458,7 +463,7 @@ String tabular(List<List<dynamic>> rows,
458463
}
459464
iCol++;
460465
formatted +=
461-
alignText(cell.toFinalString(), matrix.columns[iCol].textWidth, colToAlign[iCol]);
466+
aligner.alignText(cell.toFinalString(), matrix.columns[iCol].textWidth, colToAlign[iCol]);
462467
}
463468

464469
if (outerBorder) {

0 commit comments

Comments
 (0)