I try to use Expanded
in a DataColumn
but I got Incorrect use of ParentDataWidget
The idea is, I want to make the text inside the column center.
So I code like below:
DataColumn(
label: Expanded(
child: Center(
child: Text(
'Center',
),
),
),
),
The only way I can make it is by using Expanded
. But it will make me a Incorrect use of ParentDataWidget
So, other than Expanded
, how can I make my child widget has the size same as the parent widget, in my case, the DataColumn
widget?
Thanks
PS. full code of my DataTable
Widget getDataTable() {
return DataTable(
columns: const [
DataColumn(
label: Expanded(
child: Center(
child: Text(
'center 1',
),
),
),
),
DataColumn(
label: Expanded(
child: Center(
child: Text(
'center 2',
),
),
),
),
],
rows: const [
DataRow(
cells: [
DataCell(
Expanded(
child: Center(child: Text('centered content')),
),
),
DataCell(
Expanded(
child: Center(child: Text('centered content 2')),
),
),
],
),
],
);
}
2
Answers
In flutter code snippet, the code you write as is no issue and work well. There may another error? Here is I test and copy from flutter code snippet with
DataColumn()
.DataCell()
is made ofSizedBox.shrink()
:SizedBox.shrink()
: It creates a box that will become as small as its parent allows.Expanded()
: It tries to occupy all the space availableSo, that’s the reason you are getting error of,
Avoid wrapping
DataCell
withExpanded
: