How to fill empty space of screen by Datatable rows? - flutter

I have a datatable that are filled by results from mysql. The number of rows depends on the number of results from the mysql. I need to the DataTable fill by rows all empty screen space, whatever of the number of mysql results. In other words: the top of DataTable rows will be filled, and the bottom ones will be empty if number of results is less than the rows number.
How it works now:
SingleChildScrollView _dataColumn() {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
child: CustomDataTable(
columns: [
DataColumn(
label: Text('Teachers'),
),
],
rows: _filterEmployees
.map(
(employee) => DataRow(cells: [
DataCell(
Text(
employee.teachName,
),
onTap: () {
...
});
},
),
]),
)
.toList(),
),
),
);
}

Why not just use List Generate for a fixed number of iterations and mapping over the number in the employees.
SingleChildScrollView _dataColumn() {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
child: CustomDataTable(
columns: [
DataColumn(
label: Text('Teachers'),
),
],
rows:
List<int>.generate(10, (i) => i).map((num) {
if (num < _filterEmployees.length) {
return _filterEmployees[num];
}
//Default Employee object
return Employee();
}).map((employee) => DataRow(cells: [
DataCell(
Text(
employee.teachName,
),
onTap: () {
...
});
},
),
]),
)
.toList(),
),
),
);
}
Now your Datatable will always consist of 10 entries, first entries will be filled but later on there will be empty entries.

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.

Flutter load list with id

Hey how can i load one of several lists with one id?
my lists look like this
List one = [
{'name': 'USA'},
{'name': 'Germany'},
];
List two = [
{'name': 'BMW'},
{'name': 'Audi'},
];
i need load one of this list like this
body: Column(
children: one.map((info) {
return Container(
child: SizedBox(
children: [
Text(
info['name'],
),
],
),
),
);
}).toList(),
));
all i want is to load one of the two lists depending on the id of my previous page
for example id 1 was passed then two.map should be loaded
I have created a variable on this page that is also passed, I tried to use variable.map but it doesn't work
If you have many lists, put your Lists into a List. With your variable from the previous page you can call the right List out of your data.
data[variable].map(info) { ....... }
Edit:
Step
create List<List> data ; their you can save all your list; when you want your List "one".
const List<List<dynamic>> data = [[{'name': 'USA'},{'name': 'Germany'},],[ {'name': 'BMW'},{'name': 'Audi'},]];
Step
put it into your widget tree and call the right data with your variable(x)(INTEGER) to get the right list and map it as before:
body: Column(
children: data[x].map((info) {
return Container(
child: SizedBox(
children: [
Text(
info['name'],
),
],
),
),
);
}).toList(),
));
If that isn't working you may have another problem in another topic.
Tom
ternary operator helps you
body: Column(
children:Your variable == 1
? one.map((info) {
return Container(
child: SizedBox(
child: Text(
info['name'],
),
),
);
}).toList()
: one.map((info) {
return Container(
child: SizedBox(
child: Text(
info['name'],
),
),
);
}).toList()),
);

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.

How to display a list of items inside a column using a ListTile in Flutter

staff.ulogin is a list returned from a web service. If there is more than one item returned, I need to display of list of those items (displaying the company name). I can get the first item displaying, but I'm not sure how to display the entire list.
I also need the user to be able to tap an item so I can setup that company for use, so I'll need to know what item they chose. Thanks for any help.
if (staff.ulogin.length > 1) {
Alert(
context: context,
title: 'Choose Company',
content: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//how to display all the items
ListTile(
title: Text(staff.ulogin[0].company),
onTap () {}, // <--- how to get the index of the item tapped
),
],
),
),
buttons: [
DialogButton(
child: Text('Cancel', style: TextStyle(color: Colors.white, fontSize: 20)),
color: kMainColor,
onPressed: () {
Navigator.of(context).pop();
},
),
],
).show();
} else
I believe that the correct way of display a list o items is using a ListView. For this case you can use a ListView.builder like this:
Container(
height: 300.0, // Change as you wish
width: 300.0, // Change as you wish
child: ListView.builder
(
itemCount: staff.ulogin.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(staff.ulogin[index].company),
onTap () {
someFunction(staff.ulogin[index]);
},
),
}
)
)

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,)))),
]
)