setOrientation function
Locks the screen orientation.
isPortrait determines whether the orientation should be portrait.
allowRotation if true, enables upside-down or both landscape directions depending on isPortrait.
Examples:
- Portrait only:
setOrientation() - Portrait with upside down:
setOrientation(allowRotation: true) - Landscape only:
setOrientation(isPortrait: false) - Landscape with both directions:
setOrientation(isPortrait: false, allowRotation: true)
Implementation
void setOrientation({
bool isPortrait = true,
bool allowRotation = false,
}) {
final orientations = <DeviceOrientation>[];
if (isPortrait) {
orientations.add(DeviceOrientation.portraitUp);
if (allowRotation) orientations.add(DeviceOrientation.portraitDown);
} else {
orientations.add(DeviceOrientation.landscapeLeft);
if (allowRotation) orientations.add(DeviceOrientation.landscapeRight);
}
SystemChrome.setPreferredOrientations(orientations);
}