I'm trying to get the constrains.maxHeight.
This is the code I use:
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
print(constraints.maxHeight);
return Column(
children: <Widget>[
LayoutBuilder(builder:
(BuildContext context, BoxConstraints constraints) {
print(constraints.maxHeight);
return Text(constraints.maxHeight.toString());
}),
],
);
}),
The first one gives: 812, but the second LayoutBuilder gives infinity. (my Code is simplified, I also use other things in the column, but tot test, I only put the LayoutBuilder in it.)
What am I doing wrong?
Thanks!
The constraints are infinity or unbounded for elements placed in a Column
. Wrap the LayoutBuilder
with Expanded
widget and try.
In Flutter, widgets are rendered by their underlying RenderBox objects. Render boxes are given constraints by their parent, and size themselves within those constraints. Constraints consist of minimum and maximum widths and heights; sizes consist of a specific width and height.
Generally, there are three kinds of boxes, in terms of how they handle their constraints:
Those that try to be as big as possible. For example, the boxes used by Center and ListView. Those that try to be the same size as their children. For example, the boxes used by Transform and Opacity. Those that try to be a particular size. For example, the boxes used by Image and Text. Some widgets, for example Container, vary from type to type based on their constructor arguments. In the case of Container, it defaults to trying to be as big as possible, but if you give it a width, for instance, it tries to honor that and be that particular size.
Others, for example Row and Column (flex boxes) vary based on the constraints they are given, as described below in the “Flex” section.
documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With