I have these three containers. That the first one needs to grow to cater for the children that will be in it. The second one could have a fixed height say a flex factor of 1, while the third one has a fixed height too say flex of 2. The whole of this screen can be scrollable. It actually has to be scrollable since the children of the first container can be a lot.
Note: the children of the first container will have a fixed height, the container will house a listview or column which will have fixed height children, say containers each of 50.0. How do I achieve this, especially the 'growability' of the first container, while maintaining the heights of the other containers.
What I have currently is
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child: ListView(
children: [
Flexible(
flex: 1,
child: Container(
color: Colors.blueAccent[200],
child: Column(children: [
Container(
height: 100.0,
color: Colors.greenAccent[200],
),
Container(
height: 100.0,
color: Colors.purpleAccent[200],
),
Container(
height: 100.0,
color: Colors.orangeAccent[200],
)
]),
)),
Expanded(
flex: 1,
child: Container(
color: Colors.redAccent[100],
),
),
Expanded(
flex: 1,
child: Container(
//height: 120.0,
color: Colors.redAccent[400],
),
),
],
),
),
),
Which just brings up lots and lots of errors, does not matter how I lay it out using flexibles and Expanded widgets. Again, I don't need the first container to scroll internally, it should grow according to the number of children in it, while the rest of the two containers maintain their heights.
You can not user Expanded or Flexible inside a scrollable widget since the child of Expanded will then get a height of infinity and the height of the child of Flexible will not get its parents height to compare with.
You can set the height of the bottom two containers based on the height of the screen.
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child: ListView(
children: [
Container(
color: Colors.blueAccent[200],
child: Column(
children: [
Container(
height: 100.0,
color: Colors.greenAccent[200],
),
Container(
height: 100.0,
color: Colors.purpleAccent[200],
),
Container(
height: 100.0,
color: Colors.orangeAccent[200],
)
],
),
),
Container(
// 33 % of the screen height
height: .33 * MediaQuery.of(context).size.height,
color: Colors.redAccent[100],
),
Container(
// 33 % of the screen height
height: .33 * MediaQuery.of(context).size.height,
color: Colors.redAccent[400],
),
],
),
),
),
Related
How do I align one widget of a column in the center and put the rest around it?
If I had a Container or so that holds the three input fields and the button and another one that is the box above. How do I align the Container in the middle and put the box in the remaining space without moving the center container?
I tried using Expanded or Flexible but couldnt figure out how to align the widgets in that kind of way.
You can use Column like this:
Column(
children: [
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 40,
width: 40,
color: Colors.yellow,
),
)),
Container(
color: Colors.red,
height: 100,
width: double.infinity,
),
Expanded(
child: Align(
alignment: Alignment.topCenter,
child: Container(
height: 40,
width: 40,
color: Colors.green,
),
)),
],
),
I'm trying to create a custom widget and it makes use of the Stack widget. When I wrap the Stack's children using the Positined widget, the stack hugs the children that are not positioned. When I try to use the Align widget for more precise placement the stack expands to fill all the available space.
I'm trying to avoid using any widget with a fixed size, i.e. Container or SizedBox, as the parent for the Stack widget.
Is there a way to prevent the stack from filling all the available space or work around this that archives the same result?
I'm trying to position a child to the top center of the Stack as an example and this is where I am right now:
Using the Positined widget (I have to try multiple time to get the center position and sometimes its still off):
Positioned(
top: 0,
left: 80,
child: child,
);
Using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
This should be the result using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
You can set Stack's alignment to topCenter, Try this:
Stack(
alignment: Alignment.topCenter, // <=== add this
children: [
Container(
color: Colors.grey,
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min, // <=== add this
children: [
Container(
color: Colors.amber,
height: 30,
width: 30,
),
Container(
color: Colors.green,
height: 30,
width: 30,
),
Container(
color: Colors.blue,
height: 30,
width: 30,
),
Container(
color: Colors.yellow,
height: 30,
width: 30,
),
],
),
),
Container(
color: Colors.red,
height: 30,
width: 30,
)
],
)
Note: make sure you set Row's mainAxisSize to min.
I am working on a Flutter project. I'm trying to create a widget like the image below and I can't manage to do it.
So what I'm trying to do, is to create a widget with a Text at the top left (the size of this widget is not constant and depends on the text in it).
I'm trying to align 2 other widgets with the Text:
The Widget 1 is aligned horizontaly and centered with the Text widget and at its right with no space between
The Widget 2's right edge is aligned verticaly with the Text widget's right edge, and is positioned under it (and there is no space between the 2 widgets).
I tried to implement this with Column and Row widgets or with a GridView but I couldn't manage to do so.
Also the overall size of this custom widget has to be shrinked.
Does someone know how to do something like that ?
Thank you.
This works
Container(
width: double.infinity,
height: 300,
color: Colors.grey[200],
child: Column(
children: <Widget>[
Row(
children: [
Flexible(
flex: 2,
child: Container(
height: 200,
color: Colors.blue,
),
),
Flexible(
flex: 1,
child: Container(
height: 100,
color: Colors.green,
),
),
],
),
Row(
children: <Widget>[
Flexible(
flex: 2,
child: Align(
alignment: Alignment.centerRight,
child: Container(
width: 150,
height: 100,
color: Colors.orange,
),
),
),
Flexible(
flex: 1,
child: Container(),
),
],
),
],
),
),
Image of the problem, shown as colored rectangles
I want the first child of Column to be Flexible and the second to take up the entire remaining space. Apparently, this does not work.
I'm now aware that this is the expected behavior. I would appreciate a workaround.
I've tried using FittedBox on the first child instead of Flexible, but it didn't work as the content of the first child has unbounded width.
SizedBox(
height: 300,
width: 50,
child: Container(
color: Colors.black,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Container(
height: 100,
color: Colors.red,
),
),
Expanded(
child: Container(
color: Colors.blue,
),
),
],
),
),
);
I want to make the first child (i.e. A TextField) Flexible so that if the height gets too small, there are no ugly yellow-black overflow bars.
Update:
Below image shows what I really want to do, but it has the overflow issue because the first child isn't flexible.
shows what happens when I do use Flexible.
I'm using AnimatedContainer to change the height. I prefer not to use SizeTransition if possible.
Just replace your code with the next one (flexes ratio can be adjusted to the right one):
SizedBox(
height: 300,
width: 50,
child: Container(
color: Colors.black,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
flex:1
child: Container(
height: 100,
color: Colors.red,
),
),
Flexible(
flex:10
child: Container(
color: Colors.blue,
),
),
],
),
),
);
A regular vertical ListView would fill all the space on both axes, and shrinkWrap would make it shrink on the main axis, however, how can I make it shrink on the cross axis, just taking as much space as its biggest child needs, while being constrained by the maximum available horizontal space? Something like this:
You can't.
Children are laid out only when they are visible on screen. Which means ListView don't know the maximum cross-axis size of it's children.
If you have a small amount of children you can instead replace ListView by SingleChildSrollView and then add as a child a Column containing all your children
SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
color: Colors.red,
width: 50.0,
height: 200.0,
),
Container(
color: Colors.yellow,
width: 100.0,
height: 200.0,
),
Container(
color: Colors.red,
width: 50.0,
height: 200.0,
),
Container(
color: Colors.red,
width: 50.0,
height: 200.0,
),
],
),
),