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

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

    Import package:flutter/services.dart, then

    Put the SystemChrome.setPreferredOrientations inside the Widget build() method.

    Example:

      class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          SystemChrome.setPreferredOrientations([
            DeviceOrientation.portraitUp,
            DeviceOrientation.portraitDown,
          ]);
          return new MaterialApp(...);
        }
      }
    

    Update

    This solution mightn't work for some IOS devices as mentioned in the updated flutter documentation on Oct 2019.

    They Advise to fixed the orientation by setting UISupportedInterfaceOrientations in Info.plist like this

    
        UIInterfaceOrientationPortrait
    
    

    For more information https://github.com/flutter/flutter/issues/27235#issuecomment-508995063

提交回复
热议问题