I am trying to add padding to a child element in Flutter, but I want the padding to be a percentage of the child’s height. The problem is that the child’s height is unknown at the time of building the widget.
For example, if the child’s height is 500px, I want to add 20% padding (100px) to the top and bottom. I have tried using the EdgeInsets.symmetric
constructor, but it only allows me to set a fixed padding value in pixels.
Is there a way to achieve this in Flutter, even if the child’s height is unknown?
Here is an example of my code:
Padding(
child: Image.network(getRandomImage()),
padding: EdgeInsets.symmetric(vertical: 50), // This should be 20% of child height
),
Any help would be greatly appreciated!
2
Answers
Use LayoutBuilder to measure child‘s height, here is an example
Try this simpler way :