flutter DataTable: columns and rows property not defined problem - flutter

I've started developing in Flutter with tables, but I met some problems with tables.
I use DataTable to represent data in a table. There is a problem that i face (The named parameter 'columns' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'columns'). it dos not recognize column's and rows property in DataTable widget. here is my code:
import 'package:flutter/material.dart';
class DataTable extends StatelessWidget {
#override
enter code heWidget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: DataTable(
columns: <DataColumn>[
DataColumn(
label: Text(
'Name',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Age',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Designation',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
],
rows: const <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('Mohit')),
DataCell(Text('23')),
DataCell(Text('Associate Software Developer')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('Akshay')),
DataCell(Text('25')),
DataCell(Text('Software Developer')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('Deepak')),
DataCell(Text('29')),
DataCell(Text('Team Lead ')),
],
),
],
),
),
);

Your class is named DataTable which clashes with the DataTable widget found in the material.dart.
Since dart first checks the current scope while resolving conflicts, the DataTable that you are using inside your build is referencing your own DataTable Stateless widget.
Rename it to MyDataTable, your issue will be fixed.
class MyDataTable extends StatelessWidget

Related

Flutter DataTable get all row values

I have a data table in which every row could be editable. I need to make a submit button that saves all changes in the table. But I can't figure out how to get all rows in the table.
TextButton(
style: TextButton.styleFrom(
alignment: Alignment.center,
primary: Colors.white,
),
onPressed: () {
print(table1.rows());
},
child: Text(
"Save all changes",
),
),
And DataTable with the key "table1";
Container(
margin: const EdgeInsets.all(10.0),
child: DataTable(
key: table1,
sortAscending: true,
columns: <DataColumn>[
DataColumn(label: Text('AAAA')),
DataColumn(label: Text('BBBB')),
],
rows: provider.data.results.map((data) =>
DataRow(
selected: selectedUsers.contains(data),
onSelectChanged: (b) {
onSelectedRow(b, data);},
cells: [
DataCell(Text(data.aaaa)),
DataCell(Text(data.bbbb)),])).toList(),),),
I want to something like call table1 and get rows and cells in it.
The DataTable class has a rows() method that will give you the rows. Then, for each row you can access the cells by calling the cells() method on the DataRow class.
Check the documentation:
https://api.flutter.dev/flutter/material/DataTable-class.html
If you have specific problems, post your code and what you are stuck at.

How to hide or remove column from data table or set it invisible using flutter

I have a screen as the below one:
So I need now to remove the columns with name Name & Value and just keep the rows or even set the column visible, I tried to create DataTable without Column by removing them it gives me an error and conflicts, as it's gives me the below error:
'package:flutter/src/material/data_table.dart': Failed assertion: line 419 pos 15: 'columns != null': is not true.
So there's any way to remove this?
Here's a small part for my code:
DataTable(
columnSpacing: 83,
dataRowHeight: double.parse('20'),
columns: [
DataColumn(label: Text('Name')),
DataColumn(
label: Row(
children: <Widget>[
Text('Value'),
],
)),
],
rows: [
DataRow(cells: [
DataCell(Row(
children: <Widget>[
Text('Sim operator'),
],
)),
DataCell(
Text(simInfo.operator == null
? ' '
: simInfo.operator),
),
]),
DataRow(cells: [
DataCell(Row(
children: <Widget>[
Text('ICCID'),
],
)),
DataCell(
Text(simInfo == null ? '' : simInfo.iccid)),
]),
],
),
if you want to don't show column title, set them as empty Text and set headingRowHeight property to 0 or another small number.
DataTable(
columnSpacing: 83,
headingRowHeight: 0,
dataRowHeight: double.parse('20'),
columns: [
DataColumn(label: Text('')),
DataColumn(label: Text('')),
],
rows: [
...
],
),
another way is use Table widget that you have much control on details
https://api.flutter.dev/flutter/widgets/Table-class.html
You can easily change the Text('some data text', style: TextStyle(color: Colors.transparent))
But alternatively I will recommend you take a moment to understand the data and create a class that fits your data - model. In order you can take whatever data directly from the corresponding data - object. And display it wherever you want.
Otherwise you can use a Stack() widget. Syntax like a Column or Row // with children[] // and just put a container above it.

Is it possible to make only the rows of DataTable widget scrollable?

