Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent device rotation only for certain classes in Flutter? [duplicate]

I'm developing a Flutter app, and I need to prevent the device rotation only in some classes, while keeping it working in others. I now how to disable the rotation globally in the app, but how to disable it only in certain classes?

like image 861
Ale TheFe Avatar asked Sep 01 '25 10:09

Ale TheFe


1 Answers

You can try using something like this in your widget:

// to lock in landscape view
@override
void initState() {
  super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]);
}

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

The fact that initState() and dispose() are used means that you have to use a StatefulWidget if that wasn't the case already.

like image 177
Guillaume Roux Avatar answered Sep 03 '25 19:09

Guillaume Roux