【问题标题】:How to set different values to a property based on the device orientation如何根据设备方向为属性设置不同的值
【发布时间】:2022-11-22 13:05:16
【问题描述】:

我想根据屏幕方向更改 Positioned 小部件的 top 属性值。

我不知道该怎么做。

谁能帮忙?

【问题讨论】:

    标签: android flutter dart mobile-development


    【解决方案1】:

    用户MediaQuery.of(context).orientation
    它可以响应设备的方向。

    Positioned(
      top: MediaQuery.of(context).orientation == Orientation.portrait ? portraitValue : landScapeValue,
    child: //your code
    );
    

    【讨论】:

      【解决方案2】:

      official flutter article 开始,您可以使用 OrientationBuilder 根据设备方向管理您的小部件,如下所示:

        OrientationBuilder(
        builder: (context, orientation) {
          return Positioned(
            top: orientation == Orientation.portrait ? 10 : 50,
            child: /*...0*/
          );
        },
      ),
      

      用您的值更改1050

      如果您愿意在应用程序中的小部件以外的其他地方获取设备的方向,例如方法,您可以使用 MediaQuery.of(context).orientation 来获取设备的实际方向。

      printOrientation() {
        print(MediaQuery.of(context).orientation);
       }
      

      检查此以获取更多信息:

      OrientationBuilder()

      MediaQuery.of(context).orientation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 2011-11-27
        • 1970-01-01
        相关资源
        最近更新 更多