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

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

Related

Make Container fill TableCell in Flutter

I'm trying to make a Table with two cells of the same width and height, so that the height depends on the size of the content. However, the smaller TableCell always shrinks:
This is what I'm trying to implement:
Here's the code:
Table(
children: [
TableRow(
children: [
TableCell(
child: Container(
color: Colors.green,
child: Text(
'long text long text long text long text long text long text long text'),
),
),
TableCell(
child: Container(
color: Colors.orange,
child: Text('short text'),
),
),
],
)
],
),
P.S. I could solve it by adding verticalAlignment: TableCellVerticalAlignment.fill, to the smaller cell, but any cell can be the smaller one, depending on the content. When I add this line to both cells, the whole table disappears. The only bypass I could imagine is to calculate the length of the content and find the smaller cell, but I wonder if there is a way to implement this UI directly with Flutter.
Would appreciate any help.
1. Row with IntrinsicHeight
IntrinsicHeight limits the height of the Row to the content size, which however is considered a 'relatively expensive' approach and is not recommended.
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.green,
child: Text(
'long text long text long text long text long text',
))),
Expanded(
child: Container(
color: Colors.orange,
child: Text(
'short text',
))),
],
),
),
2. Table with TableCellVerticalAlignment.fill
As mentioned in the question, the .fill option must not be used in the largest TableCell, because in this case the TableRow will have zero height. This is the preferred solution, because it doesn't have the 'expensiveness' issue of the previous one.
final texts = ['long text long text long text long text long text', 'short text'];
final colors = [Colors.green, Colors.orange];
// find the longest text and its index
final max = texts.asMap().entries.reduce(
(a, b) => (a.value.length > b.value.length) ? a : b,
);
return Table(children: [
TableRow(
children: texts
.asMap()
.entries
.map((e) => TableCell(
// use .fill in all cells except the largest
verticalAlignment: (e.key != max.key)
? TableCellVerticalAlignment.fill
: TableCellVerticalAlignment.top,
child: Container(
color: colors[e.key],
child: Text(e.value),
)))
.toList(),
),
]);

Flutter: Is it possible to set vertical alignment in text lines at height> 1.0?

All texts in Figma have some height, for example 1.5, but when I set that height to the TextStyle, all lines with the new height are aligned to the bottom.
If using Center or Align widgets - wrong result. Examples has bottom vertical alignment in their lines. Like on bottom screenshots.
[
Is there a possibility to set vertical alignment in flutter Text wiget for every line? Or maybe someone has some helpful tips to solve the problem?
Text(
'Example\nExample',
textAlign: TextAlign.center,
style:TextStyle(
height: 2.5,
fontSize: 20,
),
);
Solution:
As user1032613 suggested, such a solution helped.
final text = 'Example Example\nExample Example';
const double height = 2.5;
const double textSize = 16.0;
const double bottomPadding = (height * textSize - textSize) / 2;
const double baseline = height * textSize - height * textSize / 4;
final Widget textWidget = Container(
color: const Color(0xFFFFFFFF),
padding: const EdgeInsets.only(bottom: bottomPadding),
child: Baseline(
baselineType: TextBaseline.alphabetic,
baseline: baseline,
child: Text(
text,
style: const AppTextStyle(
height: height,
fontSize: textSize,
color: const Color(0xFFaa3a3a),
),
),
),
);
There is a property called leadingDistribution which can be used for that:
Text(
'text',
style: TextStyle(
height: 2.0,
leadingDistribution: TextLeadingDistribution.even,
),
)
This is a quite common problem in Flutter when using custom fonts.
The solution our team currently uses is either use a Padding or a Baseline widget and manually tweak the text to make it appear vertically centered.
This can be done by setting textHeightBehavior property of Text.
Text(
'text',
style: TextStyle(
color: Colors.black,
height: 16 / 12,
),
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: true,
applyHeightToLastDescent: true,
leadingDistribution: TextLeadingDistribution.even,
),
),
Most important thing is to set leadingDistribution as TextLeadingDistribution.even.
One way:
Align(
alignment: Alignment.center,
child: Text("Text"),
),
Another way:
Center(
child: Text("Hello World", textAlign: TextAlign.center,),
),

