feat(player): 未就绪改由进度条展示加载态

This commit is contained in:
Zhou Weidong committed 2026-10-21 10:07:34 +08:00
1 parent 76cc52ad66
commit cf4b07aa69
12 files changed
+359 -167

No files matched your search

-1
View File
@@ -319,7 +319,6 @@ class _PlayerPageState extends State<PlayerPage>
),
PlayerVideoSurface(
controller: videoPort?.controller,
posterUrl: drama.posterUrl,
videoUrl: videoPort?.controller.dataSource ?? episode.videoUrl,
theater: theater,
onTap: () =>
@@ -10,6 +10,7 @@ import '../../../core/formatting/localized_content.dart';
import '../../../core/theme/app_colors.dart';
import '../../../l10n/app_localizations.dart';
import 'interaction_rail.dart';
import 'loading_progress_track.dart';
part 'home_feed_overlay_parts.dart';
@@ -37,10 +37,13 @@ class _FeedProgressBarState extends State<_FeedProgressBar> {
final width = constraints.maxWidth;
final source = widget.controller;
final track = source == null
? _track(0, width)
? _loadingTrack()
: ValueListenableBuilder<VideoPlayerValue>(
valueListenable: source,
builder: (context, value, _) {
if (!value.isInitialized || value.isBuffering) {
return _loadingTrack();
}
final total = value.duration.inMilliseconds;
final played = total <= 0
? 0.0
@@ -67,6 +70,23 @@ class _FeedProgressBarState extends State<_FeedProgressBar> {
);
}
Widget _loadingTrack() {
final bottom = _lineCenterFromBottom - widget.height / 2;
return SizedBox(
height: widget.hitHeight,
child: Stack(
children: [
Positioned(
left: 0,
right: 0,
bottom: bottom,
child: LoadingProgressTrack(height: widget.height),
),
],
),
);
}
void _update(double fraction) {
setState(() => _preview = fraction.clamp(0.0, 1.0).toDouble());
}
@@ -0,0 +1,102 @@
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import '../../../core/theme/app_colors.dart';
/// 播放未就绪时沿进度轨道循环移动的加载高光。
class LoadingProgressTrack extends StatefulWidget {
const LoadingProgressTrack({
super.key = const Key('progress-loading'),
required this.height,
this.trackColor = Colors.white24,
this.highlightColor = AppColors.blue,
});
final double height;
final Color trackColor;
final Color highlightColor;
@override
State<LoadingProgressTrack> createState() => _LoadingProgressTrackState();
}
class _LoadingProgressTrackState extends State<LoadingProgressTrack>
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
late final AnimationController _controller;
late final Animation<double> _position;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1200),
);
_position = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
WidgetsBinding.instance.addObserver(this);
_syncAnimation();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
_syncAnimation();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_controller.dispose();
super.dispose();
}
void _syncAnimation() {
if (WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed) {
if (!_controller.isAnimating) _controller.repeat();
} else {
_controller.stop();
}
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: widget.height,
child: ClipRRect(
borderRadius: BorderRadius.circular(widget.height / 2),
child: LayoutBuilder(
builder: (context, constraints) {
final highlightWidth = constraints.maxWidth * 0.28;
final travel = constraints.maxWidth + highlightWidth;
return AnimatedBuilder(
animation: _position,
builder: (context, _) {
final left = -highlightWidth + travel * _position.value;
return Stack(
fit: StackFit.expand,
children: [
ColoredBox(
key: const Key('progress-loading-track'),
color: widget.trackColor,
),
Positioned(
left: left,
top: 0,
bottom: 0,
width: highlightWidth,
child: ColoredBox(
key: const Key('progress-loading-highlight'),
color: widget.highlightColor,
),
),
],
);
},
);
},
),
),
);
}
}
@@ -4,25 +4,21 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import '../../../core/widgets/remote_image.dart';
const Duration _videoFadeDuration = Duration(milliseconds: 200);
const double _errorHorizontal = 32;
const int _errorMaxLines = 3;
/// 封面底层与视频层共用同一控制器,视频首帧就绪后再淡入。
/// 黑色底层与视频层共用同一控制器,视频首帧就绪后再淡入。
class PlayerVideoSurface extends StatefulWidget {
const PlayerVideoSurface({
super.key,
required this.controller,
required this.posterUrl,
required this.videoUrl,
required this.theater,
required this.onTap,
});
final VideoPlayerController? controller;
final String posterUrl;
final String videoUrl;
final bool theater;
final VoidCallback onTap;
@@ -57,7 +53,10 @@ class _PlayerVideoSurfaceState extends State<PlayerVideoSurface> {
return Stack(
fit: StackFit.expand,
children: [
_FallbackBackground(posterUrl: widget.posterUrl),
const ColoredBox(
key: Key('player-fallback-background'),
color: Colors.black,
),
AnimatedOpacity(
key: const Key('player-video-opacity'),
opacity: reveal ? 1 : 0,
@@ -164,35 +163,3 @@ class _VideoLayer extends StatelessWidget {
);
}
}
class _FallbackBackground extends StatelessWidget {
const _FallbackBackground({required this.posterUrl});
final String posterUrl;
@override
Widget build(BuildContext context) {
if (posterUrl.trim().isEmpty) {
return const ColoredBox(
key: Key('player-fallback-background'),
color: Colors.black,
);
}
return Stack(
key: const Key('player-fallback-background'),
fit: StackFit.expand,
children: [
RemoteImage(
posterUrl,
key: const Key('player-fallback-cover'),
fit: BoxFit.cover,
fallbackColor: Colors.black,
),
const ColoredBox(
key: Key('player-fallback-scrim'),
color: Color(0x4D000000),
),
],
);
}
}
+55 -42
View File
@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import '../../../core/theme/app_colors.dart';
import 'loading_progress_track.dart';
/// Keeps seeks within the playable range so dragging to the edge does not
/// trigger the player's completion callback before playback resumes.
@@ -92,6 +93,7 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
Widget _buildTrack(BuildContext context, VideoPlayerValue video) {
final total = video.duration.inMilliseconds;
final loading = !video.isInitialized || video.isBuffering;
final enabled = video.isInitialized && total > 0 && widget.onSeek != null;
final played = total > 0
? (video.position.inMilliseconds / total).clamp(0.0, 1.0)
@@ -100,7 +102,15 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
final previewPosition = _previewFraction.value == null
? video.position
: seekPositionForFraction(fraction, video.duration);
final scrubbing = _previewFraction.value != null;
final scrubbing = !loading && _previewFraction.value != null;
final trackHeight = widget.compact
? (scrubbing ? 3.0 : 1.5)
: (scrubbing ? 4.0 : 3.0);
final trackAreaHeight = loading
? (widget.compact ? 20.0 : 44.0)
: widget.compact
? (scrubbing ? 32.0 : 20.0)
: (scrubbing ? 48.0 : 44.0);
return Column(
mainAxisSize: MainAxisSize.min,
@@ -120,47 +130,50 @@ class _SeekProgressBarState extends State<SeekProgressBar> {
),
),
SizedBox(
height: widget.compact
? (scrubbing ? 32 : 20)
: (scrubbing ? 48 : 44),
child: SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: widget.compact
? (scrubbing ? 3 : 1.5)
: (scrubbing ? 4 : 3),
activeTrackColor: AppColors.blue,
inactiveTrackColor: widget.inactiveTrackColor,
thumbColor: AppColors.onPrimary,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: widget.compact
? (scrubbing ? 6 : 4)
: (scrubbing ? 8 : 6),
),
overlayShape: RoundSliderOverlayShape(
overlayRadius: widget.compact ? 12 : 18,
),
),
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,
),
),
),
height: trackAreaHeight,
child: loading
? Center(
child: LoadingProgressTrack(
height: trackHeight,
trackColor: widget.inactiveTrackColor,
),
)
: SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: trackHeight,
activeTrackColor: AppColors.blue,
inactiveTrackColor: widget.inactiveTrackColor,
thumbColor: AppColors.onPrimary,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: widget.compact
? (scrubbing ? 6 : 4)
: (scrubbing ? 8 : 6),
),
overlayShape: RoundSliderOverlayShape(
overlayRadius: widget.compact ? 12 : 18,
),
),
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,
),
),
),
),
],
);