PrasadM96
PrasadM96

Reputation: 591

Flutter how to disable landscape orientation?

I want to disable the landscape mode. I tried to allow only portrait mode using following code. But it is not working with my physical device. How to solve this?

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

void main() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(MyApp());
}

Upvotes: 0

Views: 3295

Answers (2)

Muhammad Anees
Muhammad Anees

Reputation: 31

add this line of code

WidgetsFlutterBinding.ensureInitialized();

before

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

Upvotes: 0

Hamza
Hamza

Reputation: 1636

You need to paste the code in Widget build(). Consider this answer for more details

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      return new MaterialApp(...);
    }
  }

Upvotes: 4

Related Questions