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

前端 未结 18 1457
鱼传尺愫
鱼传尺愫 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:16

    @boeledi, If you want to “lock” the device orientation and not allow it to change as the user rotates their phone, this was easily set as below,

    // This did not work as requirement
    void main() {
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
      runApp(new MyApp());
    }
    

    You have to wait until setPreferredOrientations is done and then start the app

    // This will works always for lock screen Orientation.
    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
        .then((_) {
          runApp(new MyApp());
        });
    }
    

提交回复
热议问题