Flutter: how to prevent device orientation changes and force portrait?

前端 未结 18 1411
鱼传尺愫
鱼传尺愫 2020-12-04 05:41

I would like to prevent my application from changing its orientation and force the layout to stick to \"portrait\".

In the main.dart, I put:

void mai         


        
18条回答
  •  渐次进展
    2020-12-04 06:14

    As of new flutter versions along with setting the preferred Orientation we need to add one extra line i.e

     WidgetsFlutterBinding.ensureInitialized();
    

    So working code for this is -

    import 'package:flutter/services.dart';
        void main() {
              WidgetsFlutterBinding.ensureInitialized();
              SystemChrome.setPreferredOrientations([
                DeviceOrientation.portraitUp,
                DeviceOrientation.portraitDown
              ]);
              runApp(MyApp());
            }
    

提交回复
热议问题