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.
Question
What does the incoming constraint mean in Flutter?
According to Google's flutter documentation on understanding constraints:
A widget gets its own constraints from its parent [the "incoming constraint" you are asking about]. A constraint is just a set of 4 doubles: a minimum and maximum width, and a minimum and maximum height.
Then the widget goes through its own list of children. One by one, the widget tells its children what their constraints are (which can be different for each child), and then asks each child what size it wants to be.
Then, the widget positions its children (horizontally in the x axis, and vertically in the y axis), one by one.
And, finally, the widget tells its parent about its own size (within the original constraints, of course).
I suggest you read that entire URL for more details. The Google people are writing some amazingly clear documentation.
Related
Is there some widget that makes it easy to swap out one child in a Row for another smaller one in case the width is not enough to fit the bigger one?
In the image below, the Widget B is a full width version, and D is a mini version of the same widget.
Note that I do not want the widget to adjust its size seamlessly like a Flexible or Expanded - I want it to be either as wide as B, or if that doesn't fit I want it to be as wide as D - not anything in between.
I know I can do this using a LayoutBuilder and a hardcoded condition to check if the available width is at least X pixels. But is there a widget out there that does it without needing a hardcoded min-width? Just adapting to the actual width of B?
You can use a stateful widget and use addPostFrameCallback to get the size from the context:
WidgetsBinding.instance?.addPostFrameCallback((_) {
if (context.size is Size) {
print('My context size is: ${context.size}');
}
});
If you want to know the sizes of both widgets, you can assign a global key to each, and then use a RenderBox to get their sizes.
Any solution that you go with to dynamically get the size of widgets will necessarily mean that they will get laid out, which may not be cheap, depending on the widgets themselves.
In flutter, Is there any difference between these layouts? -
Expanded
SizedBox.expand
BoxConstraints.expand()
If all are same, why there are multiple layout classes/functions for same functionality?
As far as I know.
Expanded widget can only used inside of Row, Column and Flex. It helps to expand the children based on the flex value. By default - it's 1.
SizedBox.expand - It will expand the child as large as it's parent allows. You can put anywhere unlike Expanded.
BoxConstraints.expand - Usually takes as object of ConstrainedBox constraints property.
From the documentation -
Creates box constraints that expand to fill another box constraints.
If width or height is given, the constraints will require exactly the
given value in the given dimension.
My Flutter UI has a Row widget with and arbitrary number Widgets in it. I would like to move all of those widgets over to the right by a fixed amount. But the caveat is, if the other widgets grow in width such that the available horizontal space is consumed, the spacing widget will relinquish its space.
The Spacer widget does not work for me, as it does not allow you to specify a fix maximum. It only allows a flex value, which is a function of the width of the other content in the row. I want this spacer to take up a fixed amount of space regardless of the width of the other content of the row (unless all the room is used up).
Try using sizedBox or FractionallySizedBox as explained in this answer
https://stackoverflow.com/a/63430801/8213343
In a flutter app, what is the difference between a Container inside a Row and a Row inside Container?
In flutter in each time I want to make an app, I took minutes to ask my self
which one should I choose?
In flutter Row and Column provide a way to use the Flex layout system aka FlexBox.
In short Flex is a one-dimensional layout system, where in the children can be aligned on the main-axis or cross-axis, the main axis can be defined as horizontal or vertical.
A Container in Flutter is a general-purpose widget that gives you access to a range of properties like padding, decoration, size etc. A full-list is available at the API Docs
To answer your question,
A Container in a Row is like we said above, a child of a Flex, which means it will respect the properties of alignment set by the parent (MainAxisAlignment/CrossAxisAlignment )
A Row in a Container, is the opposite of that which means that, the Row or Column will respect properties set on the Container that is for example the Decoration or Size properties amongst others.
Should we consider the Column widget constrained or unconstrained in the vertical direction?
According to the document of the Column widget:
When the contents of a Column exceed the amount of space available,
the Column overflows, and the contents are clipped.
To my understanding, this means that the Column widget is constrained (or bounded) vertically.
On the other hand, the documentation of the Center widget says:
If a dimension is unconstrained and the corresponding size factor is
null then the widget will match its child's size in that dimension.
When I place the Center widget in the children of a Column, what happens is that the Center widget shrinks its height to match the height of its child. According to the above document, this means that the Column widget is unconstrained in the vertical direction.
So which take is correct? Is the Column widget constrained or unconstrained in the vertical direction?
I think you're mixing the parent constraints with the children constraints.
The box constraints that the column itself receives from its parent is different from the one it passes on to its children: the column may be constrained in height by its parent, but it gives its children an infinite (unconstrained) height. Therefore..
To my understanding, this means that the Column widget is constrained (or bounded) vertically.
Yes, the column is constrained by its parent.
According to the above document, this means that the Column widget is unconstrained in the vertical direction.
Not the column itself but its children, on the other hand, are not constrained in height. This explains why the Center shrinks on the vertical direction to match its child.