Flutter, How to get the size of a widget? Flutter, How to get the size of a widget? flutter flutter

Flutter, How to get the size of a widget?


You can wrap in LayoutBuilder:

return LayoutBuilder(    builder: (BuildContext context, BoxConstraints constraints) {//      height: constraints.maxHeight,//      width: constraints.maxWidth        return YourWidget();    });


This is because the _keyRed is not attached to Context in your code.

to fix it -

 @override  Widget build(BuildContext context) {    return Scaffold(      key: _keyRed,  //Add this.      appBar: AppBar(


In the link you referred to, after creating the GlobalKey it is assigned to the red container

but in your code you haven't assigned it to the container so it can be referred to from the _getSizes() and _getPositions(), _keyRed have to be attached to a Context in your code before you use it.

//creating Key for red panelGlobalKey _keyRed = GlobalKey();    ...    //set key        Flexible(                 flex: 2,                 child: Container(                   key: _keyRed, // assigned as a key of the red container.                   color: Colors.red,                 ),         ),