Get parent widget height in flutter
Asked Answered
C

1

10

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?

Chrysalis answered 13/7, 2020 at 5:56 Comment(4)
by using LayoutBuilder? the docs say: "Builds a widget tree that can depend on the parent widget's size."Kilburn
How can I do with LayoutBuilder?Chrysalis
api.flutter.dev/flutter/widgets/LayoutBuilder-class.htmlKilburn
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 Widgets: LayoutBuilder and some SizedBox or ContainerKilburn
D
13

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.

Dejadeject answered 13/7, 2020 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.