[mob][photos] Make dragging of seek bar interactive, both in the seek bar widget and in the video

This commit is contained in:
ashilkn
2024-08-07 15:39:09 +05:30
parent 7795625708
commit af758d4e85

View File

@@ -2,6 +2,7 @@ import "dart:async";
import "package:flutter/material.dart"; import "package:flutter/material.dart";
import "package:native_video_player/native_video_player.dart"; import "package:native_video_player/native_video_player.dart";
import "package:photos/utils/debouncer.dart";
class SeekBar extends StatefulWidget { class SeekBar extends StatefulWidget {
final NativeVideoPlayerController controller; final NativeVideoPlayerController controller;
@@ -15,7 +16,10 @@ class SeekBar extends StatefulWidget {
class _SeekBarState extends State<SeekBar> with SingleTickerProviderStateMixin { class _SeekBarState extends State<SeekBar> with SingleTickerProviderStateMixin {
late final AnimationController _animationController; late final AnimationController _animationController;
double _prevPositionFraction = 0.0; double _prevPositionFraction = 0.0;
final _debouncer = Debouncer(
const Duration(milliseconds: 100),
executionInterval: const Duration(milliseconds: 325),
);
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@@ -66,14 +70,18 @@ class _SeekBarState extends State<SeekBar> with SingleTickerProviderStateMixin {
min: 0.0, min: 0.0,
max: 1.0, max: 1.0,
value: _animationController.value, value: _animationController.value,
onChanged: (value) {}, onChanged: (value) {
setState(() {
_animationController.value = value;
});
_seekTo(value);
},
divisions: 4500, divisions: 4500,
onChangeEnd: (value) { onChangeEnd: (value) {
widget.controller.seekTo((value * widget.duration!).round()); setState(() {
_animationController.animateTo( _animationController.value = value;
value, });
duration: const Duration(microseconds: 0), _seekTo(value);
);
}, },
allowedInteraction: SliderInteraction.tapAndSlide, allowedInteraction: SliderInteraction.tapAndSlide,
), ),
@@ -82,6 +90,14 @@ class _SeekBarState extends State<SeekBar> with SingleTickerProviderStateMixin {
); );
} }
void _seekTo(double value) {
_debouncer.run(() async {
unawaited(
widget.controller.seekTo((value * widget.duration!).round()),
);
});
}
void _startMovingSeekbar() { void _startMovingSeekbar() {
//Video starts playing after a slight delay. This delay is to ensure that //Video starts playing after a slight delay. This delay is to ensure that
//the seek bar animation starts after the video starts playing. //the seek bar animation starts after the video starts playing.
@@ -119,7 +135,7 @@ class _SeekBarState extends State<SeekBar> with SingleTickerProviderStateMixin {
//To immediately set the position to 0 when the ends when playing in loop //To immediately set the position to 0 when the ends when playing in loop
if (_prevPositionFraction == 1.0 && target == 0.0) { if (_prevPositionFraction == 1.0 && target == 0.0) {
unawaited( unawaited(
_animationController.animateTo(0, duration: const Duration(seconds: 0)), _animationController.animateTo(0, duration: Duration.zero),
); );
} }