Block landscape mode in flutter only for one page | Flutter

Issue

I want to block the landscape mode for some pages in my application and that page should always be in portrait. I Tried some code but that make entire application portrait.

Solution

In initState you can set portrait like following.

@override
  void initState() {
    super.initState();

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

Then on dispose method you can set all mode you want like following.

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

Answered By – Chirag Kothiya

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *