How to force the Flutter application to remain in Portrait (vertical) mode even if the user is rotating the SmartPhone?

Difficulty: Beginner

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void main() async {
  ///
  /// Force the layout to Portrait mode
  /// 
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  runApp(new MyApp());
}

Explanation

To force the layout to “Portrait” mode, at application startup, set the SystemChrome.setPreferredOrientations([…]) as shown in lines #5-8.

If you want to force to “Landscape” (horizontal) mode, change lines #5-8 to:

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