fix(player): 修复拖动进度条时加载态闪烁
This commit is contained in:
1 parent
72708630ba
commit
1993ff1102
5 files changed
+350
-13
No files matched your search
@@ -1,5 +1,7 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
|
||||
@@ -28,7 +28,36 @@ class _FeedProgressBar extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _FeedProgressBarState extends State<_FeedProgressBar> {
|
||||
static const _bufferDebounce = Duration(milliseconds: 350);
|
||||
|
||||
double? _preview;
|
||||
Timer? _bufferTimer;
|
||||
bool _buffering = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_attachSource();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _FeedProgressBar oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.controller != widget.controller) {
|
||||
_detachSource(oldWidget.controller);
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_buffering = false;
|
||||
_attachSource();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_detachSource(widget.controller);
|
||||
_bufferTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -36,12 +65,16 @@ class _FeedProgressBarState extends State<_FeedProgressBar> {
|
||||
builder: (context, constraints) {
|
||||
final width = constraints.maxWidth;
|
||||
final source = widget.controller;
|
||||
final scrubbing = _preview != null;
|
||||
final loading =
|
||||
source == null ||
|
||||
(!scrubbing && (!source.value.isInitialized || _buffering));
|
||||
final track = source == null
|
||||
? _loadingTrack()
|
||||
: ValueListenableBuilder<VideoPlayerValue>(
|
||||
valueListenable: source,
|
||||
builder: (context, value, _) {
|
||||
if (!value.isInitialized || value.isBuffering) {
|
||||
if (!scrubbing && (!value.isInitialized || _buffering)) {
|
||||
return _loadingTrack();
|
||||
}
|
||||
final total = value.duration.inMilliseconds;
|
||||
@@ -56,20 +89,80 @@ class _FeedProgressBarState extends State<_FeedProgressBar> {
|
||||
return GestureDetector(
|
||||
key: const Key('home-feed-progress'),
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onHorizontalDragStart: (details) {
|
||||
widget.onScrubStart?.call();
|
||||
_update(details.localPosition.dx / width);
|
||||
},
|
||||
onHorizontalDragUpdate: (details) =>
|
||||
_update(details.localPosition.dx / width),
|
||||
onHorizontalDragEnd: (_) => _commit(_preview),
|
||||
onHorizontalDragCancel: () => setState(() => _preview = null),
|
||||
onHorizontalDragStart: loading
|
||||
? null
|
||||
: (details) {
|
||||
widget.onScrubStart?.call();
|
||||
_update(details.localPosition.dx / width);
|
||||
},
|
||||
onHorizontalDragUpdate: loading
|
||||
? null
|
||||
: (details) => _update(details.localPosition.dx / width),
|
||||
onHorizontalDragEnd: loading ? null : (_) => _commit(_preview),
|
||||
onHorizontalDragCancel: loading
|
||||
? null
|
||||
: () => setState(() => _preview = null),
|
||||
child: track,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _attachSource() {
|
||||
widget.controller?.addListener(_handleSourceChanged);
|
||||
_syncBufferState(notify: false);
|
||||
}
|
||||
|
||||
void _detachSource(ValueListenable<VideoPlayerValue>? source) {
|
||||
source?.removeListener(_handleSourceChanged);
|
||||
}
|
||||
|
||||
void _handleSourceChanged() => _syncBufferState();
|
||||
|
||||
void _syncBufferState({bool notify = true}) {
|
||||
final source = widget.controller;
|
||||
if (source == null) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(false, notify: notify);
|
||||
return;
|
||||
}
|
||||
final value = source.value;
|
||||
if (!value.isInitialized) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(true, notify: notify);
|
||||
return;
|
||||
}
|
||||
if (!value.isBuffering) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(false, notify: notify);
|
||||
return;
|
||||
}
|
||||
if (_buffering || _bufferTimer != null) return;
|
||||
_bufferTimer = Timer(_bufferDebounce, () {
|
||||
_bufferTimer = null;
|
||||
final current = widget.controller;
|
||||
if (!mounted ||
|
||||
current != source ||
|
||||
!current!.value.isInitialized ||
|
||||
!current.value.isBuffering) {
|
||||
return;
|
||||
}
|
||||
_setBuffering(true);
|
||||
});
|
||||
}
|
||||
|
||||
void _setBuffering(bool buffering, {bool notify = true}) {
|
||||
if (_buffering == buffering) return;
|
||||
if (!notify || !mounted) {
|
||||
_buffering = buffering;
|
||||
return;
|
||||
}
|
||||
setState(() => _buffering = buffering);
|
||||
}
|
||||
|
||||
Widget _loadingTrack() {
|
||||
final bottom = _lineCenterFromBottom - widget.height / 2;
|
||||
return SizedBox(
|
||||
|
||||
@@ -44,22 +44,94 @@ class SeekProgressBar extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SeekProgressBarState extends State<SeekProgressBar> {
|
||||
static const _bufferDebounce = Duration(milliseconds: 350);
|
||||
|
||||
final _previewFraction = ValueNotifier<double?>(null);
|
||||
Timer? _bufferTimer;
|
||||
bool _buffering = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_attachSource();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant SeekProgressBar oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.valueListenable != widget.valueListenable) {
|
||||
_detachSource(oldWidget.valueListenable);
|
||||
_previewFraction.value = null;
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_buffering = false;
|
||||
_attachSource();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_detachSource(widget.valueListenable);
|
||||
_bufferTimer?.cancel();
|
||||
_previewFraction.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _attachSource() {
|
||||
widget.valueListenable?.addListener(_handleSourceChanged);
|
||||
_syncBufferState(notify: false);
|
||||
}
|
||||
|
||||
void _detachSource(ValueListenable<VideoPlayerValue>? source) {
|
||||
source?.removeListener(_handleSourceChanged);
|
||||
}
|
||||
|
||||
void _handleSourceChanged() => _syncBufferState();
|
||||
|
||||
void _syncBufferState({bool notify = true}) {
|
||||
final source = widget.valueListenable;
|
||||
if (source == null) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(false, notify: notify);
|
||||
return;
|
||||
}
|
||||
final value = source.value;
|
||||
if (!value.isInitialized) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(true, notify: notify);
|
||||
return;
|
||||
}
|
||||
if (!value.isBuffering) {
|
||||
_bufferTimer?.cancel();
|
||||
_bufferTimer = null;
|
||||
_setBuffering(false, notify: notify);
|
||||
return;
|
||||
}
|
||||
if (_buffering || _bufferTimer != null) return;
|
||||
_bufferTimer = Timer(_bufferDebounce, () {
|
||||
_bufferTimer = null;
|
||||
final current = widget.valueListenable;
|
||||
if (!mounted ||
|
||||
current != source ||
|
||||
!current!.value.isInitialized ||
|
||||
!current.value.isBuffering) {
|
||||
return;
|
||||
}
|
||||
_setBuffering(true);
|
||||
});
|
||||
}
|
||||
|
||||
void _setBuffering(bool buffering, {bool notify = true}) {
|
||||
if (_buffering == buffering) return;
|
||||
if (!notify || !mounted) {
|
||||
_buffering = buffering;
|
||||
return;
|
||||
}
|
||||
setState(() => _buffering = buffering);
|
||||
}
|
||||
|
||||
Future<void> _finish(double fraction, Duration duration) async {
|
||||
final source = widget.valueListenable;
|
||||
_previewFraction.value = fraction;
|
||||
@@ -93,7 +165,8 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
|
||||
|
||||
Widget _buildTrack(BuildContext context, VideoPlayerValue video) {
|
||||
final total = video.duration.inMilliseconds;
|
||||
final loading = !video.isInitialized || video.isBuffering;
|
||||
final scrubbing = _previewFraction.value != null;
|
||||
final loading = !scrubbing && (!video.isInitialized || _buffering);
|
||||
final enabled = video.isInitialized && total > 0 && widget.onSeek != null;
|
||||
final played = total > 0
|
||||
? (video.position.inMilliseconds / total).clamp(0.0, 1.0)
|
||||
@@ -102,7 +175,6 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
|
||||
final previewPosition = _previewFraction.value == null
|
||||
? video.position
|
||||
: seekPositionForFraction(fraction, video.duration);
|
||||
final scrubbing = !loading && _previewFraction.value != null;
|
||||
final trackHeight = widget.compact
|
||||
? (scrubbing ? 3.0 : 1.5)
|
||||
: (scrubbing ? 4.0 : 3.0);
|
||||
|
||||
Reference in new issue
Block a user