auto_rotate.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import 'dart:io';
  2. import 'package:auto_orientation/auto_orientation.dart';
  3. import 'package:chewie/chewie.dart';
  4. import 'package:chewie/src/chewie_player.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:video_player/video_player.dart';
  9. void main() {
  10. runApp(
  11. ChewieDemo(),
  12. );
  13. }
  14. class ChewieDemo extends StatefulWidget {
  15. ChewieDemo({this.title = 'Chewie Demo'});
  16. final String title;
  17. @override
  18. State<StatefulWidget> createState() {
  19. return _ChewieDemoState();
  20. }
  21. }
  22. class _ChewieDemoState extends State<ChewieDemo> {
  23. TargetPlatform _platform;
  24. VideoPlayerController _videoPlayerController1;
  25. VideoPlayerController _videoPlayerController2;
  26. ChewieController _chewieController;
  27. @override
  28. void initState() {
  29. super.initState();
  30. _videoPlayerController1 = VideoPlayerController.network(
  31. 'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
  32. _videoPlayerController2 = VideoPlayerController.network(
  33. 'https://www.sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4');
  34. _chewieController = ChewieController(
  35. videoPlayerController: _videoPlayerController1,
  36. aspectRatio: 3 / 2,
  37. autoPlay: true,
  38. looping: true,
  39. routePageBuilder: (BuildContext context, Animation<double> animation,
  40. Animation<double> secondAnimation, provider) {
  41. return AnimatedBuilder(
  42. animation: animation,
  43. builder: (BuildContext context, Widget child) {
  44. return VideoScaffold(
  45. child: Scaffold(
  46. resizeToAvoidBottomPadding: false,
  47. body: Container(
  48. alignment: Alignment.center,
  49. color: Colors.black,
  50. child: provider,
  51. ),
  52. ),
  53. );
  54. },
  55. );
  56. }
  57. // Try playing around with some of these other options:
  58. // showControls: false,
  59. // materialProgressColors: ChewieProgressColors(
  60. // playedColor: Colors.red,
  61. // handleColor: Colors.blue,
  62. // backgroundColor: Colors.grey,
  63. // bufferedColor: Colors.lightGreen,
  64. // ),
  65. // placeholder: Container(
  66. // color: Colors.grey,
  67. // ),
  68. // autoInitialize: true,
  69. );
  70. }
  71. @override
  72. void dispose() {
  73. _videoPlayerController1.dispose();
  74. _videoPlayerController2.dispose();
  75. _chewieController.dispose();
  76. super.dispose();
  77. }
  78. @override
  79. Widget build(BuildContext context) {
  80. return MaterialApp(
  81. title: widget.title,
  82. theme: ThemeData.light().copyWith(
  83. platform: _platform ?? Theme.of(context).platform,
  84. ),
  85. home: Scaffold(
  86. appBar: AppBar(
  87. title: Text(widget.title),
  88. ),
  89. body: Column(
  90. children: <Widget>[
  91. Expanded(
  92. child: Center(
  93. child: Chewie(
  94. controller: _chewieController,
  95. ),
  96. ),
  97. ),
  98. FlatButton(
  99. onPressed: () {
  100. _chewieController.enterFullScreen();
  101. },
  102. child: Text('Fullscreen'),
  103. ),
  104. Row(
  105. children: <Widget>[
  106. Expanded(
  107. child: FlatButton(
  108. onPressed: () {
  109. setState(() {
  110. _chewieController.dispose();
  111. _videoPlayerController2.pause();
  112. _videoPlayerController2.seekTo(Duration(seconds: 0));
  113. _chewieController = ChewieController(
  114. videoPlayerController: _videoPlayerController1,
  115. aspectRatio: 3 / 2,
  116. autoPlay: true,
  117. looping: true,
  118. );
  119. });
  120. },
  121. child: Padding(
  122. child: Text("Video 1"),
  123. padding: EdgeInsets.symmetric(vertical: 16.0),
  124. ),
  125. ),
  126. ),
  127. Expanded(
  128. child: FlatButton(
  129. onPressed: () {
  130. setState(() {
  131. _chewieController.dispose();
  132. _videoPlayerController1.pause();
  133. _videoPlayerController1.seekTo(Duration(seconds: 0));
  134. _chewieController = ChewieController(
  135. videoPlayerController: _videoPlayerController2,
  136. aspectRatio: 3 / 2,
  137. autoPlay: true,
  138. looping: true,
  139. );
  140. });
  141. },
  142. child: Padding(
  143. padding: EdgeInsets.symmetric(vertical: 16.0),
  144. child: Text("Video 2"),
  145. ),
  146. ),
  147. )
  148. ],
  149. ),
  150. Row(
  151. children: <Widget>[
  152. Expanded(
  153. child: FlatButton(
  154. onPressed: () {
  155. setState(() {
  156. _platform = TargetPlatform.android;
  157. });
  158. },
  159. child: Padding(
  160. child: Text("Android controls"),
  161. padding: EdgeInsets.symmetric(vertical: 16.0),
  162. ),
  163. ),
  164. ),
  165. Expanded(
  166. child: FlatButton(
  167. onPressed: () {
  168. setState(() {
  169. _platform = TargetPlatform.iOS;
  170. });
  171. },
  172. child: Padding(
  173. padding: EdgeInsets.symmetric(vertical: 16.0),
  174. child: Text("iOS controls"),
  175. ),
  176. ),
  177. )
  178. ],
  179. )
  180. ],
  181. ),
  182. ),
  183. );
  184. }
  185. }
  186. class VideoScaffold extends StatefulWidget {
  187. const VideoScaffold({Key key, this.child}) : super(key: key);
  188. final Widget child;
  189. @override
  190. State<StatefulWidget> createState() => _VideoScaffoldState();
  191. }
  192. class _VideoScaffoldState extends State<VideoScaffold> {
  193. @override
  194. void initState() {
  195. if(Platform.isIOS){
  196. AutoOrientation.landscapeLeftMode();
  197. }else{
  198. SystemChrome.setPreferredOrientations([
  199. DeviceOrientation.landscapeRight,
  200. DeviceOrientation.landscapeLeft,
  201. ]);
  202. }
  203. super.initState();
  204. }
  205. @override
  206. dispose() {
  207. SystemChrome.setPreferredOrientations([
  208. DeviceOrientation.portraitUp,
  209. DeviceOrientation.portraitDown,
  210. ]);
  211. AutoOrientation.portraitUpMode();
  212. super.dispose();
  213. }
  214. @override
  215. Widget build(BuildContext context) {
  216. return widget.child;
  217. }
  218. }