Flutter 如何根据不同的屏幕大小添加填充或边距

Flutter 如何根据不同的屏幕大小添加填充或边距,flutter,dart,responsive-design,responsive,Flutter,Dart,Responsive Design,Responsive,我想根据屏幕大小给出填充或边距 下面是两个具有相同填充的不同大小屏幕的代码和图像 这是我的代码: Padding( padding: const EdgeInsets.only(top: 160, left: 90, bottom: 20), child: Row( children: <Widget>[ Image.asset( '

我想根据屏幕大小给出填充或边距

下面是两个具有相同填充的不同大小屏幕的代码和图像

这是我的代码:

    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,
                  ),
                ),
              ],
            ),
          ),
填充(
填充:仅限常量边集(顶部:160,左侧:90,底部:20),
孩子:排(
儿童:[
影像资产(
“assets/logo.png”,
身高:70,
宽度:70,
),
正文(
“Whatsapp”,
样式:TextStyle(
尺寸:26,
颜色:颜色,白色,
fontWeight:fontWeight.bold,
),
),
],
),
),
我真正的设备

Android Studio模拟器


您可以使用
MediaQuery.of(context).size.height
获取屏幕高度,使用
MediaQuery.of(context).size.width
获取屏幕宽度


根据您的图片,最好使用Flex小部件,如调整列内小部件之间的间距。

您可以创建两个接受BuildContext的方法

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

double deviceWidth(BuildContext context) => MediaQuery.of(context).size.width;
如果您想要统一的边距,而不考虑设备的宽度或高度,请这样使用

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,
              ),
            ),
          ],
        ),
      ),
填充(
填充:仅限边缘设置(
顶部:设备高度(上下文)*0.3,
左:设备宽度(上下文)*0.09,
底部:设备高度(上下文)*0.06,
),
孩子:排(
儿童:[
影像资产(
“assets/logo.png”,
身高:70,
宽度:70,
),
正文(
“Whatsapp”,
样式:TextStyle(
尺寸:26,
颜色:颜色,白色,
fontWeight:fontWeight.bold,
),
),
],
),
),
deviceHeight(context)*0.3仅表示屏幕高度的30%,
deviceWidth(context)*0.09
表示屏幕宽度的9%&
底部:deviceHeight(context)*0.06
表示屏幕高度的6%


这里的优点是,您可以调整图形以匹配,并且在任何设备上都将采用相同的间距。

我遵循与您在此处显示的完全相同的顺序,并且显示了一个错误-无效的常量值。注意,您不能在常量表达式中使用上述内容。例如:
const容器(高度:设备高度(上下文)*0.6,子级:文本(“Hello World!”),)这将导致错误。因为在编译过程中无法确定该函数的值。