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

  runApp(new MyApp());
}
Raj008
  • 3,539
  • 2
  • 28
  • 26

3 Answers3

12

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

ibhavikmakwana
  • 8,948
  • 4
  • 20
  • 39
2

Here is the code working for both Android and iOS

Future main() async {
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}
Sunny
  • 3,134
  • 1
  • 17
  • 31
  • At time of writing this won't work on iPads https://github.com/flutter/flutter/issues/27235 – F-1 Jul 11 '19 at 08:33
-2

Following code worked for me.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/splashScreen.dart';

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);

  runApp(MaterialApp(home: splashScreen(),));
}