auto_rotate.dart 6.8 KB

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