player_with_controls.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'dart:async';
  2. import 'dart:ui';
  3. import 'package:chewie/src/chewie_progress_colors.dart';
  4. import 'package:chewie/src/cupertino_controls.dart';
  5. import 'package:chewie/src/material_controls.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:video_player/video_player.dart';
  9. class PlayerWithControls extends StatefulWidget {
  10. final VideoPlayerController controller;
  11. final Future<dynamic> Function() onExpandCollapse;
  12. final bool fullScreen;
  13. final ChewieProgressColors cupertinoProgressColors;
  14. final ChewieProgressColors materialProgressColors;
  15. final Widget placeholder;
  16. final double aspectRatio;
  17. final bool autoPlay;
  18. final bool showControls;
  19. PlayerWithControls({
  20. Key key,
  21. @required this.controller,
  22. @required this.onExpandCollapse,
  23. @required this.aspectRatio,
  24. this.fullScreen = false,
  25. this.showControls = true,
  26. this.cupertinoProgressColors,
  27. this.materialProgressColors,
  28. this.placeholder,
  29. this.autoPlay,
  30. }) : super(key: key);
  31. @override
  32. State createState() {
  33. return new _VideoPlayerWithControlsState();
  34. }
  35. }
  36. class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
  37. @override
  38. Widget build(BuildContext context) {
  39. final controller = widget.controller;
  40. return new Center(
  41. child: new Container(
  42. width: MediaQuery.of(context).size.width,
  43. child: widget.fullScreen &&
  44. MediaQuery.of(context).orientation == Orientation.landscape
  45. ? _buildPlayerWithControls(controller, context)
  46. : new AspectRatio(
  47. aspectRatio: widget.aspectRatio,
  48. child: _buildPlayerWithControls(controller, context),
  49. ),
  50. ),
  51. );
  52. }
  53. Container _buildPlayerWithControls(
  54. VideoPlayerController controller, BuildContext context) {
  55. return new Container(
  56. child: new Stack(
  57. children: <Widget>[
  58. widget.placeholder ?? new Container(),
  59. new Center(
  60. child: new Hero(
  61. tag: controller,
  62. child: widget.fullScreen &&
  63. MediaQuery.of(context).orientation ==
  64. Orientation.landscape
  65. ? new VideoPlayer(controller)
  66. : new AspectRatio(
  67. aspectRatio: widget.aspectRatio,
  68. child: new VideoPlayer(controller),
  69. ),
  70. ),
  71. ),
  72. _buildControls(context, controller),
  73. ],
  74. ),
  75. );
  76. }
  77. Widget _buildControls(
  78. BuildContext context,
  79. VideoPlayerController controller,
  80. ) {
  81. return widget.showControls
  82. ? Theme.of(context).platform == TargetPlatform.android
  83. ? new MaterialControls(
  84. controller: controller,
  85. onExpandCollapse: widget.onExpandCollapse,
  86. fullScreen: widget.fullScreen,
  87. progressColors: widget.materialProgressColors,
  88. autoPlay: widget.autoPlay,
  89. )
  90. : new CupertinoControls(
  91. backgroundColor: new Color.fromRGBO(41, 41, 41, 0.7),
  92. iconColor: new Color.fromARGB(255, 200, 200, 200),
  93. controller: controller,
  94. onExpandCollapse: widget.onExpandCollapse,
  95. fullScreen: widget.fullScreen,
  96. progressColors: widget.cupertinoProgressColors,
  97. autoPlay: widget.autoPlay,
  98. )
  99. : new Container();
  100. }
  101. @override
  102. void initState() {
  103. // Hack to show the video when it starts playing. Should be fixed by the
  104. // Plugin IMO.
  105. widget.controller.addListener(_onPlay);
  106. super.initState();
  107. }
  108. @override
  109. void didUpdateWidget(PlayerWithControls oldWidget) {
  110. super.didUpdateWidget(oldWidget);
  111. if (widget.controller.dataSource != oldWidget.controller.dataSource) {
  112. widget.controller.addListener(_onPlay);
  113. }
  114. }
  115. @override
  116. dispose(){
  117. widget.controller.removeListener(_onPlay);
  118. super.dispose();
  119. }
  120. void _onPlay() {
  121. if (widget.controller.value.isPlaying) {
  122. setState(() {
  123. widget.controller.removeListener(_onPlay);
  124. });
  125. }
  126. }
  127. }