Closed daveshirman closed 2 years ago
Hi @daveshirman,
You are using a different text style for the cell widget. By default, the cell width is calculated based on the default text style. To calculate the cell width based on different TextStyle, just override the computeHeaderCellWidth method for header and computeCellWidth method for cell and return the super method with the required TextStyle. Please check the following sample and code snippet.
In DataGrid:
final CustomColumnSizer _customColumnSizer = CustomColumnSizer();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter SfDataGrid'),
),
body: SfDataGrid(
source: _employeeDataSource,
columns: getColumns,
columnSizer: _customColumnSizer,
columnWidthMode: ColumnWidthMode.auto),
);
}
In DataGridSource:
class EmployeeDataSource extends DataGridSource {
…
@override
DataGridRowAdapter? buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((dataGridCell) {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
dataGridCell.value.toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
softWrap: false,
));
}).toList());
}
}
In ColumnSizer:
class CustomColumnSizer extends ColumnSizer {
@override
double computeHeaderCellWidth(GridColumn column, TextStyle style) {
style = const TextStyle(fontWeight: FontWeight.bold);
return super.computeHeaderCellWidth(column, style);
}
@override
double computeCellWidth(GridColumn column, DataGridRow row, Object? cellValue,
TextStyle textStyle) {
textStyle = const TextStyle(fontWeight: FontWeight.bold);
return super.computeCellWidth(column, row, cellValue, textStyle);
}
}
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/main-621685425
Also, we have already provided examples in our UG documentation. Please go through this,
UG Documentation: https://help.syncfusion.com/flutter/datagrid/columns-sizing?cs-save-lang=1&cs-lang=dart#autofit-calculation-based-on-different-textstyle
We hope this helps. Please let us know if you require any further assistance on this. We will be happy to assist you.
Regards, Tamilarasan
Thank you. Although in the documentation, I guess it just wasn't obvious.
Having followed the examples here and thoroughly read the documentation here, I simply don't understand why my columns don't show all their content.
See in this screenshot, but the email and phone number columns are cut off:
Things I tried to pay attention to:
Any help to explain why this is happening would be greatly appreciated, thanks.
Page:
AthleteDataSource