void _rotateScreen() {
final currentOrientation = MediaQuery.of(context).orientation;
if (currentOrientation == Orientation.portrait) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
}
setState(() {});
}
when i click the button screen is rotating down but if screen is down its not come again portrait up
2
Answers
You have to define your default screen orientation in the main.dart like so:
when you need an other orientation on a specific screen (StatefulWidget) use this in your init method:
inside the on dispose method you can set the orientation back, like this:
In Flutter, you can use the
MediaQuery
class to get the currentorientation
of the device. However, this will only tell you if the device is inportrait
orlandscape
mode, not the specific orientation (up, down, left, right).Since you are checking if the
currentOrientation == Orientation.portrait
every time it returns the same value, so the result is the same. Instead of that condition, I think you can:or
And you can set the default (up or down) on
initState
.