VipiN Negi
VipiN Negi

Reputation: 3108

Locked orientation overridden by Youtube Player Flutter

I restricted my app for only portrait mode. The orientation was locked until I played a video full screen within my app with Youtube plugin. Once I exit the video, the app now can go landscape orientation as well if I tilt my phone.

Below code, I used for orientation restriction:

void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
  runApp(new MyApp());
});
}

Any Ideas, how can I keep the orientation locked?

Upvotes: 0

Views: 2608

Answers (3)

DeePanShu
DeePanShu

Reputation: 1326

If you not want to go back from whole screen just change the orientation on back button use the given below code:

Future<bool> _checkForLandscape(BuildContext ctx) async {
if (MediaQuery.of(ctx).orientation == Orientation.landscape) {
  await SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
    ],
  );
  _controller.toggleFullScreenMode();
  return false;
}
return true;

}

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => _checkForLandscape(context),
      child: _anyChild());}

Upvotes: 1

Dakroub_M
Dakroub_M

Reputation: 122

Use this on dispose:

@override
  void dispose() {
    _controller.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }

Upvotes: 3

Vinz
Vinz

Reputation: 240

Using SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); on build method

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    return MaterialApp(...);
   }

Upvotes: 1

Related Questions