Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive padding flutter

Im trying to pass a percentage of screen size to padding widget but it says the value should be constant, also when you just type numbers it shows different UIs in different devices. so what should we do for responsive padding.

double h = MediaQuery.of(context).size.height;

Padding(padding: const EdgeInsets.fromLTRB(h*0.2, 5.0, 50.0, 0.0),
        child:...)
like image 415
amff Avatar asked Nov 15 '25 04:11

amff


1 Answers

Remove the const keyword from your EdgeInsets. The const keyword means that Flutter knows the values that the specific widget will use beforehand. Obviously, if those values depend on the height of the device, Flutter is not able to know them until it executes your code.

Also, consider using LayoutBuilder instead of MediaQuery to get the available space constraints. In my experience, it is easier and more flexible to build the desired responsive layouts by obtaining a reference to the available screen space, instead of using the fixed device size.

like image 151
drogel Avatar answered Nov 17 '25 19:11

drogel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!