How to set landscape orientation mode for flutter app?

后端 未结 3 1364
借酒劲吻你
借酒劲吻你 2021-01-04 02:07

I was looking for code which can set orientation of my flutter app landscape forcefully.

相关标签:
3条回答
  • 2021-01-04 02:22
    import 'package:flutter/services.dart';
    
    void main()  {
       WidgetsFlutterBinding.ensureInitialized();
       SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
       .then((_) {
        runApp(new MyApp());
      });
    }
    
    0 讨论(0)
  • 2021-01-04 02:25

    Enable forcefully

    Import package: import 'package:flutter/services.dart'; in main.dart file

    1. Landscape mode:

    // Set landscape orientation
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
    

    2. Portrait mode:

    // Set portrait orientation
    SystemChrome.setPreferredOrientations([
       DeviceOrientation.portraitDown,
       DeviceOrientation.portraitUp,
    ]);
    

    0 讨论(0)
  • 2021-01-04 02:30

    SystemChrome.setPreferredOrientations is applicable for Flutter part of the app, but it's not a full solution. Because when you launching the app - Flutter are not created yet. So you should also setup orientation in the native parts.

    Here the article with detailed instructions https://medium.com/@greymag/flutter-orientation-lock-portrait-only-c98910ebd769

    0 讨论(0)
提交回复
热议问题