Skip to content

Instantly share code, notes, and snippets.

@ihrankouski
Created April 21, 2020 21:52
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Chewie: "autoFullScreenOnLandscape" + "autoLandscapeOnFullScreen"
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'package:orientation/orientation.dart';
import 'package:chewie/chewie.dart';
import 'package:chewie/src/chewie_player.dart';
void main() {
runApp(
ChewieDemo(),
);
}
class ChewieDemo extends StatefulWidget {
ChewieDemo({this.title = 'Chewie Demo'});
final String title;
@override
State<StatefulWidget> createState() {
return _ChewieDemoState();
}
}
class _ChewieDemoState extends State<ChewieDemo> {
TargetPlatform _platform;
VideoPlayerController _videoPlayerController1;
ChewieController _chewieController;
ChewieFullscreenToggler _toggler;
@override
void initState() {
super.initState();
_videoPlayerController1 = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController1,
aspectRatio: 3 / 2,
autoPlay: true,
looping: true,
deviceOrientationsAfterFullScreen: [],
routePageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondAnimation, provider) {
return AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget child) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
alignment: Alignment.center,
color: Colors.black,
child: provider,
),
);
},
);
},
);
_toggler = ChewieFullscreenToggler(_chewieController);
WidgetsBinding.instance.addObserver(_toggler);
_chewieController.addListener(() {
if (_chewieController.isFullScreen && isPortrait) {
scheduleMicrotask(() {
OrientationPlugin.forceOrientation(DeviceOrientation.landscapeRight);
});
} else if (!_chewieController.isFullScreen && !isPortrait) {
scheduleMicrotask(() {
OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
});
}
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(_toggler);
_videoPlayerController1.dispose();
_chewieController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.title,
theme: ThemeData.light().copyWith(
platform: _platform ?? Theme.of(context).platform,
),
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: Chewie(
controller: _chewieController,
),
),
),
],
),
)
);
}
}
class ChewieFullscreenToggler extends WidgetsBindingObserver {
ChewieFullscreenToggler(this.chewieController) :
assert(chewieController != null);
final ChewieController chewieController;
var _wasPortrait = false;
@override
void didChangeMetrics() {
var _isPortrait = isPortrait;
if (_isPortrait == _wasPortrait) {
return;
}
_wasPortrait = _isPortrait;
if (!_isPortrait && !chewieController.isFullScreen) {
chewieController.enterFullScreen();
} else if (_isPortrait && chewieController.isFullScreen) {
chewieController.exitFullScreen();
}
}
}
bool get isPortrait {
var size = WidgetsBinding.instance.window.physicalSize;
return size.width < size.height;
}
@AliRezaeian
Copy link

Hi,

Looks like this has a bug in Android. When I landscape the phone it will be fullscreen but when I portrait it does not exit from fullscreen. Is there any way that I can fix it?

Thanks

@ihrankouski
Copy link
Author

Sorry, I ended up not using Chewie at all, nor I ever had a chance to run this code on Android.

@AliRezaeian
Copy link

Thanks for your response. What library did you end of using?

@ihrankouski
Copy link
Author

ihrankouski commented Nov 4, 2020 via email

@Sreejithns2002
Copy link

would you please share the code that you finally used? please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment