This is the 29th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

Here is nut front-end small classroom, if you like, you can follow my public number “nut front-end,” or add my friends, for more wonderful content

Questions lead

Group friends send questions:

How can a Flutter prohibit horizontal display? None of the methods online have worked

When a group of friends meet a problem, it is necessary for them to help. In this way, the group has the meaning of existence.

The body of the

In certain apps, we don’t want the App to rotate when the phone is landscape. In the main function, you can disable landscape mode globally by setting it like this.

code

import 'package:flutter/services.dart'; void main() async => { WidgetsFlutterBinding.ensureInitialized(); await SystemChrome.setPreferredOrientations( [ DeviceOrientation.portraitUp, / / vertical screen Portrait mode DeviceOrientation portraitDown, / / DeviceOrientation landscapeLeft, Landscape Landscape pattern / / / / DeviceOrientation landscapeRight,,); runApp(MyApp()); };Copy the code

See how you write

​
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
    .then((_) {
      runApp(new MyApp());
    });
}
Copy the code

However, in some requirements, landscape mode is not completely disabled. For example, when opening a web page, it can be landscape in the WebView scenario, but it cannot be landscape in other interfaces. How do you set that up?

  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }
  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    super.dispose();
  }
Copy the code

Like this, you can just put it in the initState and Dispose of a StatefulWidget. So in my code, for example, I put a WebPage on the WebView and call WebPage, so that when the user enters the WebPage, WebPage is landscape, but when he clicks back, WebPage forces him to resume portrait.

If it does not work, you can do the following separately

IOS:

The android:

Android/app/SRC/main/AndroidManifest. XML ` as shown below:

Also, there is one on pub.dev

Plug-in orientation

Pub. Flutter – IO. Cn/packages/the or…

You can try it. I won’t explain it here.

We think to help you, pay attention to oh!

Of course, for the code of life, it will not be executed according to your idea, and it is full of too many unknowns and anomalies. If you are not careful, there will be errors, and problems must be dealt with. Although the DeBug process is very difficult and painful, it will be another beautiful scene after successfully solving it. Nuts, refueling

\