Преглед на файлове

Allow ChewieController.of

And make the provider private
Ben Hagen преди 6 години
родител
ревизия
2bd06b78f3
променени са 7 файла, в които са добавени 184 реда и са изтрити 188 реда
  1. 1 1
      example/lib/main.dart
  2. 0 1
      lib/chewie.dart
  3. 0 175
      lib/src/chewie_controller.dart
  4. 176 3
      lib/src/chewie_player.dart
  5. 3 3
      lib/src/cupertino_controls.dart
  6. 2 2
      lib/src/material_controls.dart
  7. 2 3
      lib/src/player_with_controls.dart

+ 1 - 1
example/lib/main.dart

@@ -1,5 +1,5 @@
 import 'package:chewie/chewie.dart';
-import 'package:chewie/src/chewie_controller.dart';
+import 'package:chewie/src/chewie_player.dart';
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:video_player/video_player.dart';

+ 0 - 1
lib/chewie.dart

@@ -1,5 +1,4 @@
 library chewie;
 
 export 'src/chewie_player.dart';
-export 'src/chewie_controller.dart';
 export 'src/chewie_progress_colors.dart';

+ 0 - 175
lib/src/chewie_controller.dart

@@ -1,175 +0,0 @@
-import 'package:chewie/src/chewie_progress_colors.dart';
-import 'package:flutter/material.dart';
-import 'package:video_player/video_player.dart';
-
-/// The ChewieController is used to configure and drive the Chewie Player
-/// Widgets. It provides methods to control playback, such as [pause] and
-/// [play], as well as methods that control the visual appearance of the player,
-/// such as [enterFullScreen] or [exitFullScreen].
-///
-/// In addition, you can listen to the ChewieController for presentational
-/// changes, such as entering and exiting full screen mode. To listen for
-/// changes to the playback, such as a change to the seek position of the
-/// player, please use the standard information provided by the
-/// `VideoPlayerController`.
-class ChewieController extends ChangeNotifier {
-  ChewieController({
-    this.videoPlayerController,
-    this.aspectRatio,
-    this.autoInitialize = false,
-    this.autoPlay = false,
-    this.startAt,
-    this.looping = false,
-    this.fullScreenByDefault = false,
-    this.cupertinoProgressColors,
-    this.materialProgressColors,
-    this.placeholder,
-    this.showControls = true,
-    this.customControls,
-    this.allowedScreenSleep = true,
-    this.isLive = false,
-  }) : assert(videoPlayerController != null,
-            'You must provide a controller to play a video') {
-    _initialize();
-  }
-
-  /// The controller for the video you want to play
-  final VideoPlayerController videoPlayerController;
-
-  /// Initialize the Video on Startup. This will prep the video for playback.
-  final bool autoInitialize;
-
-  /// Play the video as soon as it's displayed
-  final bool autoPlay;
-
-  /// Start video at a certain position
-  final Duration startAt;
-
-  /// Whether or not the video should loop
-  final bool looping;
-
-  /// Whether or not to show the controls
-  final bool showControls;
-
-  /// Defines customised controls. Check [MaterialControls] or
-  /// [CupertinoControls] for reference.
-  final Widget customControls;
-
-  /// The Aspect Ratio of the Video. Important to get the correct size of the
-  /// video!
-  ///
-  /// Will fallback to fitting within the space allowed.
-  final double aspectRatio;
-
-  /// The colors to use for controls on iOS. By default, the iOS player uses
-  /// colors sampled from the original iOS 11 designs.
-  final ChewieProgressColors cupertinoProgressColors;
-
-  /// The colors to use for the Material Progress Bar. By default, the Material
-  /// player uses the colors from your Theme.
-  final ChewieProgressColors materialProgressColors;
-
-  /// The placeholder is displayed underneath the Video before it is initialized
-  /// or played.
-  final Widget placeholder;
-
-  /// Defines if the player will start in fullscreen when play is pressed
-  final bool fullScreenByDefault;
-
-  /// Defines if the player will sleep in fullscreen or not
-  final bool allowedScreenSleep;
-
-  /// Defines if the controls should be for live stream video
-  final bool isLive;
-
-  bool _isFullScreen = false;
-
-  bool get isFullScreen => _isFullScreen;
-
-  Future _initialize() async {
-    await videoPlayerController.setLooping(looping);
-
-    if ((autoInitialize || autoPlay) &&
-        !videoPlayerController.value.initialized) {
-      await videoPlayerController.initialize();
-    }
-
-    if (autoPlay) {
-      if (fullScreenByDefault) {
-        enterFullscreen();
-      }
-
-      await videoPlayerController.play();
-    }
-
-    if (startAt != null) {
-      await videoPlayerController.seekTo(startAt);
-    }
-
-    if (fullScreenByDefault) {
-      videoPlayerController.addListener(() async {
-        if (await videoPlayerController.value.isPlaying && !_isFullScreen) {
-          enterFullscreen();
-        }
-      });
-    }
-  }
-
-  void enterFullscreen() {
-    _isFullScreen = true;
-    notifyListeners();
-  }
-
-  void exitFullscreen() {
-    _isFullScreen = false;
-    notifyListeners();
-  }
-
-  void toggleFullscreen() {
-    _isFullScreen = !_isFullScreen;
-    notifyListeners();
-  }
-
-  Future<void> play() async {
-    await videoPlayerController.play();
-  }
-
-  Future<void> setLooping(bool looping) async {
-    await videoPlayerController.setLooping(looping);
-  }
-
-  Future<void> pause() async {
-    await videoPlayerController.pause();
-  }
-
-  Future<void> seekTo(Duration moment) async {
-    await videoPlayerController.seekTo(moment);
-  }
-
-  Future<void> setVolume(double volume) async {
-    await videoPlayerController.setVolume(volume);
-  }
-}
-
-class ChewieControllerProvider extends InheritedWidget {
-  const ChewieControllerProvider({
-    Key key,
-    @required this.controller,
-    @required Widget child,
-  })  : assert(controller != null),
-        assert(child != null),
-        super(key: key, child: child);
-
-  final ChewieController controller;
-
-  static ChewieController of(BuildContext context) {
-    final ChewieControllerProvider chewieControllerProvider =
-        context.inheritFromWidgetOfExactType(ChewieControllerProvider);
-
-    return chewieControllerProvider.controller;
-  }
-
-  @override
-  bool updateShouldNotify(ChewieControllerProvider old) =>
-      controller != old.controller;
-}