Handle tap in Flutter TableRow

I need to make the TableRow clickable and navigate to other screen but I cannot wrap TableRow with GestureDetector or Inkwell. How can I make the TableRow clickable. I have implemented as follows:
for (int i = 0; i < menuList.length; i++)
TableRow(children: [
SizedBox(
width: 5,
),
Text((i + 1).toString()),
Text(menuList[i].name),
Text(menuList[i].price.toString()),
Text(menuList[i].maxQty.toString()),
menuList[i].status == 0
? Text(
menuList[i].foodStatus,
style: TextStyle(color: Colors.red),
)
: YourListViewItem(
id: menuList[i].id,
index: menuList[i].status,
),
]),
I don't think you can do this,
You can use DataTable instead of Table widget, it will definitely meet your need.
In DataRow there is a property named onSelectChanged, This is
exactly what you want.
Not for the whole row, but for the individual cells in a row you can use TableRowInkWell. TableRowInkWell goes inside a TableRow itself, wrapping a child. Here is the answer, with credit to SoloWofl93: How to use TableRowInkWell inside Table in flutter?
Table(
border: TableBorder.all(),
children: [
TableRow(children: [
// HERE IT IS...
TableRowInkWell(
onTap: (){},
child: Column(children: [
Icon(
Icons.account_box,
size: iconSize,
),
Text('My Account'),
]),
),
Column(children: [
Icon(
Icons.settings,
size: iconSize,
),
Text('Settings')
]),
]),
],
)
It seems impossible to do this.
One thing you can try is adding inkwell to a table row of exactly the same size on top of the original table using a stack widget.

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 to change DataTable's column width in Flutter?

I have a DataTable in my Flutter app. The problem is that when data is filled, the width of the columns is set automatically, and it too large. How can I manually set the column width? I tried to change the width parameters in the "Widget build", but it change the width of the whole table, but not a desired column.
Add columnSpacing property to DataTable. By default it is set to 56.0.
columnSpacing: 30.0
#Smith, you mean you can't do this ? if you could share some code ...
Widget build(BuildContext context) {
return Scaffold(
body: DataTable(
columns: [DataColumn(label: Text('label'))],
rows: [
DataRow(cells: [DataCell(
Container(
width: 200, //SET width
child: Text('text')))
])
]
),
);
use columnSpacing in the databale then set 1 this takes then length of column text
columnSpacing: 0,
Better than using Container as per the accepted answer, use ConstrainedBox so that the cell size will only increase if the contents is equal to or exceeds the constrained width.
import 'package:flutter/material.dart';
void main() async {
runApp(
MaterialApp(
home: Scaffold(
body: Card(
child: DataTable(columns: [
DataColumn(
label: Text(
'short text column'.toUpperCase(),
style: TextStyle(fontWeight: FontWeight.bold),
)),
DataColumn(
label: Text(
'long text column'.toUpperCase(),
style: TextStyle(fontWeight: FontWeight.bold),
)),
], rows: [
DataRow(cells: [
DataCell(Text('short text')),
DataCell(ConstrainedBox(
constraints: BoxConstraints(maxWidth: 250), //SET max width
child: Text('very long text blah blah blah blah blah blah',
overflow: TextOverflow.ellipsis))),
]),
DataRow(cells: [
DataCell(Text('short text')),
DataCell(ConstrainedBox(
constraints: BoxConstraints(maxWidth: 250), //SET max width
child: Text('very long text blah blah blah blah blah blah',
overflow: TextOverflow.ellipsis))),
])
]),
),
),
),
);
}
View Dartpad example