auto_rotate.dart 6.7 KB

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