data_table_2
data_table_2 copied to clipboard
auto columns size by contents, like default implementation of DataTable
Fixed width columns are faster than default implementation of DataTable which does 2 passes to determine contents size and justify column widths
no, please hold a way, can auto columns size by contents, like default implementation of DataTable.
minWidth set all columns width is very strange, should in DataColumn2's size set auto or fixed width.
minWidth, smRatio, lmRatio, ColumnSize.M(S,L) are not needed, too complex. only DataColumn2 size set auto or fixed width.
HTML table style and CSS is a good practice, no smRatio, lmRatio, Size.M, Size.S, Size.L
No plans to add that feature as of now. PR is welcome
@maxim-saplin , thanks very much, I typed a small piece of very ugly code, the table automatically sets the column width by content, I know there is a performance problem, but this is my personal solution.
// Joo Tsao
final TextStyle effectiveHeadingTextStyle =
headingTextStyle ?? theme.dataTableTheme.headingTextStyle ?? theme.textTheme.subtitle2!;
final TextStyle effectiveDataTextStyle =
dataTextStyle ?? theme.dataTableTheme.dataTextStyle ?? theme.textTheme.bodyText2!;
for (var i = 0; i < columns.length; i++) {
var col = columns[i];
var label = (col.label as Text);
var textDirection = col.numeric ? TextDirection.rtl : TextDirection.ltr;
var size = (TextPainter(
text: TextSpan(text: label.data, style: effectiveHeadingTextStyle),
maxLines: 1,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: textDirection)
..layout())
.size;
widths[i] = paddingStart + size.width + paddingEnd;
if (col.onSort != null) widths[i] += 18;
for (var j = 0; j < rows.length; j++) {
var row = rows[j];
var cell = row.cells[i];
if (cell.child is! Text) continue;
var cellText = cell.child as Text;
var size = (TextPainter(
text: TextSpan(text: cellText.data, style: effectiveDataTextStyle),
maxLines: 1,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: textDirection)
..layout())
.size;
var width = paddingStart + size.width + paddingEnd;
if (width > widths[i]) widths[i] = width;
}
}

just like the default data_table, build table performance is ok, 0~4ms.

do not use IntrinsicColumnWidth, calc column max row width by content to set FixedColumnWidth.
Thanks for the attempt! Though a proper GitHub pool request with new feature (not breaking any of the existing capabilities), documented, new example (under /examples folder) showcasing the new capability and with integration tests covering new feature - that might be useful. A snippet of 'ugly' (as you say ;) and slow code is of little help here
In my spare time, I will PR and test.
Btw, it seems you used TextPainter to calculate cell contents' width. Mind that DataCell can have any widget, not just Text