【问题标题】:How to Lock Mobile Phone Screen Orientation In Flutter? (Not in Tabs/ iPads)如何在 Flutter 中锁定手机屏幕方向? (不在选项卡/ iPad 中)
【发布时间】:2021-11-21 11:22:08
【问题描述】:

我使用颤振开发了一个应用程序。现在我只想在手机中锁定它的屏幕方向。因为我无法为移动横向模式提供功能,因为它没有足够的景观。那么,我该怎么做呢?重要的是我不想锁定选项卡/iPad 的屏幕方向。

【问题讨论】:

标签: flutter dart layout flutter-layout screen-orientation


【解决方案1】:
import 'package:flutter/services.dart'; 
       
       //When you load the Widget, do something like this:
       
       @override
       void initState(){
         super.initState();
        if(getDeviceType == ‘phone’){    
             SystemChrome.setPreferredOrientations([
                DeviceOrientation. portraitUp,
             ]);    
        }    
        else{
            SystemChrome.setPreferredOrientations([    
                DeviceOrientation. portraitUp,
                DeviceOrientation.landscapeRight,
                DeviceOrientation.landscapeLeft,
          ]);
        }
       }
       
       
       String getDeviceType() {
           final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
           return data.size.shortestSide < 600 ? 'phone' :'tablet';
         }

【讨论】:

  • 非常感谢@Jignesh 你解决了我的问题。它工作正常。
  • @ChanakaNiroshan - 如果上述答案正确,请接受。它将帮助其他人找到正确的解决方案。
  • @Sachin - 是的。这个答案是正确的,我感谢他的回答。接受正确答案还有其他事情要做吗?
猜你喜欢
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2018-10-23
相关资源
最近更新 更多