I create the video screen, and when the device rotates, the video player also rotates. I have used the youtube player flutter library with auto-rotation enabled. When the device rotates from portrait to landscape, the video player rotates accordingly. However, when the device rotates from landscape to portrait, the rotation does not occur automatically, although it works manually. The code below is used in the build widget.
// Auto rotation Setup
if (isAutoRotation) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
} else {
if (isFullScreenClick) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
}
isFullScreenClick Boolean variable value get from the library fullscreen on that time true and then false.
void _onPlayerStateChange() {
setState(() {
//Value update from the library. isFullScreen or not.
isFullScreenClick = _controller.value.isFullScreen;
});
}
Below code default library method I handle the isAutoRotation flag.
child: YoutubePlayerBuilder(
onEnterFullScreen: () {
setState(() {
isAutoRotation = false;
});
},
onExitFullScreen: () {
setState(() {
// Click on the full screen is AutorotationFlag on.
isAutoRotation = true;
});
//Navigator.pop(context, true);
},
2
Answers
I’m using
youtube_player_flutter: ^8.1.2
(Link). The screen rotation feature is working as expected. I initially turned ON screen rotation in my emulator & simulator. By the way, I tested this on Android emulator & iOS simulator. Try & test the below-mentioned code & provide acknowledgement for same.Updated: as you requested. As your app is always in portrait & doesn’t allow automatic screen rotation, I made a few changes & improvements in my previous answer. Try & test the below-mentioned code & provide acknowledgement for same. If you think I misunderstood, feel free to add a comment.