If you have a Row in Flutter and set it to mainAxisAlignment: MainAxisAlignment.spaceBetween, all Children gets evenly spaced by the distances between each other.
However, if you are conditionally displaying Widgets in this Row ((boolean) ? Display : Container()), the empty Container actually counts as a Widget that takes space with this particular mainAxisAlignment, and you will see empty space instead of Flutter just ignoring it completely and realigning as if it does not exist.
How do I keep using MainAxisAlignment.spaceBetween and use a conditional empty Widget at the same time?
You shouldn't use
(boolean) ? Display : Container()
but simply
if (boolean) Display
Related
Let's say I have this Column:
Column(
children: [
Flexible(
flex: 2,
child: Card(
child: Text("Card")
)
),
Flexible(
flex: 3,
child: Column(
children: [
// Buttons
]
)
)
]
)
What I want to achieve is that the Card always has a flex of 2, and only gets smaller if the screen (or parent) is too small to render both the card and the buttons. However, with the code above, the card gets scaled down to its minimum size to contain the text, although there is space at the end of the screen.
Replacing the card's Flexible widget with an Expanded widget would fix this, but as I already said, it will never get smaller then, which will end up in a bottom overflow if the screen gets smaller (or the buttons get bigger).
Is there any way to achieve what I want? Or is this just a limitation of Flutter's rendering system?
Edit: I want to avoid making the screen scrollable.
As you probably already know, Flexible allows its child to expand, but it does not force to do so. Viceversa, we use Expanded to force a child to occupy the remaining column/row.
Here's your options:
Use a Flexible Widget, and exploit its fit property: set it to FlexFit.tight to force it to fill the space (default is FlexFit.loose: the child can be at most as large as the available space, but is allowed to be smaller);
Use an Expanded Widget, since I sense that you want to occupy all the available space, a priori;
Mix these two approaches (Flexible on your Card, Expanded on your Buttons, or vice-versa)
In both cases, if some smaller screens are giving you trouble, consider making some elements scrollable: I don't know how big your Card is, but you could wrap it inside a SingleChildScrollView. Or, if you want try something else, you could make the whole screen scrollable with the same approach (you have to pay attention to that Column, though, as there is infinite height...)
I am trying to make a row of widgets with a small text below them mentioning what that button is for. One of the label is supposed to be multi lined but this is affecting the button in the column. How can I align them properly without it affecting the buttons placed?
My Widget Structure is something like this:
Column -> Row -> Column -> GestureDetector, Text
Try to add crossAxisAlignment: CrossAxisAlignment.start at the Row containing all the widgets.
If that doesn't work, post all of the widget parent's code
I used ListView inside Column and it causes a rendering error. When I wrapped ListView in Container or Expanded widgets, it works fine. What is the reason that causes the error?
Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
],
),
),
],
),
A ListView tries to fill all the available space given by the parent element (here column). But the column has no definite space defined.
Column's children should have a definite size. If you haven't used Expanded or shrinkWrap property of listview, it won't recognize. Hence will give you rendering error.
With Expanded, you defined that child can expand as much as it wants, giving the definite size.
Or
If you set it to shrinkwrap: true, the list will wrap its content and be as big as its children allow it to be.
So in both cases, the column will get a definite size to render.
exchange
I want to add in my project this. How can i do something like that or similar?
U can use row widgets for this and nest them
Row(
Row()
Row()
)
Use listview builder to display all the set of rows
Eg question Flutter nested Rows MainAxisAlignment
How can I use condition in widget? I did it with empty widget like container() or Text() but when I put empty Widget in row() and give spaceBetween of MainAxisAlignment it takes their own space without any showing. Searching on the internet I think there are only the old info so that's not working anymore (if I am wrong I'm really sorry).
If conditions are not allowed and I do not want to show the widget - How can i do that?
If i understand correctly, you want to render Row conditional with size of childrens.
List<Widget> elements = []
Row(children: elements)
First option is to initiate elements inside your initState(){} method.
Another one, if you want to change it dynamically, then you have to know about setState(){} method and how key works, that is optional attribute of all Stateful/Stateless Widgets.