2

I wanna force the orientation of some pages of my application. Like, I want the orientation to be Portrait for the landing page, the options of my game, and the player selection, but I want to force the orientation to be landscape when the game begins... I tried using:

super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
])

and it does work on android, (except that it change the orientation 3 times before getting the right orientation...) But on iOS, it doesn't force the change... It only change when you turn the device and then it works and stays in landscape mode.

Does anyone had the same issue and know how to fix it ?

I tried to put the SystemChrome.setPreferredOrientations in the Widget build function but it didn't worked.

Simon B
  • 503
  • 12
  • 29
  • Please look at https://stackoverflow.com/a/50884081/4975404 I think it has the answer you are looking for at the bottom of the answer. – Mason Oct 16 '20 at 03:47

2 Answers2

-1

Please try this.

void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
    .then((_) {
      runApp(new MyApp());
    });
}

2nd Way:

await SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);

The list of supported orientations are:

landscapeLeft
landscapeRight
portraitDown
portraitUp
  • Doesn't work... It doesent turn the view, it just do when you change the orientation of the phone physically. I want to do it even if the phone is in portrait – Simon B Nov 11 '19 at 14:38
-1

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(...);
    }
  }
Maadhav Sharma
  • 559
  • 1
  • 7
  • 18