fix(player): 修正进度条按下与拖动状态

This commit is contained in:
Zhou Weidong committed 2026-10-22 20:23:24 +08:00
1 parent 7599da1d40
commit d66ff13d0f
3 files changed
+221 -26

No files matched your search

+66 -26
View File
@@ -30,6 +30,7 @@ class SeekProgressBar extends StatefulWidget {
required this.valueListenable,
required this.semanticsLabel,
this.onScrubStart,
this.onScrubEnd,
this.onSeek,
this.showTimes = true,
this.compact = false,
@@ -45,6 +46,7 @@ class SeekProgressBar extends StatefulWidget {
final ValueListenable<VideoPlayerValue>? valueListenable;
final String semanticsLabel;
final VoidCallback? onScrubStart;
final VoidCallback? onScrubEnd;
final Future<void> Function(Duration)? onSeek;
final bool showTimes;
final bool compact;
@@ -66,6 +68,10 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
final _previewFraction = ValueNotifier<double?>(null);
Timer? _bufferTimer;
bool _buffering = false;
bool _dragging = false;
bool _pointerMoved = false;
Offset? _pointerStart;
double? _dragStartFraction;
@override
void initState() {
@@ -165,10 +171,14 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
}
Future<void> _finish(double fraction, Duration duration) async {
if (widget.locked) return;
_previewFraction.value = fraction;
if (widget.locked || !_dragging) return;
setState(() => _dragging = false);
_pointerMoved = false;
_pointerStart = null;
_dragStartFraction = null;
widget.thumbnailService?.cancelPending();
_previewFraction.value = null;
widget.onScrubEnd?.call();
await widget.onSeek?.call(seekPositionForFraction(fraction, duration));
}
@@ -193,7 +203,7 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
Widget _buildTrack(BuildContext context, VideoPlayerValue video) {
final total = video.duration.inMilliseconds;
final scrubbing = _previewFraction.value != null;
final scrubbing = _dragging;
final loading = !widget.locked && !scrubbing && _buffering;
final enabled =
!widget.locked &&
@@ -212,9 +222,7 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
widget.showTimes &&
widget.thumbnailService != null &&
(widget.episodeId?.isNotEmpty ?? false);
final trackHeight = widget.compact
? (scrubbing ? 3.0 : 1.5)
: (scrubbing ? 4.0 : 3.0);
final trackHeight = scrubbing ? 6.0 : 2.0;
final trackAreaHeight = loading
? (widget.compact ? 20.0 : 44.0)
: widget.compact
@@ -258,9 +266,7 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
inactiveTrackColor: widget.inactiveTrackColor,
thumbColor: AppColors.onPrimary,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: widget.compact
? (scrubbing ? 6 : 4)
: (scrubbing ? 8 : 6),
enabledThumbRadius: scrubbing ? 8 : 6,
),
overlayShape: RoundSliderOverlayShape(
overlayRadius: widget.compact ? 12 : 18,
@@ -268,23 +274,57 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
),
child: Semantics(
label: widget.semanticsLabel,
child: Slider(
value: fraction,
// 轨道与上方文案共用同一层水平内边距,避免 Slider 默认内缩。
padding: EdgeInsets.zero,
onChangeStart: enabled
? (value) {
_previewFraction.value = value;
widget.onScrubStart?.call();
}
: null,
onChanged: enabled
? (value) => _previewFraction.value = value
: null,
onChangeEnd: enabled
? (value) =>
unawaited(_finish(value, video.duration))
: null,
child: Listener(
onPointerDown: (event) {
_pointerStart = event.position;
_pointerMoved = false;
},
onPointerMove: (event) {
final start = _pointerStart;
if (start != null &&
(event.position - start).distance > 0) {
_pointerMoved = true;
}
},
onPointerUp: (_) {
if (!_dragging) {
_pointerMoved = false;
_pointerStart = null;
_dragStartFraction = null;
}
},
onPointerCancel: (_) {
if (!_dragging) {
_pointerMoved = false;
_pointerStart = null;
_dragStartFraction = null;
}
},
child: Slider(
value: fraction,
// 轨道与上方文案共用同一层水平内边距,避免 Slider 默认内缩。
padding: EdgeInsets.zero,
onChangeStart: enabled
? (value) {
_dragStartFraction = value;
_pointerMoved = false;
}
: null,
onChanged: enabled
? (value) {
if (!_pointerMoved) return;
if (!_dragging) {
setState(() => _dragging = true);
widget.onScrubStart?.call();
}
_previewFraction.value = value;
}
: null,
onChangeEnd: enabled
? (value) =>
unawaited(_finish(value, video.duration))
: null,
),
),
),
),