Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add padding or margin according to different screen sizes

I want to give padding or margin according to screen sizes.

Below is code and images of two different sizes screens with same padding.

This is my code:

    Padding(
            padding: const EdgeInsets.only(top: 160, left: 90, bottom: 20),
            child: Row(
              children: <Widget>[
                Image.asset(
                  'assets/logo.png',
                  height: 70,
                  width: 70,
                ),
                Text(
                  '  Whatsapp',
                  style: TextStyle(
                    fontSize: 26,
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),

My real device

enter image description here

Android Studio emulator

enter image description here

like image 986
Arjun Mahar Avatar asked Mar 02 '23 11:03

Arjun Mahar


1 Answers

You can create two methods that accept BuildContext

double deviceHeight(BuildContext context) => MediaQuery.of(context).size.height;

double deviceWidth(BuildContext context) => MediaQuery.of(context).size.width;

And if you want uniform margin regardless of device width or height, use it like this

Padding(
        padding: EdgeInsets.only(
          top: deviceHeight(context) * 0.3,
          left: deviceWidth(context) * 0.09,
          bottom: deviceHeight(context) * 0.06,
        ),
        child: Row(
          children: <Widget>[
            Image.asset(
              'assets/logo.png',
              height: 70,
              width: 70,
            ),
            Text(
              '  Whatsapp',
              style: TextStyle(
                fontSize: 26,
                color: Colors.white,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),

deviceHeight(context) * 0.3 simply means 30% of the screen Height, deviceWidth(context) * 0.09 means 9% of the screen Width & bottom: deviceHeight(context) * 0.06 means 6% of the screen Height

The advantage here is that you can adjust the figures to match, and it will take equal spacing on any device.

like image 195
Brendan Avatar answered Mar 05 '23 03:03

Brendan