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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With