Skip to content

Commit

Permalink
Merge pull request #703 from GyanendroKh/master
Browse files Browse the repository at this point in the history
Adding Seek buttons for Android
  • Loading branch information
diegotori authored Sep 10, 2024
2 parents 7ba611a + 38d0209 commit 34d7b5f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 7 deletions.
51 changes: 51 additions & 0 deletions lib/src/center_seek_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter/material.dart';

class CenterSeekButton extends StatelessWidget {
const CenterSeekButton({
Key? key,
required this.iconData,
required this.backgroundColor,
this.iconColor,
required this.show,
this.fadeDuration = const Duration(milliseconds: 300),
this.iconSize = 26,
this.onPressed,
}) : super(key: key);

final IconData iconData;
final Color backgroundColor;
final Color? iconColor;
final bool show;
final VoidCallback? onPressed;
final Duration fadeDuration;
final double iconSize;

@override
Widget build(BuildContext context) {
return ColoredBox(
color: Colors.transparent,
child: Center(
child: UnconstrainedBox(
child: AnimatedOpacity(
opacity: show ? 1.0 : 0.0,
duration: fadeDuration,
child: DecoratedBox(
decoration: BoxDecoration(
color: backgroundColor,
shape: BoxShape.circle,
),
// Always set the iconSize on the IconButton, not on the Icon itself:
// https://github.com/flutter/flutter/issues/52980
child: IconButton(
iconSize: iconSize,
padding: const EdgeInsets.all(8.0),
icon: Icon(iconData, color: iconColor),
onPressed: onPressed,
),
),
),
),
),
);
}
}
14 changes: 14 additions & 0 deletions lib/src/chewie_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ class ChewieController extends ChangeNotifier {
this.fullScreenByDefault = false,
this.cupertinoProgressColors,
this.materialProgressColors,
this.materialSeekButtonFadeDuration = const Duration(milliseconds: 300),
this.materialSeekButtonSize = 26,
this.placeholder,
this.overlay,
this.showControlsOnInitialize = true,
Expand Down Expand Up @@ -324,6 +326,8 @@ class ChewieController extends ChangeNotifier {
bool? fullScreenByDefault,
ChewieProgressColors? cupertinoProgressColors,
ChewieProgressColors? materialProgressColors,
Duration? materialSeekButtonFadeDuration,
double? materialSeekButtonSize,
Widget? placeholder,
Widget? overlay,
bool? showControlsOnInitialize,
Expand Down Expand Up @@ -375,6 +379,10 @@ class ChewieController extends ChangeNotifier {
cupertinoProgressColors ?? this.cupertinoProgressColors,
materialProgressColors:
materialProgressColors ?? this.materialProgressColors,
materialSeekButtonFadeDuration:
materialSeekButtonFadeDuration ?? this.materialSeekButtonFadeDuration,
materialSeekButtonSize:
materialSeekButtonSize ?? this.materialSeekButtonSize,
placeholder: placeholder ?? this.placeholder,
overlay: overlay ?? this.overlay,
showControlsOnInitialize:
Expand Down Expand Up @@ -505,6 +513,12 @@ class ChewieController extends ChangeNotifier {
/// player uses the colors from your Theme.
final ChewieProgressColors? materialProgressColors;

// The duration of the fade animation for the seek button (Material Player only)
final Duration materialSeekButtonFadeDuration;

// The size of the seek button for the Material Player only
final double materialSeekButtonSize;

/// The placeholder is displayed underneath the Video before it is initialized
/// or played.
final Widget? placeholder;
Expand Down
80 changes: 73 additions & 7 deletions lib/src/material/material_controls.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:chewie/src/center_play_button.dart';
import 'package:chewie/src/center_seek_button.dart';
import 'package:chewie/src/chewie_player.dart';
import 'package:chewie/src/chewie_progress_colors.dart';
import 'package:chewie/src/helpers/utils.dart';
Expand Down Expand Up @@ -388,13 +389,48 @@ class _MaterialControlsState extends State<MaterialControls>
});
}
},
child: CenterPlayButton(
backgroundColor: Colors.black54,
iconColor: Colors.white,
isFinished: isFinished,
isPlaying: controller.value.isPlaying,
show: showPlayButton,
onPressed: _playPause,
child: Container(
alignment: Alignment.center,
color: Colors
.transparent, // The Gesture Detector doesn't expand to the full size of the container without this; Not sure why!
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (!isFinished && !chewieController.isLive)
CenterSeekButton(
iconData: Icons.replay_10,
backgroundColor: Colors.black54,
iconColor: Colors.white,
show: showPlayButton,
fadeDuration: chewieController.materialSeekButtonFadeDuration,
iconSize: chewieController.materialSeekButtonSize,
onPressed: _seekBackward,
),
Container(
margin: EdgeInsets.symmetric(
horizontal: marginSize,
),
child: CenterPlayButton(
backgroundColor: Colors.black54,
iconColor: Colors.white,
isFinished: isFinished,
isPlaying: controller.value.isPlaying,
show: showPlayButton,
onPressed: _playPause,
),
),
if (!isFinished && !chewieController.isLive)
CenterSeekButton(
iconData: Icons.forward_10,
backgroundColor: Colors.black54,
iconColor: Colors.white,
show: showPlayButton,
fadeDuration: chewieController.materialSeekButtonFadeDuration,
iconSize: chewieController.materialSeekButtonSize,
onPressed: _seekForward,
),
],
),
),
);
}
Expand Down Expand Up @@ -546,6 +582,36 @@ class _MaterialControlsState extends State<MaterialControls>
});
}

void _seekRelative(Duration relativeSeek) {
_cancelAndRestartTimer();
final position = _latestValue.position + relativeSeek;
final duration = _latestValue.duration;

if (position < Duration.zero) {
controller.seekTo(Duration.zero);
} else if (position > duration) {
controller.seekTo(duration);
} else {
controller.seekTo(position);
}
}

void _seekBackward() {
_seekRelative(
const Duration(
seconds: -10,
),
);
}

void _seekForward() {
_seekRelative(
const Duration(
seconds: 10,
),
);
}

void _startHideTimer() {
final hideControlsTimer = chewieController.hideControlsTimer.isNegative
? ChewieController.defaultHideControlsTimer
Expand Down

0 comments on commit 34d7b5f

Please sign in to comment.