0

Let's say I have a Stack widget and within that I have a bunch of Containers. The last widget of the Stack is a Text widget and I want to set the text to the height of the Stack (until the Text widget).

I've tried using a LayoutBuilder. So, I wrapped the last Text widget with a LayoutBuilder and tried to set the height to constraints.maHeight, but for some reason it gives me my screen size's height instead.

Any help is greatly appreciated.

Pranav Manoj
  • 69
  • 1
  • 6

1 Answers1

0

You can get the size of a widget using its GlobalKey to get its BuildContext and use it to find the RenderBox which contains the size and position information.

Note that you cannot do this before the build since the widget has not been rendered yet. Similarly, widgets like ListView may also cause problems.

A workaround is using Overlay widget and its OverlayEntry which gives you a stack-like widget but it renders after the whole tree renders. Which allows you to get the rendered sizes without a problem. However, there are some details on how to use it. I suggest checking out some use cases from the docs before getting started.

  • Could you share a minimal widget tree example to demonstrate ? The example on [this page](https://stackoverflow.com/questions/49307677/how-to-get-height-of-a-widget?rq=1) was too complex for me to understand – Pranav Manoj Dec 27 '20 at 11:44