Flutter - Cells spannable table view - flutter

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

Related

How to align widgets to top in Column class using NavigationRai

I want to put my Text widget at the upper left side of my Homepage.
In your home page you are making the Column mainAxisSize min. This means the column takes up as little room as possible. I believe it is in the row widget of your main.dart file. Rows by default have cross axis alignment set to center. Try to remove mainAxisSize in your homepage Column or add CrossAxisAlignment.start to the main.dart Row.
Try the following code:
Align(
alignment: Alignment.topLeft,
child: Text(
…
),
),

Flutter wrap to end of next line

I have a widget containing two (text) items, one of which has variable length. I want the second item to wrap to the end of the next line if space runs out.
Flutter has a widget named Wrap which allows for wrapping content if space runs out, however I have not been able to get the desired result using all kinds of combinations of Wrap, Expanded, Row and Spacer widgets. The closest I got was the second element wrapping to the start of the second row, but I want it to go to the end of the second row.
I am fairly new to Flutter but have found ways to do it in CSS by placing the variable width element in a container and applying flex: 1 0 auto to the container and flex-wrap: wrap and flex-justify: flex-end to the flexbox containing both elements.
I tried putting the first element in an Expanded, but apparently putting an Expanded directly inside a Wrap is not allowed so that gave me errors and no results.
Put the two sentences into a list like so:
List<TextSpan> reasonList = [TextSpan(text: 'sentence1'), TextSpan(text: 'sentence2') ];
Then:
Container(
child: RichText(
text: TextSpan(
children: reasonList,
style: TextStyle(
color: Colors.black, fontSize: 16)),
),
);
This not only wraps but gives you more control over every sentence, like gesture detection and color changing.

How to make flexible only distribute empty space in Column?

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

Flutter Table layout is not responding according to expected

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

Flutter row, align items individually in cross axis

In a row widget, with the crossAxisAlignment: CrossAxisAlignment.center property, all the widgets inside of the row will be vertically centred inside the Row, as expected.
But how do I do if I want only one of them aligned for exmample to the start, something like the picture below:
I can think of some ways to do it, like adding a Column in the last widget, and play with the main axis aligment, Expanded...etc etc but seems like a lot of boilerplate code for such a simple output, ther might be out there a simpler and more elegant way to achieve this??
You can wrap widget 4 in a Container and set the height as widget 3 and add Alignment.topCenter
Ah, stumbled across this and found a solution.
Wrap widgets 3 and 4 in another Row and set the inner Row to CrossAxisAlignment.start.
Row(
children: [
LargeWidget('w1'),
LargeWidget('w2'),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
LargeWidget('w3'),
SmallWidget('w4'),
],
),
],
);
This frees you from having to know the height of each widget.