8

I want create statefulWidget and want in this widget get parent widget height. Don't want do this by sending height data from parent widget. How can I do this?

esfsef
  • 193
  • 3
  • 11
  • 1
    by using `LayoutBuilder`? the docs say: *"Builds a widget tree that can depend on the parent widget's size."* – pskink Jul 13 '20 at 05:59
  • How can I do with ``LayoutBuilder``? – esfsef Jul 13 '20 at 06:02
  • https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html – pskink Jul 13 '20 at 06:03
  • but honestly you should use `FractionallySizedBox` - docs say: *"A widget that sizes its child to a fraction of the total available space.*" - that way you can do that with one `Widget` while using `LayoutBuilder` you need most likely two `Widget`s: `LayoutBuilder` and some `SizedBox` or `Container` – pskink Jul 13 '20 at 06:18

1 Answers1

11

Use LayoutBuilder as a child widget.

LayoutBuilder(builder: (ctx, constraints) {
      return 
          Container(
            height: constraints.maxHeight * 0.5,
            width: constraints.maxWidth * 0.5,
            child: Text('Inside Layout Builder'),
     );
  })

As per this code Container will use half of the height and width of parent widget.

Alpesh Rathod
  • 251
  • 1
  • 7