Skip to content

Commit 34d7b5f

Browse files
authored
Merge pull request #703 from GyanendroKh/master
Adding Seek buttons for Android
2 parents 7ba611a + 38d0209 commit 34d7b5f

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

lib/src/center_seek_button.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter/material.dart';
2+
3+
class CenterSeekButton extends StatelessWidget {
4+
const CenterSeekButton({
5+
Key? key,
6+
required this.iconData,
7+
required this.backgroundColor,
8+
this.iconColor,
9+
required this.show,
10+
this.fadeDuration = const Duration(milliseconds: 300),
11+
this.iconSize = 26,
12+
this.onPressed,
13+
}) : super(key: key);
14+
15+
final IconData iconData;
16+
final Color backgroundColor;
17+
final Color? iconColor;
18+
final bool show;
19+
final VoidCallback? onPressed;
20+
final Duration fadeDuration;
21+
final double iconSize;
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return ColoredBox(
26+
color: Colors.transparent,
27+
child: Center(
28+
child: UnconstrainedBox(
29+
child: AnimatedOpacity(
30+
opacity: show ? 1.0 : 0.0,
31+
duration: fadeDuration,
32+
child: DecoratedBox(
33+
decoration: BoxDecoration(
34+
color: backgroundColor,
35+
shape: BoxShape.circle,
36+
),
37+
// Always set the iconSize on the IconButton, not on the Icon itself:
38+
// https://github.com/flutter/flutter/issues/52980
39+
child: IconButton(
40+
iconSize: iconSize,
41+
padding: const EdgeInsets.all(8.0),
42+
icon: Icon(iconData, color: iconColor),
43+
onPressed: onPressed,
44+
),
45+
),
46+
),
47+
),
48+
),
49+
);
50+
}
51+
}

lib/src/chewie_player.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ class ChewieController extends ChangeNotifier {
275275
this.fullScreenByDefault = false,
276276
this.cupertinoProgressColors,
277277
this.materialProgressColors,
278+
this.materialSeekButtonFadeDuration = const Duration(milliseconds: 300),
279+
this.materialSeekButtonSize = 26,
278280
this.placeholder,
279281
this.overlay,
280282
this.showControlsOnInitialize = true,
@@ -324,6 +326,8 @@ class ChewieController extends ChangeNotifier {
324326
bool? fullScreenByDefault,
325327
ChewieProgressColors? cupertinoProgressColors,
326328
ChewieProgressColors? materialProgressColors,
329+
Duration? materialSeekButtonFadeDuration,
330+
double? materialSeekButtonSize,
327331
Widget? placeholder,
328332
Widget? overlay,
329333
bool? showControlsOnInitialize,
@@ -375,6 +379,10 @@ class ChewieController extends ChangeNotifier {
375379
cupertinoProgressColors ?? this.cupertinoProgressColors,
376380
materialProgressColors:
377381
materialProgressColors ?? this.materialProgressColors,
382+
materialSeekButtonFadeDuration:
383+
materialSeekButtonFadeDuration ?? this.materialSeekButtonFadeDuration,
384+
materialSeekButtonSize:
385+
materialSeekButtonSize ?? this.materialSeekButtonSize,
378386
placeholder: placeholder ?? this.placeholder,
379387
overlay: overlay ?? this.overlay,
380388
showControlsOnInitialize:
@@ -505,6 +513,12 @@ class ChewieController extends ChangeNotifier {
505513
/// player uses the colors from your Theme.
506514
final ChewieProgressColors? materialProgressColors;
507515

516+
// The duration of the fade animation for the seek button (Material Player only)
517+
final Duration materialSeekButtonFadeDuration;
518+
519+
// The size of the seek button for the Material Player only
520+
final double materialSeekButtonSize;
521+
508522
/// The placeholder is displayed underneath the Video before it is initialized
509523
/// or played.
510524
final Widget? placeholder;

lib/src/material/material_controls.dart

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22

33
import 'package:chewie/src/center_play_button.dart';
4+
import 'package:chewie/src/center_seek_button.dart';
45
import 'package:chewie/src/chewie_player.dart';
56
import 'package:chewie/src/chewie_progress_colors.dart';
67
import 'package:chewie/src/helpers/utils.dart';
@@ -388,13 +389,48 @@ class _MaterialControlsState extends State<MaterialControls>
388389
});
389390
}
390391
},
391-
child: CenterPlayButton(
392-
backgroundColor: Colors.black54,
393-
iconColor: Colors.white,
394-
isFinished: isFinished,
395-
isPlaying: controller.value.isPlaying,
396-
show: showPlayButton,
397-
onPressed: _playPause,
392+
child: Container(
393+
alignment: Alignment.center,
394+
color: Colors
395+
.transparent, // The Gesture Detector doesn't expand to the full size of the container without this; Not sure why!
396+
child: Row(
397+
mainAxisAlignment: MainAxisAlignment.center,
398+
children: [
399+
if (!isFinished && !chewieController.isLive)
400+
CenterSeekButton(
401+
iconData: Icons.replay_10,
402+
backgroundColor: Colors.black54,
403+
iconColor: Colors.white,
404+
show: showPlayButton,
405+
fadeDuration: chewieController.materialSeekButtonFadeDuration,
406+
iconSize: chewieController.materialSeekButtonSize,
407+
onPressed: _seekBackward,
408+
),
409+
Container(
410+
margin: EdgeInsets.symmetric(
411+
horizontal: marginSize,
412+
),
413+
child: CenterPlayButton(
414+
backgroundColor: Colors.black54,
415+
iconColor: Colors.white,
416+
isFinished: isFinished,
417+
isPlaying: controller.value.isPlaying,
418+
show: showPlayButton,
419+
onPressed: _playPause,
420+
),
421+
),
422+
if (!isFinished && !chewieController.isLive)
423+
CenterSeekButton(
424+
iconData: Icons.forward_10,
425+
backgroundColor: Colors.black54,
426+
iconColor: Colors.white,
427+
show: showPlayButton,
428+
fadeDuration: chewieController.materialSeekButtonFadeDuration,
429+
iconSize: chewieController.materialSeekButtonSize,
430+
onPressed: _seekForward,
431+
),
432+
],
433+
),
398434
),
399435
);
400436
}
@@ -546,6 +582,36 @@ class _MaterialControlsState extends State<MaterialControls>
546582
});
547583
}
548584

585+
void _seekRelative(Duration relativeSeek) {
586+
_cancelAndRestartTimer();
587+
final position = _latestValue.position + relativeSeek;
588+
final duration = _latestValue.duration;
589+
590+
if (position < Duration.zero) {
591+
controller.seekTo(Duration.zero);
592+
} else if (position > duration) {
593+
controller.seekTo(duration);
594+
} else {
595+
controller.seekTo(position);
596+
}
597+
}
598+
599+
void _seekBackward() {
600+
_seekRelative(
601+
const Duration(
602+
seconds: -10,
603+
),
604+
);
605+
}
606+
607+
void _seekForward() {
608+
_seekRelative(
609+
const Duration(
610+
seconds: 10,
611+
),
612+
);
613+
}
614+
549615
void _startHideTimer() {
550616
final hideControlsTimer = chewieController.hideControlsTimer.isNegative
551617
? ChewieController.defaultHideControlsTimer

0 commit comments

Comments
 (0)