Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

@AliRezaeian AliRezaeian commented Oct 19, 2020

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

This comment has been minimized.

Copy link
Owner Author

@ihrankouski ihrankouski commented Nov 3, 2020

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

@AliRezaeian

This comment has been minimized.

Copy link

@AliRezaeian AliRezaeian commented Nov 3, 2020

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

@ihrankouski

This comment has been minimized.

Copy link
Owner Author

@ihrankouski ihrankouski commented Nov 4, 2020

@Sreejithns2002

This comment has been minimized.

Copy link

@Sreejithns2002 Sreejithns2002 commented Nov 20, 2020

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