Reputation: 89
Any type of height, width, top, left, right, etc.. needs px OR %(percentage) so how can I use it. it can be a Container, Padding, SizeBox, etc.. widget.
margin: EdgeInsets.only(top: 10.0), //need 10px or 10%, so how to use ?
Upvotes: 0
Views: 3331
Reputation: 1487
Use the MediaQuery Class to get the current size of the window, as recommended by the official documentation.
Querying the current media using MediaQuery.of will cause your widget to rebuild automatically whenever the MediaQueryData changes (e.g., if the user rotates their device).
margin: MediaQuery.of(context).size.height * .10 //To get the 10%
or
margin: 10.0 //To set 10px
If you want to get the size of only a single Widget, use MediaQueryData.
Upvotes: 1
Reputation: 1533
no need to install additional packages, this can be achieved with standard flutter
double screenWidth = MediaQueryData.fromWindow(WidgetsBinding.instance!.window).size.width;
double screenHeight = MediaQueryData.fromWindow(WidgetsBinding.instance!.window).size.height;
margin: EdgeInsets.only(top: screenHeight * 0.1)
Upvotes: 0
Reputation: 277
You may use a Sizer package.
After adding Sizer package to your project.
You may use 10.h to get 10% of the height.
margin: EdgeInsets.only(top: 10.h)
Upvotes: 0