I'm trying to implement a DataTable where the content is scrollable.
The issue is that the whole Table is scrollable, but I want the header to be static.
Does someone have an idea how to realize this?
Maybe two seperated DataTables? One just with the header and another scrollable one just with the content.
Though I think there must be a more elegant way to do this.
Here is my current, custom DataTable widget:
Widget _widgetDatatable(List<Client> clients) {
return Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child:
DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text('Name')),
DataColumn(label: Text('First')),
DataColumn(label: Text('Date Birth')),
DataColumn(label: Text('Date Moved In')),
],
rows: clients
.map(
(e) => DataRow(
onSelectChanged: (value) {
bloc.dataRowKlientSingleClicked(e);
},
selected: e.clickedInDataTable,
cells: [
DataCell(Text(e.lastName)),
DataCell(Text(e.firstName)),
DataCell(
Text(format(e.dateOfBirth)),
),
DataCell(
Text(format(e.dateMovedIn)),
),
]),
)
.toList()),
),);
}
At the moment, there is no option to do this with the DataTable widget. But luckily, there is a package that implements a table that can do just that: table_sticky_headers.

flutter DataTable multiline wrapping and centering

I'm trying to have multiple, centered lines in the DataColumn() row of a DataTable() in flutter. It seems, though, that there is no support for centering or for multiple lines.
My DataTable Code looks something like this:
class TestDayData extends StatelessWidget {
final List<String> timesList = [
"This is",
"a bunch",
"of strings",
];
final String day;
TestDayData({Key key, this.day}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(
label: Center(child: Text(day)),
numeric: false,
),
],
rows: timesList
.map(
(times) => DataRow(cells: [
DataCell(
Text(times.toString()),
),
]),
)
.toList(),
),
);
}
}
I made a dartpad file here to show the above code in a larger context. (the reason that I am putting multiple DataTables in a Row widget, instead of using one DataTable for all of the days, is because I plan on putting each of them into a Stack widget so that I can overlay appointments on top of the columns.)
https://dartpad.dev/44bbb788e0d5f1e6393dd38a29430981
So far, I can approximate a multi-lined, centered DataColumn row by adding spaces and using a newline character as seen in the dartpad file. (but there has to be a better way!)
You are missing textAlign property in Text widget
DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(
label: Center(child: Text(day, textAlign:TextAlign.center)),
numeric: false,
),
],
rows: timesList
.map((times) => DataRow(cells: [
DataCell(
Text(times.toString(), textAlign: TextAlign.center),
),
]),
)
.toList(),
),
You can try this to center your text in Datacolumn
DataColumn(
label: Center( widthFactor: 1.4,
child: Text("HELLO", textAlign: TextAlign.center,
style: TextStyle(fontSize: 18.0,),),)),
You can try this to center your text in Datacell for rows.
DataCell( Center(child: Text("Hello")))
For me on the DataColumn, using the Center widget or the textAlign property on the Text widget didn't work:
this is my solution:
DataColumn(
label: Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [Text("text")],
),
),
),
DataCell worked just fine with the Center widget
I figured out the solution.
you just need to wrap the text with Center widget and then wrap it again with Expanded widget just like this:
DataTable(
columns: [
DataColumn(label: Expanded(child: Center(child: Text('ID', textAlign: TextAlign.center,))),),
DataColumn(label: Expanded(child: Center(child: Text('name', textAlign: TextAlign.center,)))),
]
)

How do you center the label in a Flutter DataColumn widget?

I can center the DataCell in a DataRow but how do you do it for the DataColumn label?
I want the first DataColumn left justified and the rest centered. Wrapping the label in a Center widget does not take effect.
new DataColumn(
label: Center(
child: Text(statName,textAlign: TextAlign.center,
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),),
)
);
Found how:
DataColumn(
label: Expanded(
child: Text(
'Label',
textAlign: TextAlign.center,
))),
You may want to wrap the contents of the Label in a Center widget. There's also an Align widget that uses alignment: Alignment.center and your Text as it's child.
This is how i resolved this issue :
DataColumn(
label: Center(
widthFactor: 5.0, // You can set as per your requirement.
child: Text(
'View',
style: style_16_bold_primary,
),
),
),
I had the same problem and solved the issue by commenting the following code section in data_table.dart file.
if (onSort != null) {
final Widget arrow = _SortArrow(
visible: sorted,
down: sorted ? ascending : null,
duration: _sortArrowAnimationDuration,
);
const Widget arrowPadding = SizedBox(width: _sortArrowPadding);
label = Row(
textDirection: numeric ? TextDirection.rtl : null,
children: <Widget>[ label, arrowPadding, arrow ],
);
}