Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force the application to Portrait mode in flutter

Tags:

flutter

void main(){
  ///
  /// Force the layout to Portrait mode
  /// 
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  runApp(new MyApp());
}
like image 397
Raj008 Avatar asked Aug 17 '18 09:08

Raj008


2 Answers

Put this code in the MyApp() and don't forget to import the services package:

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

Like below:

import 'package:flutter/services.dart';

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

Update

There is a bug which prevents this code working on ipads github.com/flutter/flutter/issues/27235 as per comment by @F-1.

Below comment may help you out.

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

like image 59
ibhavikmakwana Avatar answered Oct 21 '22 06:10

ibhavikmakwana


Here is the code working for both Android and iOS

Future main() async {
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}
like image 45
Sunny Avatar answered Oct 21 '22 05:10

Sunny