Set width of individual DataColumn in PaginatedDataTable - flutter

I have a PaginatedDataTable with 4 DataColumns. Because I have 4 columns they also don't fit on my page which makes it so you have to horizontally scroll.
Only the first DataColumn is a string for name and the 3 other columns will take an int value. Therefore they shouldn't be that long.
How can I set the width for individual DataColumns?
I also tried adding columnSpacing but that cuts off the right side of the table and doesn't adjust columns individually.
Below is my code and a screenshot of my current UI.
Widget build(BuildContext context){
return Scaffold(
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Column(
children: [
PaginatedDataTable(
source: data,
columns: [
DataColumn(label: Text('Name')),
DataColumn(label: Text('Amount')),
DataColumn(label: Text('Price')),
DataColumn(label: Text('Total')),
],
rowsPerPage: 10,
),
],
),
),
)
);
}
Thanks in advance.

Try to play with options
double horizontalMargin = 24.0, double columnSpacing = 56.0
of PaginatedDataTable.
Right now no other ways to tune the spacing between columns.

I've managed to fix this by using this package: https://pub.dev/packages/data_table_2. Which is nearly the same as the flutter built in widget, but this one has way more options to modify the table.

Related

Flutter consistent spacing for List Tile elements

I have a ListView.Builder with ListTiles and I would like the spacing to be consistent across the data fields. The way I have it setup now is by using Spacers in between each element such as this:
return ListTile(
title: Row(children: [
Text(data[0]),
Spacer(),
Text(data[1]),
Spacer(),
Text(data[2])
])
);
I'm looking for a solution that will keep the spacing responsive without the spacing being inconsistent across the ListView.builder.
You may have to set a width for the Text
final size = MediaQuery.of(context).size;
return ListTile(
title: Row(children: [
SizedBox(
width: size.width/4,
child: Text(data[0]),
)
Spacer(),
Text(data[1]),
Spacer(),
Text(data[2])
])
);

Flutter DataTable limit width to screen width & wrap text

This is my DataTable:
SizedBox(
height: 200,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: DataTable(
columnSpacing: 0,
columns: const [
DataColumn(label: Text("Domain")),
DataColumn(label: Text("Request count")),
],
rows: const [
DataRow(cells: [
DataCell(Text("A very long text which causes the right column to be pushed out of the screen")),
DataCell(Text("12345")),
]),
DataRow(cells: [
DataCell(Text("It would be the best if this text would be shown in to lines so that there is enough space for the second column")),
DataCell(Text("678890")),
]),
],
),
),
)
The exception is:
The following assertion was thrown during layout:
A RenderFlex overflowed by 39 pixels on the right.
The relevant error-causing widget was
DataTable
I want the table to be limited in height, so that you can scroll through it, but horizontally the width should be fixed, so that you cannot scroll. If the content does not fit into the columns, as the above error shows, then the text should not disappear from the screen, but instead the text should go into a second row so that there is enough space for both columns on the screen. I have already tried many things, but I simply cannot limit the width of the table so that it is only as wide as the parent widget.
Providing width for every DataCell child solve the issue.
DataCell(
SizedBox(
width: x,
child: Text(
"A very long text which causes the right column to be pushed out of the screen",
overflow: TextOverflow.visible,
softWrap: true,
),
),
),
To get responsive width, you can use LayoutBuilder on body and use it's width: constraints.maxWidth * .5, to get 50% width of the screen or MediaQuery, (make sure minimizing the padding).

How do you display a Table / DataTable with expanding widths and heights depending on the cell content? (like a HTML table behaviour)

How to get the cell width AND height of a table / datatable (or other widget) to adjust depending on the content, how do you do this in Flutter?
This is the default table behaviour in HTML and I want that in Flutter because it is the best way to fit as much data as possible in a table.
Using a DataTable, the widths of each cell expand based on the length of the text in the cells. However, the cell heights (or row height) doesn't expand.
DataTable text truncates where the text is too long; the height of the cell is not expanded:
Using a Table widget, the width of the cells do not adjust depending on the length of the content. The column widths do contract & expand but only as a fraction of the total allocated space for the widget and not depending on their content (using columnWidths & FixedColumnWidth).
Here, note how 'Medium column' has space to its right, 'Long column' should have expanded into that space:
class TableDemo extends StatelessWidget {
Widget build(BuildContext context) {
return Table(
columnWidths: {
0: FixedColumnWidth(20),
},
defaultColumnWidth: FlexColumnWidth(),
children: [
TableRow(children: [Text('Short column'), Text('Long column'), Text('Medium column')]),
TableRow(
children: [
Text('1'),
Text('Some long content i would like to be wrapped when column width is not enought to fully display it'),
Container(
child: Text('Some more text'),
)
],
)
],
);
}
}
class DataTableDemo extends StatelessWidget {
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Container(child: Text("Short column"), width: 100)),
DataColumn(label: Container(child: Text("Long column"), width: 100)),
DataColumn(label: Container(child: Text("Medium column"), width: 100)),
],
rows: [
DataRow(
cells: [
DataCell(Text('1')),
DataCell(Text(//
'Some long content i would like to be wrapped when column width is not enought to fully display it, Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not enought to fully display it')),
DataCell(Container(child: Text('Some more text'))),
],
),
DataRow(
cells: [
DataCell(Text('1')),
DataCell(Text('Some long content i would like to be wrapped when column width is not enought to fully display it')),
DataCell(Container(child: Text('Some more text'))),
],
),
DataRow(
cells: [
DataCell(Text('1')),
DataCell(Text('Some long content i would like to be wrapped when column width is not enought to fully display it')),
DataCell(Container(child: Text('Some more text'))),
],
),
],
);
}
}
Use https://api.flutter.dev/flutter/rendering/IntrinsicColumnWidth-class.html in the columnWidths property of the Table

