Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform controls #599

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions lib/src/chewie_player.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:async';

import 'package:chewie/src/chewie_progress_colors.dart';
import 'package:chewie/src/material/models/options_translation.dart';
import 'package:chewie/src/models/option_item.dart';
import 'package:chewie/src/models/options_translation.dart';
import 'package:chewie/src/models/subtitle_model.dart';
import 'package:chewie/src/notifiers/player_notifier.dart';
import 'package:chewie/src/player_with_controls.dart';
Expand All @@ -11,13 +12,11 @@ import 'package:provider/provider.dart';
import 'package:video_player/video_player.dart';
import 'package:wakelock/wakelock.dart';

import 'material/models/option_item.dart';

typedef ChewieRoutePageBuilder = Widget Function(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
_ChewieControllerProvider controllerProvider,
ChewieControllerProvider controllerProvider,
);

/// A Video Player with Material and Cupertino skins.
Expand All @@ -40,7 +39,7 @@ class Chewie extends StatefulWidget {
}

class ChewieState extends State<Chewie> {
bool _isFullScreen = false;
bool _isFullScreen = false;

bool get isControllerFullScreen => widget.controller.isFullScreen;
late PlayerNotifier notifier;
Expand Down Expand Up @@ -84,7 +83,7 @@ class ChewieState extends State<Chewie> {

@override
Widget build(BuildContext context) {
return _ChewieControllerProvider(
return ChewieControllerProvider(
controller: widget.controller,
child: ChangeNotifierProvider<PlayerNotifier>.value(
value: notifier,
Expand All @@ -96,7 +95,7 @@ class ChewieState extends State<Chewie> {
Widget _buildFullScreenVideo(
BuildContext context,
Animation<double> animation,
_ChewieControllerProvider controllerProvider,
ChewieControllerProvider controllerProvider,
) {
return Scaffold(
resizeToAvoidBottomInset: false,
Expand All @@ -112,7 +111,7 @@ class ChewieState extends State<Chewie> {
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
_ChewieControllerProvider controllerProvider,
ChewieControllerProvider controllerProvider,
) {
return AnimatedBuilder(
animation: animation,
Expand All @@ -127,7 +126,7 @@ class ChewieState extends State<Chewie> {
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
final controllerProvider = _ChewieControllerProvider(
final controllerProvider = ChewieControllerProvider(
controller: widget.controller,
child: ChangeNotifierProvider<PlayerNotifier>.value(
value: notifier,
Expand Down Expand Up @@ -184,7 +183,8 @@ class ChewieState extends State<Chewie> {

void onEnterFullScreen() {
final videoWidth = widget.controller.videoPlayerController.value.size.width;
final videoHeight = widget.controller.videoPlayerController.value.size.height;
final videoHeight =
widget.controller.videoPlayerController.value.size.height;

SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

Expand All @@ -201,7 +201,9 @@ class ChewieState extends State<Chewie> {

if (widget.controller.deviceOrientationsOnEnterFullScreen != null) {
/// Optional user preferred settings
SystemChrome.setPreferredOrientations(widget.controller.deviceOrientationsOnEnterFullScreen!);
SystemChrome.setPreferredOrientations(
widget.controller.deviceOrientationsOnEnterFullScreen!,
);
} else {
final isLandscapeVideo = videoWidth > videoHeight;
final isPortraitVideo = videoWidth < videoHeight;
Expand Down Expand Up @@ -316,29 +318,32 @@ class ChewieController extends ChangeNotifier {
List<DeviceOrientation>? deviceOrientationsOnEnterFullScreen,
List<SystemUiOverlay>? systemOverlaysAfterFullScreen,
List<DeviceOrientation>? deviceOrientationsAfterFullScreen,

Widget Function(
Widget Function(
BuildContext,
Animation<double>,
Animation<double>,
_ChewieControllerProvider,
ChewieControllerProvider,
)?
routePageBuilder,
}) {
return ChewieController(
videoPlayerController: videoPlayerController ?? this.videoPlayerController,
videoPlayerController:
videoPlayerController ?? this.videoPlayerController,
optionsTranslation: optionsTranslation ?? this.optionsTranslation,
aspectRatio: aspectRatio ?? this.aspectRatio,
autoInitialize: autoInitialize ?? this.autoInitialize,
autoPlay: autoPlay ?? this.autoPlay,
startAt: startAt ?? this.startAt,
looping: looping ?? this.looping,
fullScreenByDefault: fullScreenByDefault ?? this.fullScreenByDefault,
cupertinoProgressColors: cupertinoProgressColors ?? this.cupertinoProgressColors,
materialProgressColors: materialProgressColors ?? this.materialProgressColors,
cupertinoProgressColors:
cupertinoProgressColors ?? this.cupertinoProgressColors,
materialProgressColors:
materialProgressColors ?? this.materialProgressColors,
placeholder: placeholder ?? this.placeholder,
overlay: overlay ?? this.overlay,
showControlsOnInitialize: showControlsOnInitialize ?? this.showControlsOnInitialize,
showControlsOnInitialize:
showControlsOnInitialize ?? this.showControlsOnInitialize,
showOptions: showOptions ?? this.showOptions,
optionsBuilder: optionsBuilder ?? this.optionsBuilder,
additionalOptions: additionalOptions ?? this.additionalOptions,
Expand All @@ -355,10 +360,15 @@ class ChewieController extends ChangeNotifier {
allowPlaybackSpeedChanging ?? this.allowPlaybackSpeedChanging,
useRootNavigator: useRootNavigator ?? this.useRootNavigator,
playbackSpeeds: playbackSpeeds ?? this.playbackSpeeds,
systemOverlaysOnEnterFullScreen: systemOverlaysOnEnterFullScreen ?? this.systemOverlaysOnEnterFullScreen,
deviceOrientationsOnEnterFullScreen: deviceOrientationsOnEnterFullScreen ?? this.deviceOrientationsOnEnterFullScreen,
systemOverlaysAfterFullScreen: systemOverlaysAfterFullScreen ?? this.systemOverlaysAfterFullScreen,
deviceOrientationsAfterFullScreen: deviceOrientationsAfterFullScreen ?? this.deviceOrientationsAfterFullScreen,
systemOverlaysOnEnterFullScreen: systemOverlaysOnEnterFullScreen ??
this.systemOverlaysOnEnterFullScreen,
deviceOrientationsOnEnterFullScreen:
deviceOrientationsOnEnterFullScreen ??
this.deviceOrientationsOnEnterFullScreen,
systemOverlaysAfterFullScreen:
systemOverlaysAfterFullScreen ?? this.systemOverlaysAfterFullScreen,
deviceOrientationsAfterFullScreen: deviceOrientationsAfterFullScreen ??
this.deviceOrientationsAfterFullScreen,
routePageBuilder: routePageBuilder ?? this.routePageBuilder,
);
}
Expand All @@ -381,13 +391,16 @@ class ChewieController extends ChangeNotifier {
/// the builder method. Just add your own options to the Widget
/// you'll build. If you want to hide the chewieOptions, just leave them
/// out from your Widget.
final Future<void> Function(BuildContext context, List<OptionItem> chewieOptions)? optionsBuilder;
final Future<void> Function(
BuildContext context,
List<OptionItem> chewieOptions,
)? optionsBuilder;

/// Add your own additional options on top of chewie options
final List<OptionItem> Function(BuildContext context)? additionalOptions;

/// Define here your own Widget on how your n'th subtitle will look like
final Widget Function(BuildContext context, dynamic subtitle)? subtitleBuilder;
Widget Function(BuildContext context, dynamic subtitle)? subtitleBuilder;

/// Add a List of Subtitles here in `Subtitles.subtitle`
Subtitles? subtitle;
Expand Down Expand Up @@ -419,7 +432,8 @@ class ChewieController extends ChangeNotifier {

/// When the video playback runs into an error, you can build a custom
/// error message.
final Widget Function(BuildContext context, String errorMessage)? errorBuilder;
final Widget Function(BuildContext context, String errorMessage)?
errorBuilder;

/// The Aspect Ratio of the Video. Important to get the correct size of the
/// video!
Expand Down Expand Up @@ -482,7 +496,8 @@ class ChewieController extends ChangeNotifier {
final ChewieRoutePageBuilder? routePageBuilder;

static ChewieController of(BuildContext context) {
final chewieControllerProvider = context.dependOnInheritedWidgetOfExactType<_ChewieControllerProvider>()!;
final chewieControllerProvider =
context.dependOnInheritedWidgetOfExactType<ChewieControllerProvider>()!;

return chewieControllerProvider.controller;
}
Expand All @@ -496,7 +511,8 @@ class ChewieController extends ChangeNotifier {
Future _initialize() async {
await videoPlayerController.setLooping(looping);

if ((autoInitialize || autoPlay) && !videoPlayerController.value.isInitialized) {
if ((autoInitialize || autoPlay) &&
!videoPlayerController.value.isInitialized) {
await videoPlayerController.initialize();
}

Expand Down Expand Up @@ -569,8 +585,8 @@ class ChewieController extends ChangeNotifier {
}
}

class _ChewieControllerProvider extends InheritedWidget {
const _ChewieControllerProvider({
class ChewieControllerProvider extends InheritedWidget {
const ChewieControllerProvider({
Key? key,
required this.controller,
required Widget child,
Expand All @@ -579,5 +595,6 @@ class _ChewieControllerProvider extends InheritedWidget {
final ChewieController controller;

@override
bool updateShouldNotify(_ChewieControllerProvider old) => controller != old.controller;
bool updateShouldNotify(ChewieControllerProvider old) =>
controller != old.controller;
}
53 changes: 53 additions & 0 deletions lib/src/cupertino/cupertino_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'package:chewie/src/center_play_button.dart';
import 'package:chewie/src/chewie_player.dart';
import 'package:chewie/src/chewie_progress_colors.dart';
import 'package:chewie/src/cupertino/cupertino_progress_bar.dart';
import 'package:chewie/src/cupertino/widgets/cupertino_options_dialog.dart';
import 'package:chewie/src/helpers/utils.dart';
import 'package:chewie/src/models/option_item.dart';
import 'package:chewie/src/models/subtitle_model.dart';
import 'package:chewie/src/notifiers/index.dart';
import 'package:flutter/cupertino.dart';
Expand Down Expand Up @@ -150,6 +152,53 @@ class _CupertinoControlsState extends State<CupertinoControls>
super.didChangeDependencies();
}

GestureDetector _buildOptionsButton(
Color iconColor,
double barHeight,
) {
final options = <OptionItem>[];

if (chewieController.additionalOptions != null &&
chewieController.additionalOptions!(context).isNotEmpty) {
options.addAll(chewieController.additionalOptions!(context));
}

return GestureDetector(
onTap: () async {
_hideTimer?.cancel();

if (chewieController.optionsBuilder != null) {
await chewieController.optionsBuilder!(context, options);
} else {
await showCupertinoModalPopup<OptionItem>(
context: context,
semanticsDismissible: true,
useRootNavigator: chewieController.useRootNavigator,
builder: (context) => CupertinoOptionsDialog(
options: options,
cancelButtonText:
chewieController.optionsTranslation?.cancelButtonText,
),
);
if (_latestValue.isPlaying) {
_startHideTimer();
}
}
},
child: Container(
height: barHeight,
color: Colors.transparent,
padding: const EdgeInsets.only(left: 4.0, right: 8.0),
margin: const EdgeInsets.only(right: 6.0),
child: Icon(
Icons.more_vert,
color: iconColor,
size: 18,
),
),
);
}

Widget _buildSubtitles(Subtitles subtitles) {
if (!_subtitleOn) {
return Container();
Expand Down Expand Up @@ -231,6 +280,10 @@ class _CupertinoControlsState extends State<CupertinoControls>
_buildSubtitleToggle(iconColor, barHeight),
if (chewieController.allowPlaybackSpeedChanging)
_buildSpeedButton(controller, iconColor, barHeight),
if (chewieController.additionalOptions != null &&
chewieController
.additionalOptions!(context).isNotEmpty)
_buildOptionsButton(iconColor, barHeight),
],
),
),
Expand Down
40 changes: 40 additions & 0 deletions lib/src/cupertino/widgets/cupertino_options_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:chewie/src/models/option_item.dart';
import 'package:flutter/cupertino.dart';

class CupertinoOptionsDialog extends StatefulWidget {
const CupertinoOptionsDialog({
Key? key,
required this.options,
this.cancelButtonText,
}) : super(key: key);

final List<OptionItem> options;
final String? cancelButtonText;

@override
// ignore: library_private_types_in_public_api
_CupertinoOptionsDialogState createState() => _CupertinoOptionsDialogState();
}

class _CupertinoOptionsDialogState extends State<CupertinoOptionsDialog> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: CupertinoActionSheet(
actions: widget.options
.map(
(option) => CupertinoActionSheetAction(
onPressed: () => option.onTap!(),
child: Text(option.title),
),
)
.toList(),
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
isDestructiveAction: true,
child: Text(widget.cancelButtonText ?? 'Cancel'),
),
),
);
}
}
Loading