11import 'dart:async' ;
22
3- import 'package:chewie/src/chewie_controller .dart' ;
3+ import 'package:chewie/src/chewie_progress_colors .dart' ;
44import 'package:chewie/src/player_with_controls.dart' ;
55import 'package:flutter/material.dart' ;
66import 'package:flutter/services.dart' ;
77import 'package:flutter/widgets.dart' ;
88import 'package:screen/screen.dart' ;
9+ import 'package:video_player/video_player.dart' ;
910
1011/// A Video Player with Material and Cupertino skins.
1112///
@@ -62,7 +63,7 @@ class ChewieState extends State<Chewie> {
6263
6364 @override
6465 Widget build (BuildContext context) {
65- return ChewieControllerProvider (
66+ return _ChewieControllerProvider (
6667 controller: widget.controller,
6768 child: PlayerWithControls (),
6869 );
@@ -75,7 +76,7 @@ class ChewieState extends State<Chewie> {
7576 body: Container (
7677 alignment: Alignment .center,
7778 color: Colors .black,
78- child: ChewieControllerProvider (
79+ child: _ChewieControllerProvider (
7980 controller: widget.controller,
8081 child: PlayerWithControls (),
8182 ),
@@ -131,3 +132,177 @@ class ChewieState extends State<Chewie> {
131132 ]);
132133 }
133134}
135+
136+ /// The ChewieController is used to configure and drive the Chewie Player
137+ /// Widgets. It provides methods to control playback, such as [pause] and
138+ /// [play] , as well as methods that control the visual appearance of the player,
139+ /// such as [enterFullScreen] or [exitFullScreen] .
140+ ///
141+ /// In addition, you can listen to the ChewieController for presentational
142+ /// changes, such as entering and exiting full screen mode. To listen for
143+ /// changes to the playback, such as a change to the seek position of the
144+ /// player, please use the standard information provided by the
145+ /// `VideoPlayerController` .
146+ class ChewieController extends ChangeNotifier {
147+ ChewieController ({
148+ this .videoPlayerController,
149+ this .aspectRatio,
150+ this .autoInitialize = false ,
151+ this .autoPlay = false ,
152+ this .startAt,
153+ this .looping = false ,
154+ this .fullScreenByDefault = false ,
155+ this .cupertinoProgressColors,
156+ this .materialProgressColors,
157+ this .placeholder,
158+ this .showControls = true ,
159+ this .customControls,
160+ this .allowedScreenSleep = true ,
161+ this .isLive = false ,
162+ }) : assert (videoPlayerController != null ,
163+ 'You must provide a controller to play a video' ) {
164+ _initialize ();
165+ }
166+
167+ /// The controller for the video you want to play
168+ final VideoPlayerController videoPlayerController;
169+
170+ /// Initialize the Video on Startup. This will prep the video for playback.
171+ final bool autoInitialize;
172+
173+ /// Play the video as soon as it's displayed
174+ final bool autoPlay;
175+
176+ /// Start video at a certain position
177+ final Duration startAt;
178+
179+ /// Whether or not the video should loop
180+ final bool looping;
181+
182+ /// Whether or not to show the controls
183+ final bool showControls;
184+
185+ /// Defines customised controls. Check [MaterialControls] or
186+ /// [CupertinoControls] for reference.
187+ final Widget customControls;
188+
189+ /// The Aspect Ratio of the Video. Important to get the correct size of the
190+ /// video!
191+ ///
192+ /// Will fallback to fitting within the space allowed.
193+ final double aspectRatio;
194+
195+ /// The colors to use for controls on iOS. By default, the iOS player uses
196+ /// colors sampled from the original iOS 11 designs.
197+ final ChewieProgressColors cupertinoProgressColors;
198+
199+ /// The colors to use for the Material Progress Bar. By default, the Material
200+ /// player uses the colors from your Theme.
201+ final ChewieProgressColors materialProgressColors;
202+
203+ /// The placeholder is displayed underneath the Video before it is initialized
204+ /// or played.
205+ final Widget placeholder;
206+
207+ /// Defines if the player will start in fullscreen when play is pressed
208+ final bool fullScreenByDefault;
209+
210+ /// Defines if the player will sleep in fullscreen or not
211+ final bool allowedScreenSleep;
212+
213+ /// Defines if the controls should be for live stream video
214+ final bool isLive;
215+
216+ static ChewieController of (BuildContext context) {
217+ final _ChewieControllerProvider chewieControllerProvider =
218+ context.inheritFromWidgetOfExactType (_ChewieControllerProvider );
219+
220+ Theme .of (context);
221+
222+ return chewieControllerProvider.controller;
223+ }
224+
225+ bool _isFullScreen = false ;
226+
227+ bool get isFullScreen => _isFullScreen;
228+
229+ Future _initialize () async {
230+ await videoPlayerController.setLooping (looping);
231+
232+ if ((autoInitialize || autoPlay) &&
233+ ! videoPlayerController.value.initialized) {
234+ await videoPlayerController.initialize ();
235+ }
236+
237+ if (autoPlay) {
238+ if (fullScreenByDefault) {
239+ enterFullscreen ();
240+ }
241+
242+ await videoPlayerController.play ();
243+ }
244+
245+ if (startAt != null ) {
246+ await videoPlayerController.seekTo (startAt);
247+ }
248+
249+ if (fullScreenByDefault) {
250+ videoPlayerController.addListener (() async {
251+ if (await videoPlayerController.value.isPlaying && ! _isFullScreen) {
252+ enterFullscreen ();
253+ }
254+ });
255+ }
256+ }
257+
258+ void enterFullscreen () {
259+ _isFullScreen = true ;
260+ notifyListeners ();
261+ }
262+
263+ void exitFullscreen () {
264+ _isFullScreen = false ;
265+ notifyListeners ();
266+ }
267+
268+ void toggleFullscreen () {
269+ _isFullScreen = ! _isFullScreen;
270+ notifyListeners ();
271+ }
272+
273+ Future <void > play () async {
274+ await videoPlayerController.play ();
275+ }
276+
277+ Future <void > setLooping (bool looping) async {
278+ await videoPlayerController.setLooping (looping);
279+ }
280+
281+ Future <void > pause () async {
282+ await videoPlayerController.pause ();
283+ }
284+
285+ Future <void > seekTo (Duration moment) async {
286+ await videoPlayerController.seekTo (moment);
287+ }
288+
289+ Future <void > setVolume (double volume) async {
290+ await videoPlayerController.setVolume (volume);
291+ }
292+ }
293+
294+ class _ChewieControllerProvider extends InheritedWidget {
295+ const _ChewieControllerProvider ({
296+ Key key,
297+ @required this .controller,
298+ @required Widget child,
299+ }) : assert (controller != null ),
300+ assert (child != null ),
301+ super (key: key, child: child);
302+
303+ final ChewieController controller;
304+
305+ @override
306+ bool updateShouldNotify (_ChewieControllerProvider old) =>
307+ controller != old.controller;
308+ }
0 commit comments