+ 176 - 3
lib/src/chewie_player.dart

@@ -1,11 +1,12 @@
 import 'dart:async';
 
-import 'package:chewie/src/chewie_controller.dart';
+import 'package:chewie/src/chewie_progress_colors.dart';
 import 'package:chewie/src/player_with_controls.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter/widgets.dart';
 import 'package:screen/screen.dart';
+import 'package:video_player/video_player.dart';
 
 /// A Video Player with Material and Cupertino skins.
 ///
@@ -62,7 +63,7 @@ class ChewieState extends State<Chewie> {
 
   @override
   Widget build(BuildContext context) {
-    return ChewieControllerProvider(
+    return _ChewieControllerProvider(
       controller: widget.controller,
       child: PlayerWithControls(),
     );
@@ -75,7 +76,7 @@ class ChewieState extends State<Chewie> {
       body: Container(
         alignment: Alignment.center,
         color: Colors.black,
-        child: ChewieControllerProvider(
+        child: _ChewieControllerProvider(
           controller: widget.controller,
           child: PlayerWithControls(),
         ),
@@ -131,3 +132,175 @@ class ChewieState extends State<Chewie> {
     ]);
   }
 }
+
+/// The ChewieController is used to configure and drive the Chewie Player
+/// Widgets. It provides methods to control playback, such as [pause] and
+/// [play], as well as methods that control the visual appearance of the player,
+/// such as [enterFullScreen] or [exitFullScreen].
+///
+/// In addition, you can listen to the ChewieController for presentational
+/// changes, such as entering and exiting full screen mode. To listen for
+/// changes to the playback, such as a change to the seek position of the
+/// player, please use the standard information provided by the
+/// `VideoPlayerController`.
+class ChewieController extends ChangeNotifier {
+  ChewieController({
+    this.videoPlayerController,
+    this.aspectRatio,
+    this.autoInitialize = false,
+    this.autoPlay = false,
+    this.startAt,
+    this.looping = false,
+    this.fullScreenByDefault = false,
+    this.cupertinoProgressColors,
+    this.materialProgressColors,
+    this.placeholder,
+    this.showControls = true,
+    this.customControls,
+    this.allowedScreenSleep = true,
+    this.isLive = false,
+  }) : assert(videoPlayerController != null,
+            'You must provide a controller to play a video') {
+    _initialize();
+  }
+
+  /// The controller for the video you want to play
+  final VideoPlayerController videoPlayerController;
+
+  /// Initialize the Video on Startup. This will prep the video for playback.
+  final bool autoInitialize;
+
+  /// Play the video as soon as it's displayed
+  final bool autoPlay;
+
+  /// Start video at a certain position
+  final Duration startAt;
+
+  /// Whether or not the video should loop
+  final bool looping;
+
+  /// Whether or not to show the controls
+  final bool showControls;
+
+  /// Defines customised controls. Check [MaterialControls] or
+  /// [CupertinoControls] for reference.
+  final Widget customControls;
+
+  /// The Aspect Ratio of the Video. Important to get the correct size of the
+  /// video!
+  ///
+  /// Will fallback to fitting within the space allowed.
+  final double aspectRatio;
+
+  /// The colors to use for controls on iOS. By default, the iOS player uses
+  /// colors sampled from the original iOS 11 designs.
+  final ChewieProgressColors cupertinoProgressColors;
+
+  /// The colors to use for the Material Progress Bar. By default, the Material
+  /// player uses the colors from your Theme.
+  final ChewieProgressColors materialProgressColors;
+
+  /// The placeholder is displayed underneath the Video before it is initialized
+  /// or played.
+  final Widget placeholder;
+
+  /// Defines if the player will start in fullscreen when play is pressed
+  final bool fullScreenByDefault;
+
+  /// Defines if the player will sleep in fullscreen or not
+  final bool allowedScreenSleep;
+
+  /// Defines if the controls should be for live stream video
+  final bool isLive;
+
+  static ChewieController of(BuildContext context) {
+    final _ChewieControllerProvider chewieControllerProvider =
+        context.inheritFromWidgetOfExactType(_ChewieControllerProvider);
+
+    return chewieControllerProvider.controller;
+  }
+
+  bool _isFullScreen = false;
+
+  bool get isFullScreen => _isFullScreen;
+
+  Future _initialize() async {
+    await videoPlayerController.setLooping(looping);
+
+    if ((autoInitialize || autoPlay) &&
+        !videoPlayerController.value.initialized) {
+      await videoPlayerController.initialize();
+    }
+
+    if (autoPlay) {
+      if (fullScreenByDefault) {
+        enterFullscreen();
+      }
+
+      await videoPlayerController.play();
+    }
+
+    if (startAt != null) {
+      await videoPlayerController.seekTo(startAt);
+    }
+
+    if (fullScreenByDefault) {
+      videoPlayerController.addListener(() async {
+        if (await videoPlayerController.value.isPlaying && !_isFullScreen) {
+          enterFullscreen();
+        }
+      });
+    }
+  }
+
+  void enterFullscreen() {
+    _isFullScreen = true;
+    notifyListeners();
+  }
+
+  void exitFullscreen() {
+    _isFullScreen = false;
+    notifyListeners();
+  }
+
+  void toggleFullscreen() {
+    _isFullScreen = !_isFullScreen;
+    notifyListeners();
+  }
+
+  Future<void> play() async {
+    await videoPlayerController.play();
+  }
+
+  Future<void> setLooping(bool looping) async {
+    await videoPlayerController.setLooping(looping);
+  }
+
+  Future<void> pause() async {
+    await videoPlayerController.pause();
+  }
+
+  Future<void> seekTo(Duration moment) async {
+    await videoPlayerController.seekTo(moment);
+  }
+
+  Future<void> setVolume(double volume) async {
+    await videoPlayerController.setVolume(volume);
+  }
+}
+
+class _ChewieControllerProvider extends InheritedWidget {
+  const _ChewieControllerProvider({
+    Key key,
+    @required this.controller,
+    @required Widget child,
+  })  : assert(controller != null),
+        assert(child != null),
+        super(key: key, child: child);
+
+  final ChewieController controller;
+
+  @override
+  bool updateShouldNotify(_ChewieControllerProvider old) =>
+      controller != old.controller;
+}

+ 3 - 3
lib/src/cupertino_controls.dart

@@ -2,7 +2,7 @@ import 'dart:async';
 import 'dart:math' as math;
 import 'dart:ui' as ui;
 
-import 'package:chewie/src/chewie_controller.dart';
+import 'package:chewie/src/chewie_player.dart';
 import 'package:chewie/src/chewie_progress_colors.dart';
 import 'package:chewie/src/cupertino_progress_bar.dart';
 import 'package:chewie/src/utils.dart';
@@ -42,7 +42,7 @@ class _CupertinoControlsState extends State<CupertinoControls> {
   Widget build(BuildContext context) {
     final backgroundColor = widget.backgroundColor;
     final iconColor = widget.iconColor;
-    chewieController = ChewieControllerProvider.of(context);
+    chewieController = ChewieController.of(context);
     controller = chewieController.videoPlayerController;
     final orientation = MediaQuery.of(context).orientation;
     final barHeight = orientation == Orientation.portrait ? 30.0 : 47.0;
@@ -72,7 +72,7 @@ class _CupertinoControlsState extends State<CupertinoControls> {
 
   @override
   void didChangeDependencies() {
-    chewieController = ChewieControllerProvider.of(context);
+    chewieController = ChewieController.of(context);
     controller = chewieController.videoPlayerController;
 
     _dispose();

+ 2 - 2
lib/src/material_controls.dart

@@ -1,6 +1,6 @@
 import 'dart:async';
 
-import 'package:chewie/src/chewie_controller.dart';
+import 'package:chewie/src/chewie_player.dart';
 import 'package:chewie/src/chewie_progress_colors.dart';
 import 'package:chewie/src/material_progress_bar.dart';
 import 'package:chewie/src/utils.dart';
@@ -65,7 +65,7 @@ class _MaterialControlsState extends State<MaterialControls> {
 
   @override
   void didChangeDependencies() {
-    chewieController = ChewieControllerProvider.of(context);
+    chewieController = ChewieController.of(context);
     controller = chewieController.videoPlayerController;
 
     _dispose();

+ 2 - 3
lib/src/player_with_controls.dart

@@ -1,6 +1,6 @@
 import 'dart:ui';
 
-import 'package:chewie/src/chewie_controller.dart';
+import 'package:chewie/src/chewie_player.dart';
 import 'package:chewie/src/cupertino_controls.dart';
 import 'package:chewie/src/material_controls.dart';
 import 'package:flutter/foundation.dart';
@@ -12,8 +12,7 @@ class PlayerWithControls extends StatelessWidget {
 
   @override
   Widget build(BuildContext context) {
-    final ChewieController chewieController =
-        ChewieControllerProvider.of(context);
+    final ChewieController chewieController = ChewieController.of(context);
 
     return Center(
       child: Container(