【问题标题】:How to add padding using Media query in Flutter?如何在 Flutter 中使用媒体查询添加填充?
【发布时间】:2019-05-14 04:04:34
【问题描述】:

我需要根据用户的屏幕尺寸添加填充,但是在添加此代码时它显示无效的常量值,有人可以建议我替代方案或更好的解决方案。

padding: const EdgeInsets.all(MediaQuery.of(context).size.width/10),

【问题讨论】:

    标签: flutter


    【解决方案1】:

    删除const,你应该很高兴。

    padding: EdgeInsets.all(MediaQuery.of(context).size.width/10),
    

    【讨论】:

    • 如果我只需要为一侧做填充,我应该怎么做?
    • @MariamAlbarghouti 使用EdgeInsets.only(left: 8)
    • 使用 MediaQuery
    • @MariamAlbarghouti 您能否发布一个更详细的单独问题?
    【解决方案2】:
    padding: const EdgeInsets.symmetric(horizontal: MediaQuery.of(context).size.width/10), // error
    

    var width = MediaQuery.of(context).size.width;//可以全局使用,和height类似。

    padding: EdgeInsets.symmetric(horizontal:width /10,), //after removing const 
    

    【讨论】:

    • 正确的代码是:padding: E​​dgeInsets.symmetric( Horizo​​ntal: MediaQuery.of(context).size.width / 10, )
    【解决方案3】:

    填充:EdgeInsets.only(左:MediaQuery.of(context).size.width / 10);

    如果您只需要在一侧进行填充

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2020-09-26
      • 2017-06-14
      • 1970-01-01
      • 2019-09-09
      • 2018-02-15
      • 2023-04-05
      相关资源
      最近更新 更多