Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed orientation issue when exiting full screen mode #813

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 21 additions & 52 deletions lib/src/chewie_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,41 @@ class Chewie extends StatefulWidget {

class ChewieState extends State<Chewie> {
bool _isFullScreen = false;

bool get isControllerFullScreen => widget.controller.isFullScreen;
late PlayerNotifier notifier;
late PlayerNotifier notifier; // Define the notifier here

@override
void initState() {
super.initState();
widget.controller.addListener(listener);
notifier = PlayerNotifier.init();
notifier = PlayerNotifier.init(); // Initialize the notifier here
}

@override
void dispose() {
widget.controller.removeListener(listener);
notifier.dispose();
super.dispose();
}

@override
void didUpdateWidget(Chewie oldWidget) {
if (oldWidget.controller != widget.controller) {
widget.controller.addListener(listener);
}
super.didUpdateWidget(oldWidget);
if (_isFullScreen != isControllerFullScreen) {
widget.controller._isFullScreen = _isFullScreen;
}
}
Comment on lines -61 to -70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to remove this, since if you refresh the widget with new properties, then removing this code won't update this StatefulWidget with them.


Future<void> listener() async {
if (isControllerFullScreen && !_isFullScreen) {
_isFullScreen = isControllerFullScreen;
if (widget.controller.isFullScreen && !_isFullScreen) {
_isFullScreen = true;
await _pushFullScreenWidget(context);
} else if (_isFullScreen) {
} else if (!widget.controller.isFullScreen && _isFullScreen) {
_isFullScreen = false;
Navigator.of(
context,
rootNavigator: widget.controller.useRootNavigator,
).pop();
_isFullScreen = false;
}
}

// Method to update _isFullScreen when toggling full screen mode
void toggleFullScreen() {
setState(() {
_isFullScreen = !_isFullScreen;
});
}

@override
Widget build(BuildContext context) {
return ChewieControllerProvider(
Expand Down Expand Up @@ -172,62 +165,32 @@ class ChewieState extends State<Chewie> {
if (!widget.controller.allowedScreenSleep) {
WakelockPlus.disable();
}

SystemChrome.setEnabledSystemUIMode(
SystemUiMode.manual,
overlays: widget.controller.systemOverlaysAfterFullScreen,
);
SystemChrome.setPreferredOrientations(
widget.controller.deviceOrientationsAfterFullScreen,
);
Comment on lines -176 to -182
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore these lines.

}

void onEnterFullScreen() {
final videoWidth = widget.controller.videoPlayerController.value.size.width;
final videoHeight =
widget.controller.videoPlayerController.value.size.height;

SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

// if (widget.controller.systemOverlaysOnEnterFullScreen != null) {
// /// Optional user preferred settings
// SystemChrome.setEnabledSystemUIMode(
// SystemUiMode.manual,
// overlays: widget.controller.systemOverlaysOnEnterFullScreen,
// );
// } else {
// /// Default behavior
// SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values);
// }

if (widget.controller.deviceOrientationsOnEnterFullScreen != null) {
/// Optional user preferred settings
SystemChrome.setPreferredOrientations(
widget.controller.deviceOrientationsOnEnterFullScreen!,
);
} else {
final isLandscapeVideo = videoWidth > videoHeight;
final isPortraitVideo = videoWidth < videoHeight;

/// Default behavior
/// Video w > h means we force landscape
if (isLandscapeVideo) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}

/// Video h > w means we force portrait
else if (isPortraitVideo) {
} else if (isPortraitVideo) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

/// Otherwise if h == w (square video)
else {
} else {
SystemChrome.setPreferredOrientations(DeviceOrientation.values);
}
}
Expand Down Expand Up @@ -590,6 +553,12 @@ class ChewieController extends ChangeNotifier {
void exitFullScreen() {
_isFullScreen = false;
notifyListeners();

// Reset preferred orientations to portrait mode
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
Comment on lines +558 to +561
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The controller should not be responsible for setting SystemChrome whatsoever.

This should be moved into ChewieState and called before calling this method.

Also, it should not assume that it will fall back to Portrait. Instead, there's an option in the ChewieController called deviceOrientationsAfterFullScreen that it should be set to instead.

}

void toggleFullScreen() {
Expand Down