chewie_controller.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:chewie/src/chewie_progress_colors.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:video_player/video_player.dart';
  4. /// The state of the [ChewieController].
  5. @immutable
  6. class ChewieValue {
  7. ChewieValue({
  8. this.isFullScreen = false,
  9. });
  10. /// True if the video is currently playing fullscreen
  11. final bool isFullScreen;
  12. ChewieValue copyWith({
  13. VideoPlayerController videoPlayerController,
  14. bool isFullScreen,
  15. }) {
  16. return ChewieValue(
  17. isFullScreen: isFullScreen ?? this.isFullScreen,
  18. );
  19. }
  20. @override
  21. String toString() {
  22. return '$runtimeType('
  23. 'isFullscreen: $isFullScreen, ';
  24. }
  25. }
  26. class ChewieController extends ValueNotifier<ChewieValue> {
  27. ChewieController({
  28. this.videoPlayerController,
  29. this.aspectRatio,
  30. this.autoInitialize = false,
  31. this.autoPlay = false,
  32. this.startAt,
  33. this.looping = false,
  34. this.fullScreenByDefault = false,
  35. this.cupertinoProgressColors,
  36. this.materialProgressColors,
  37. this.placeholder,
  38. this.showControls = true,
  39. this.allowedScreenSleep = true,
  40. this.isLive = false,
  41. }) : assert(videoPlayerController != null,
  42. 'You must provide a controller to play a video'),
  43. super(ChewieValue()) {
  44. _initialize();
  45. }
  46. /// The controller for the video you want to play
  47. final VideoPlayerController videoPlayerController;
  48. /// Initialize the Video on Startup. This will prep the video for playback.
  49. final bool autoInitialize;
  50. /// Play the video as soon as it's displayed
  51. final bool autoPlay;
  52. /// Start video at a certain position
  53. final Duration startAt;
  54. /// Whether or not the video should loop
  55. final bool looping;
  56. /// Whether or not to show the controls
  57. final bool showControls;
  58. /// The Aspect Ratio of the Video. Important to get the correct size of the
  59. /// video!
  60. ///
  61. /// Will fallback to fitting within the space allowed.
  62. final double aspectRatio;
  63. /// The colors to use for controls on iOS. By default, the iOS player uses
  64. /// colors sampled from the original iOS 11 designs.
  65. final ChewieProgressColors cupertinoProgressColors;
  66. /// The colors to use for the Material Progress Bar. By default, the Material
  67. /// player uses the colors from your Theme.
  68. final ChewieProgressColors materialProgressColors;
  69. /// The placeholder is displayed underneath the Video before it is initialized
  70. /// or played.
  71. final Widget placeholder;
  72. /// Defines if the player will start in fullscreen when play is pressed
  73. final bool fullScreenByDefault;
  74. /// Defines if the player will sleep in fullscreen or not
  75. final bool allowedScreenSleep;
  76. /// Defines if the controls should be for live stream video
  77. final bool isLive;
  78. bool get isFullScreen => value.isFullScreen;
  79. Future _initialize() async {
  80. await videoPlayerController.setLooping(looping);
  81. if (autoInitialize || autoPlay) {
  82. await videoPlayerController.initialize();
  83. }
  84. if (autoPlay) {
  85. if (fullScreenByDefault) {
  86. enterFullscreen();
  87. }
  88. await videoPlayerController.play();
  89. }
  90. if (startAt != null) {
  91. await videoPlayerController.seekTo(startAt);
  92. }
  93. if (fullScreenByDefault) {
  94. videoPlayerController.addListener(() async {
  95. if (await videoPlayerController.value.isPlaying &&
  96. !value.isFullScreen) {
  97. enterFullscreen();
  98. }
  99. });
  100. }
  101. }
  102. void enterFullscreen() {
  103. value = value.copyWith(isFullScreen: true);
  104. }
  105. void exitFullscreen() {
  106. value = value.copyWith(isFullScreen: false);
  107. }
  108. void toggleFullscreen() {
  109. value = value.copyWith(isFullScreen: !value.isFullScreen);
  110. }
  111. void play() {
  112. videoPlayerController.play();
  113. }
  114. void pause() {
  115. videoPlayerController.pause();
  116. }
  117. }
  118. class ChewieControllerProvider extends InheritedWidget {
  119. const ChewieControllerProvider({
  120. Key key,
  121. @required this.controller,
  122. @required Widget child,
  123. }) : assert(controller != null),
  124. assert(child != null),
  125. super(key: key, child: child);
  126. final ChewieController controller;
  127. static ChewieController of(BuildContext context) {
  128. final ChewieControllerProvider chewieControllerProvider =
  129. context.inheritFromWidgetOfExactType(ChewieControllerProvider);
  130. return chewieControllerProvider.controller;
  131. }
  132. @override
  133. bool updateShouldNotify(ChewieControllerProvider old) =>
  134. controller != old.controller;
  135. }