Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LayoutBuilder: constrains.maxHeight returns infinity

Tags:

flutter

dart

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!

like image 666
Karel Debedts Avatar asked Dec 18 '22 14:12

Karel Debedts


1 Answers

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

like image 117
cmd_prompter Avatar answered Feb 10 '23 02:02

cmd_prompter