Skip to content

Commit 9caca81

Browse files
committed
Adding Seek buttons for Android
1 parent 963fc3f commit 9caca81

File tree

2 files changed

+116
-7
lines changed

2 files changed

+116
-7
lines changed

lib/src/center_seek_button.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.onPressed,
11+
}) : super(key: key);
12+
13+
final IconData iconData;
14+
final Color backgroundColor;
15+
final Color? iconColor;
16+
final bool show;
17+
final VoidCallback? onPressed;
18+
19+
@override
20+
Widget build(BuildContext context) {
21+
return ColoredBox(
22+
color: Colors.transparent,
23+
child: Center(
24+
child: UnconstrainedBox(
25+
child: AnimatedOpacity(
26+
opacity: show ? 1.0 : 0.0,
27+
duration: const Duration(milliseconds: 300),
28+
child: DecoratedBox(
29+
decoration: BoxDecoration(
30+
color: backgroundColor,
31+
shape: BoxShape.circle,
32+
),
33+
// Always set the iconSize on the IconButton, not on the Icon itself:
34+
// https://github.com/flutter/flutter/issues/52980
35+
child: IconButton(
36+
iconSize: 26,
37+
padding: const EdgeInsets.all(8.0),
38+
icon: Icon(iconData, color: iconColor),
39+
onPressed: onPressed,
40+
),
41+
),
42+
),
43+
),
44+
),
45+
);
46+
}
47+
}

lib/src/material/material_controls.dart

Lines changed: 69 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';
@@ -385,13 +386,44 @@ class _MaterialControlsState extends State<MaterialControls>
385386
});
386387
}
387388
},
388-
child: CenterPlayButton(
389-
backgroundColor: Colors.black54,
390-
iconColor: Colors.white,
391-
isFinished: isFinished,
392-
isPlaying: controller.value.isPlaying,
393-
show: showPlayButton,
394-
onPressed: _playPause,
389+
child: Container(
390+
alignment: Alignment.center,
391+
color: Colors
392+
.transparent, // The Gesture Detector doesn't expand to the full size of the container without this; Not sure why!
393+
child: Row(
394+
mainAxisAlignment: MainAxisAlignment.center,
395+
children: [
396+
if (!isFinished && !chewieController.isLive)
397+
CenterSeekButton(
398+
iconData: Icons.replay_10,
399+
backgroundColor: Colors.black54,
400+
iconColor: Colors.white,
401+
show: showPlayButton,
402+
onPressed: _seekBackward,
403+
),
404+
Container(
405+
margin: EdgeInsets.symmetric(
406+
horizontal: marginSize,
407+
),
408+
child: CenterPlayButton(
409+
backgroundColor: Colors.black54,
410+
iconColor: Colors.white,
411+
isFinished: isFinished,
412+
isPlaying: controller.value.isPlaying,
413+
show: showPlayButton,
414+
onPressed: _playPause,
415+
),
416+
),
417+
if (!isFinished && !chewieController.isLive)
418+
CenterSeekButton(
419+
iconData: Icons.forward_10,
420+
backgroundColor: Colors.black54,
421+
iconColor: Colors.white,
422+
show: showPlayButton,
423+
onPressed: _seekForward,
424+
),
425+
],
426+
),
395427
),
396428
);
397429
}
@@ -542,6 +574,36 @@ class _MaterialControlsState extends State<MaterialControls>
542574
});
543575
}
544576

577+
void _seekRelative(Duration relativeSeek) {
578+
_cancelAndRestartTimer();
579+
final position = _latestValue.position + relativeSeek;
580+
final duration = _latestValue.duration;
581+
582+
if (position < Duration.zero) {
583+
controller.seekTo(Duration.zero);
584+
} else if (position > duration) {
585+
controller.seekTo(duration);
586+
} else {
587+
controller.seekTo(position);
588+
}
589+
}
590+
591+
void _seekBackward() {
592+
_seekRelative(
593+
const Duration(
594+
seconds: -10,
595+
),
596+
);
597+
}
598+
599+
void _seekForward() {
600+
_seekRelative(
601+
const Duration(
602+
seconds: 10,
603+
),
604+
);
605+
}
606+
545607
void _startHideTimer() {
546608
final hideControlsTimer = chewieController.hideControlsTimer.isNegative
547609
? ChewieController.defaultHideControlsTimer

0 commit comments

Comments
 (0)