Flutter: how to prevent device orientation changes and force portrait?

前端 未结 18 1399
鱼传尺愫
鱼传尺愫 2020-12-04 05:41

I would like to prevent my application from changing its orientation and force the layout to stick to \"portrait\".

In the main.dart, I put:

void mai         


        
相关标签:
18条回答
  • 2020-12-04 06:23

    Open android/app/src/main/AndroidManifest.xml and add the following line in the MainActivity:

    android:screenOrientation="portrait"
    

    If you have this:

    <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
    

    You should end up with something like this:

    <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize">
    

    This works for Android. On iOS, you will have to change this from the Xcode page: https://i.stack.imgur.com/hswoe.png (as Hejazi said)

    0 讨论(0)
  • 2020-12-04 06:23

    Try

     void main() async {
          WidgetsFlutterBinding.ensureInitialized();
          await SystemChrome.setPreferredOrientations(
              [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); 
        
          runApp(MyApp());
     }
    

    You can also change the screen orientation settings in the android manifest and ios info.plist file.

    0 讨论(0)
  • 2020-12-04 06:25

    iOS:

    Calling SystemChrome.setPreferredOrientations() doesn't work for me, and I had to change the Device Orientation in the Xcode project as following:

    Android:

    Set the screenOrientation attribute to portrait for the main activity in the file android/app/src/main/AndroidManifest.xml as following:

    0 讨论(0)
  • 2020-12-04 06:30

    Just add the following line of code in the main.dart file.

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    

    and remember to import services.dart file. An example is given below!

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() {
        runApp(MyApp());
    }
    
    
    class MyApp extends StatelessWidget {
         @override
         Widget build(BuildContext context) {
    
          SystemChrome.setPreferredOrientations([
            DeviceOrientation.portraitUp,
          ]);
    
          return MaterialApp(
            home: Scaffold(
              body: Center(child: Text("A Flutter Example!")),
         ),
       );
      }
    }
    
    0 讨论(0)
  • 2020-12-04 06:32

    It's possible to enable the requires fullscreen option in iOS case.

    void main() async {
          WidgetsFlutterBinding.ensureInitialized();
          SystemChrome.setPreferredOrientations([
            DeviceOrientation.landscapeRight,
            DeviceOrientation.landscapeLeft,
          ]).then((_) {
            runApp(MediaQuery(
                data: MediaQueryData(),
                child:
                    MaterialApp(debugShowCheckedModeBanner: false, home: LoginPage())));
          });
        }
    

    0 讨论(0)
  • 2020-12-04 06:33

    Import import 'package:flutter/services.dart';

    Then you include the line of code below in your main.dart file, and in your main method like so:

    WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitDown,
        DeviceOrientation.portraitUp,
      ]);
    
    runApp(myApp());
    
    
    0 讨论(0)
提交回复
热议问题