Home > Blockchain >  Flutter screen becomes horizontal then portraitup when changing pages
Flutter screen becomes horizontal then portraitup when changing pages

Time:07-29

Flutter screen becomes horizontal then portraitup when changing pages. How can i fix that?

CodePudding user response:

By adding the following code to your flutter main method you will always have a landscaped screen orientation.

 WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]).then((_){
       runApp(MyApp()); //replace this with your App Widget
      });

You can change to portrait or every other orientation by changing the DeviceOrientation's field (e.g. to DeviceOrientation. portraitUp)

CodePudding user response:

You can manage the orientation on initstate and in dispose like this

@override
void initState(){
  super.initState();
  SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
  ]);
}

The above code with set the screen in landscape and in dispose will reset to auto rotate

@override
dispose(){
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  super.dispose();
}
  • Related