8

Is there any way to measure the dimensions of a widget that hasn't been build, but it's just a variable example:

Column myColumn = Column(children[......]);

myColumn.iWouldLikeToGetItsHeightAndWidth();

  • Please take a look at this [answer](https://stackoverflow.com/a/49650741/12030116), this might help you. – iamnabink Nov 24 '20 at 16:21
  • 1
    The simplest solution is to follow the example in the doc of [BuildOwner](https://api.flutter.dev/flutter/widgets/BuildOwner-class.html). See this [answer](https://stackoverflow.com/a/74701386/7215915). That way you can use `final size = MeasureUtil.measureWidget(myColumn)` – omnesia Dec 06 '22 at 11:04

1 Answers1

0

To iterate, the post mentioned in the comment seems to be the solution of what you are looking for. As mentioned in the answer:

To get the size/position of a widget on screen, you can use GlobalKey to get its BuildContext to then find the RenderBox of that specific widget, which will contain its global position and rendered size.

Just one thing to be careful of: That context may not exist if the widget is not rendered. Which can cause a problem with ListView as widgets are rendered only if they are potentially visible.

Another problem is that you can't get a widget's RenderBox during build call as the widget hasn't been rendered yet.

To appreciate it better, you can visit the example in this blog. Take a look from the demo code:

   _getSizes() {
   }

   _getPositions(){
   }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            flex: 2,
            child: Container(
              color: Colors.red,
            ),
          ),
          Flexible(
            flex: 1,
            child: Container(
              color: Colors.purple,
            ),
          ),
          Flexible(
            flex: 3,
            child: Container(
              color: Colors.green,
            ),
          ),
          Spacer(),
          Padding(
            padding: const EdgeInsets.only(bottom: 8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                MaterialButton(
                  elevation: 5.0,
                  padding: EdgeInsets.all(15.0),
                  color: Colors.grey,
                  child: Text("Get Sizes"),
                  onPressed: _getSizes,
                ),
                MaterialButton(
                  elevation: 5.0,
                  color: Colors.grey,
                  padding: EdgeInsets.all(15.0),
                  child: Text("Get Positions"),
                  onPressed: _getPositions,
                )
              ],
            ),
          )
        ],
      ),
    );
  }

enter image description here

In this example, focus on the red panel:

Get the size of a Widget In order to do that, we need our Widget to have a Key, for this we create a GlobalKey and assign it to our

Widget.

//creating Key for red panel
GlobalKey _keyRed = GlobalKey();
...
//set key
    Flexible(
             flex: 2,
             child: Container(
               key: _keyRed,
               color: Colors.red,
             ),
     ),

Once our Widget already has a Key, we can use this Key to be able to obtain the size in the following way:

_getSizes() {
    final RenderBox renderBoxRed = _keyRed.currentContext.findRenderObject();
    final sizeRed = renderBoxRed.size;
    print("SIZE of Red: $sizeRed");
 }

If we press the Get Sizes button, you’ll get this result in the console:

flutter: SIZE of Red: Size(375.0, 152.9)

now we know that our Red panel has 375.0 as width and 152.9 as height

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65