Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Renderboxes passing down Infinity height?

Tags:

flutter

dart

I'm getting confused with flutter's render boxes. Here's what I understand: The layouting algorithm starts at the root and traverses down the widget tree, passing down constraints to the children. Flex boxes, in bounded constraints in their scroll direction, try to match their parent constraints.

Consider the following code:

Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      print("Parent");
      print(constraints.maxWidth);
      print(constraints.maxHeight);
      return Column(children: [
        LayoutBuilder(builder: (context, constraints) {
          print("Child");
          print(constraints.maxWidth);
          print(constraints.maxHeight);
          return SizedBox(
            height: 12,
          );
        })
      ]);
    });
  }

This prints

flutter: Parent
flutter: 320.0
flutter: 568.0
flutter: Child
flutter: 320.0
flutter: Infinity

How does the height constraints suddenly become Infinity? Shouldn't it match its parent and be 568.0?

like image 969
deekay42 Avatar asked Nov 06 '22 21:11

deekay42


1 Answers

LayoutBuilder parent size is important, if size of parent isn't clear, constraints sizes will be infinity. for example if you wrap your widget with scaffold, LayoutBuilder provides exact size.

like image 164
Hossein Avatar answered Dec 06 '22 15:12

Hossein