Skip to content

Instantly share code, notes, and snippets.

@ihrankouski
Created April 21, 2020 21:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihrankouski/0a353a8de649b648b3ceff0d8e97f1d7 to your computer and use it in GitHub Desktop.
Save ihrankouski/0a353a8de649b648b3ceff0d8e97f1d7 to your computer and use it in GitHub Desktop.
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;
}
@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