Fix minimum width to a Widget which needs to expand in Flutter

I need to fix a minimum width to my Column Widgets. Inside each of them, I have Text Widgets which can be very short or very long. I need to fix a minimum width to them in order to have an acceptable size of Column even if the text is short. The other Column need obviously to adapt himself.
Row(children: [
Column(
children: [
Container(
constraints: BoxConstraints(minWidth: 80), // do not work
child: Text("short text"),
),
],
),
Column(
children: [
Container(
constraints: BoxConstraints(minWidth: 110), // do not work
child: RichText(
text: TextSpan(
text:"very very longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg text")),
),
],
),
],
)
There's probably a dozen ways to do what you want. And likely none of them straightforward or easy to understand. (The subject of constraints & sizes is quite complicated. See this constraints page for more examples & explanations.)
Here's one potential solution.
This will set a minimum width for the blue column (based on stepWidth), but will expand/grow if the text (child) inside wants to.
The yellow column will resize to accommodate the blue column.
class ExpandedRowPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Expanded Row Page'),
),
body: SafeArea(
child: Center(
child: Row(
children: [
IntrinsicWidth(
stepWidth: 100,
// BLUE Column
child: Container(
color: Colors.lightBlueAccent,
child: Column(
children: [
//Text('Short'),
Text('shrt')
],
)
),
),
// YELLOW Column
Flexible(
child: Container(
alignment: Alignment.center,
color: Colors.yellow,
child: Column(
children: [
Text('Very lonnnnnnnnnnnnnnnnnnnnnnnnnnnng texttttttttttttt'),
],
)
),
)
],
)
),
),
);
}
}
You could do the above without a Flexible yellow column, but a very long text child would cause an Overflow warning without a Flexible or Expanded wrapping widget.
A Row widget by itself has an infinite width constraint. So if a child wants to be bigger than screen width, it can, and will cause an overflow. (Try removing Flexible above and rebuild to see.)
Flexible and Expanded, used only inside Row & Column (or Flex, their superclass), checks screen width and other widgets inside a Row, and provides its children with a defined constraint size instead of infinite. Children (inside Flexible/Expanded) can now look up to parent for a constraint and size themselves accordingly.
A Text widget for example, will wrap its text when it's too wide for constraints given by Flexible/Expanded.
use FittedBox();
suppose Example:
Row(
children: [
Column(
children: [
Container(
constraints: BoxConstraints(minWidth: 80), // do not work
child: Text("short text"),
),
],
),
Column(
children: [
Container(
constraints: BoxConstraints(minWidth: 110), // do not work
child:
FittedBox(
child: RichText(
text: TextSpan(
text:
"very very longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg text")),
),
),
],
),
],
);

How to position a Text/TextSpan inside a class

How can I position my Text like this? I check the docs and I only see texts that are on each of the other side of the row but not someone like this which is the other way around.
I just wanted to have my first column takes about 30% of the row inside a card with text alignment of right and my second column takes about 70% of the row inside a card with text alignment of left. I also wanted to have it like a sort of table where in everything is in proper alignment with regards to column. Thank you
Date: Today
Address: Sample
Gender: Male
Status: Active
You can use this code:
Table(
children: [
buildTableRow("Date:", "Today"),
buildTableRow("Address:", "Sample"),
buildTableRow("Gender:", "Male"),
buildTableRow("Status:", "Active"),
],
),
TableRow buildTableRow(String _title, String _value) {
return TableRow(
children: [
Container(child: Text(_title), alignment: Alignment.centerRight),
Container(
child: Padding(padding: const EdgeInsets.only(left: 15), child: Text(_value)),
alignment: Alignment.centerLeft,
),
],
);
}
Code output:
You can use the Table class of flutter
https://api.flutter.dev/flutter/widgets/Table-class.html
Table(
columnWidths: {0 : FractionColumnWidth(.3)},
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: [
]
),