Home > Net >  In flutter if i want to put height 10px or 10% so how should i used it?
In flutter if i want to put height 10px or 10% so how should i used it?

Time:05-17

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 ?

CodePudding user response:

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)

CodePudding user response:

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.

CodePudding user response:

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)
  • Related