amff
amff

Reputation: 115

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:...)

Upvotes: 4

Views: 5026

Answers (1)

drogel
drogel

Reputation: 2717

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.

Upvotes: 5

Related Questions