How to stretch one of the TableRow's children to the maximum available height? - flutter

Code:
Table(
children: [
TableRow(
children: [
Container(height: 100, color: Colors.red),
Container(color: Colors.green), // Read 'Q1' below.
Container(height: 100, color: Colors.blue),
],
),
],
)
Q1: How to stretch green Container height to the maximum height of the given TableRow?
PS: For simplicity, I provided manual heights to the container1 and the container2 but in real world, I don't know their heights. Second, I know I can find the height of both the Container using RenderBox and then I can find out the max of (container1, container3) and apply that to the 2nd one but that would be too much of work, I'm looking for a better approach.

Use TableCell and TableCellVerticalAlignment.fill.
TableCell:
A widget that controls how a child of a Table is aligned.
A TableCell widget must be a descendant of a Table, and the path from the TableCell widget to its enclosing Table must contain only TableRows, StatelessWidgets, or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).
TableCellVerticalAlignment.fill:
Cells with this alignment are sized to be as tall as the row, then made to fit the row.If all the cells have this alignment, then the row will have zero height.
Table(
children: [
TableRow(
children: [
Container(height: 100, color: Colors.red),
TableCell(
verticalAlignment: TableCellVerticalAlignment.fill,
child: Container(color: Colors.green),
),
Container(height: 100, color: Colors.blue),
],
),
],
)

Related

Starting a Column from the center of its parent

I am trying to implement a Column within a parent, but the first child of the Column should be centered vertically within the Column's parent, by using MainAxisAlignment.center the whole Column will be centered and if I have more than one child in the Column, the first child won't be in the center.
Here is what i have tried.
child: CircleAvatar(
radius: 146,
backgroundColor: Colors.black87,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('11:06',
style: TextStyle(
fontSize: 70,
)),
Icon(Icons.restart_alt),
],
),
),
Here is what i got.enter image description here
As you see the text is not in the center because the icon pushes it up a bit.
You can wrap your column in another column containing an Expanded widget followed by your column, also wrapped in an Expanded widget.
You can use some padding. I am not aware of a way to get the size of the parent and do like 50% of it like in CSS so you have to keep that in mind. However you can make calculations relative to the viewport height with MediaQuery.of(context).size.height.
Use Align inside Stack (not perfect, still needs adjustment).
CircleAvatar(
...
child: Stack(
alignment: Alignment.center,
children: const [
Text('11:06', style: TextStyle(fontSize: 70)),
Align(
alignment: Alignment(0, 0.4),
child: Icon(Icons.restart_alt),
),
],
),
)

Containers with dynamic sizes inside a listview widget

I'm creating a listview widget and i want it to have varying container sizes that change from user inputs inside it, the problem is when i wrap my columns or rows in expanded widgets, the code breaks.
is there a way to set a min and max height for a list view widget?
ListView(
children: [
Column(
children: [
Row(
children: [
Expanded(
child: Container(
color: Colors.yellow,
),
),
Container(
width: 2,
color: kKillTeamOrange,
),
Expanded(
child: Container(
color: Colors.green,
),
),
],
),
],
),
],
),
I tried wrapping the listview in a constrained container but no luck, the other option would be to have the buttons set the height state but there are a lot of buttons to code that. and using expanded widgets is great for varying screen sizes.
any help would be greatly appreciated.
cheers
Two ways came to mind:
CustomScrollView
ListView (But using cards)
Both ways are responsive to screen sizes
LISTVIEW WITH CARD:
CUSTOM SCROLLVIEW:
RESULT:
hope this helps...

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

Flutter - Reduce size of row to necessary space

I would like to create a container that contains a row of
a text widget, a sizedBox and an IconButton (to delete this container).
The container should size itself to the children, that means the row
Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this. (From the flutter webpage.)
The row expands as much as possible, but I would like it to be as small as possible. The container (in particular its borders) should be as small as possible
Widget chipForm(String chipText){
return Container(
decoration: BoxDecoration(
border: Border.all(
width: 3.0
),
borderRadius: BorderRadius.all(
Radius.circular(5.0)
),
),
child: Row(
children: [
Text(chipText),
SizedBox(width : 10),
IconButton(
icon: Icon(Icons.clear),
onPressed: (){},
)
],
),
);
}
How do I do that?
The Row widget has a property mainAxisSize which is
How much space should be occupied in the main axis.
Set this to MainAxisSize.min which
Minimize[s] the amount of free space along the main axis, subject to the incoming layout constraints.
to shrink the Row as much as possible.
Example:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(chipText),
SizedBox(width : 10),
IconButton(
icon: Icon(Icons.clear),
onPressed: (){},
)
],
),
You should check the documentation before asking here.

Flutter: How to adjust the height of a container as the maximum height of its sibling in a row

I have two widgets in a row. The first widget is a black line with width 5.0, the second widget is a text widget whose size may vary according to the content. I want the first container to have the same height as of the second widget
One way is to make both widget child of an IntrinsicHeight widget and then declare the height of the first container as double.infinity. This should solve your problem. Example code is given below:
IntrinsicHeight(
child: Row(
children: <Widget>[
Container( //this is the first container
height: double.infinity
),
secondWidget(
)
],
)
Let me know if you have further query. Happy coding!
You can use Expanded widget. It divides siblings to the same height or width.
here is the code:
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.red,
),
),
Expanded(
child: Container(
color: Colors.green,
),
)
And here is the result:
That is so easy. I wish it could help.