Using Flutter v1.20.2
The following code
var data = [
['a', 'VerylongstringwithnospacesVerylongstringwithnospaces'],
['abc', '123456789'],
['abcdefg', '123'],
];
Expanded(
child: Table(
border: TableBorder.all(color: Colors.red),
columnWidths: {
0: IntrinsicColumnWidth(),
1: IntrinsicColumnWidth(),
},
children: data.map<TableRow>((x) => (
TableRow(
children: <TableCell>[
TableCell(child: Container(child: Text(x[0]))),
TableCell(child: Container(color: Colors.green, child: Text(x[1]))),
],
)
)).toList(),
),
),
results in this layout
As you can see the text overflows the boundary of the table.
I have tried innumerable solutions but none have worked. It appears that the TableCell itself has a width bigger than it should have, comparing to the size of the column of the table.
The biggest problem is that the size of both columns are unknown, they could be hundreds of characters long or single letter. In case of it being bigger than the width the text should break to the next line.
Is there a way to make a fluid grid layout so that both sides resize according to the amount of content for each Column? The ideal layout would be something like this
The reason why It’s happening is that the IntrinsicColumnWidth doesn’t give TableCell parent size. Because it's trying to set the column width based on child width. At the same time, to make text wrapping on lines, you need to have the width of parent, otherwise text widget don’t know it’s limits.
What options do you have :
FixedColumnWidth
FlexColumnWidth
FractionColumnWidth
MaxColumnWidth
MinColumnWidth
(Last two widgets used max/min of other TableColumnWidth widgets.)
In your case if you know that the text in the first column never will need to be wraped in lines, you can remove IntrinsicColumnWidth as parameter of second column. If you know that long text is possible, add the maximum width by adding MinColumnWidth
dartpad example
Related
I just looking for a widget type where it provides a simple default solution to draw shared border lines between children widgets, instead of touching two different borders or turning a widget into a border. Basically it's just a table thing with the children as it's cell widgets.
There's Table in Flutter. But sadly seems like the cells is unspannable. No "colspan" thing for it's TableCell. If I put TableRows with different numbers of TableCells, I get error
Table contains irregular row lengths. Every TableRow in a Table must
have the same number of children, so that every cell is filled.
Otherwise, the table will contain holes.
I used to do it with Java.
<TableLayout ...>
<TableRow ...>
<... android:layout_span .../>
</TableRow>
</TableLayout>
I just want to do it again with Flutter. That's all.
ListView.separated might be what you are looking for
It takes the following arguments
itemBuilder for the content of your rows
separatorBuilder for creating a dynamic separator between your cells. You could use the Divider widget for that purpose.
Solved by simply put inner table inside outer table's cell.
Table(
children : [
TableRow( //This row will contains virtually spanned cell
children: [
TableCell(...),
],
),
TableRow( //This row will contains indirectly unspanned cells
children: [
TableCell(
child : Table(...),
),
],
)
]
);
And then remove outer borders for inner table leaving only inner borders.
TableBorder(
horizontalInside : BorderSide(
color : Colors.grey,
),
verticalInside : BorderSide(
color : Colors.grey,
),
)
I don't know whether it's a workaround or just how Flutter deal with it because I got the answer from Flutter's github
I am trying to put some text widgets inside a column, but there is this additional padding that I want to get rid of. Is there any "clean" way to do it? I could probably use Stack and specify the padding for every element in my case, but it is not a scalable solution.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('why'),
Text('so'),
Text('stretched'),
],
),
We have few solutions here:
1/. Add height for style of Text widget, height is double, default is 1.0 You can reduce it to 0.8 or 0.6 or any number as you want.
2/. Add SizedBox wrap your Text widget and set height for this SizedBox as you want.
I have a column with 2 children. The first child has a fixed height, and the second child has dynamic content.
I want the first child to be at the head of the screen, and the second child to be in the vertical middle.
So i added an empty third child and made the first and third child Flexible so that they shared vertical space equally:
Column(
children: [
Flexible(
child: Column(
children: [_firstChild()],
),
),
_secondChild(),
Flexible(child: Container()),
],
)
This works when the content of the second child is short.
But if the second child gets tall, it clips the first child:
Isn't the Flexible supposed to distribute only the empty space when fit: FlexFit.loose? I tried both the fit possibilities. I've tried to put the first child inside a SizedBox and an Align
I've tried to make the third child a Spacer. Nothing has worked so far. The empty third child is taking half of the remaining vertical space.
EDIT:
When the second child's height is too much to make it vertically centered without clipping the first child, i want it to just behave like a default Column with MainAxisAlignment.start (like the first image)
What about this:
Stack(children:[
Center(child: SecondChild()),
Align(alignment: Alignment.topCenter, child: FirstChild()),
])
I have wanted to create the next behavior in flutter, but I can't figure out how.
Is having a text in a row with some other element, the text can have variable length. And the text space in the row should accommodate inside the row, unless the text space is larger than the remaining space in the row, then the text should use one of the text overflow options.
Here a diagram of the behavior, with the ellipsis type of text overflow:
I ave try using a combination of the row and expanded widgets, but I just can't make it.
Any suggestion?
Have you tried like this:
Row(
children: [
Flexible(
child: Text("some long text", overflow: TextOverflow.ellipsis,),
),
RaisedButton(
child: Text("Button"),
)
],
)
I'm trying to build a "semi-responsive" widget. It has a fixed number of relatively large children that always fit on the screen horizontally (A, B and C) and a variable list of small children that not always fit horizontally together with A, B, and C. My idea is to have the widget look like the first picture when the screen is wide, and as a second when the screen is more narrow, and as a third when there's no space for smaller widgets at all. What I'm getting is the third only. As if Wrap always tries to put as many children horizontally as possible. I want it to minimize width instead.
My pseudocode:
Card(
child: Wrap(
children: [
Row(children: [A(), B(), C()]),
Wrap(children: [1(), 2(), 3(), 4(), 5()]),
]
),
);
Any idea how to "squeeze" Wrap vertically? Using standard layout widgets if possible.
You can archive that using the alignment property in the Wrap widget.
Card(
child: Wrap(
alignment: WrapAlignment.end,
children: [
Row(children: [A(), B(), C()]),
1(), 2(), 3(), 4(), 5(),
],),
);
check it out in dartpad gist