Files
QingTing-Player/lib/widgets/global_mini_player.dart
T
lxh2875931338 708ab91ccb fix(playback): 播放页/迷你播放器立即显示已有封面,不再等元数据流程
原因有两层:
1) Song 模型没有封面路径,播放时只带 url/title/artist,
   播放页只能等元数据服务返回 artwork 字节后才显示;
2) 元数据回来后用新 Song 覆盖当前歌曲时丢了 artworkPath,
   即使带上了也会被冲掉。

- Song 增加 artworkPath 字段
- PlaybackController / 首页收藏 / playPlaylist 构造 Song 时带上
- 播放页:artwork 字节优先,其次直接读 artwork_path 文件
- 迷你播放器同样加 FileImage 兜底
- 元数据更新重建 Song 时保留 artworkPath

真机验证:点击播放后封面立即出现,不再先显示音符占位。
2026-11-01 16:15:49 +08:00

156 lines
6.2 KiB
Dart

import 'dart:io';
// lib/widgets/global_mini_player.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/audio_service.dart';
import '../utils/navigation.dart'; // ⭐ 导入 navigatorKey
class GlobalMiniPlayer extends StatelessWidget {
const GlobalMiniPlayer({super.key});
@override
Widget build(BuildContext context) {
final song = context.select<AudioService, Song?>((s) => s.currentSong);
final isPlaying = context.select<AudioService, bool>((s) => s.isPlaying);
final bottomPadding = MediaQuery.of(context).padding.bottom;
if (song == null) {
return const SizedBox.shrink();
}
return Stack(
clipBehavior: Clip.none,
children: [
Positioned(
left: 0,
right: 0,
bottom: 0,
child: Container(
height: 60 + bottomPadding,
color: const Color(0xFF1A1F1E),
),
),
Positioned(
left: 0,
right: 0,
bottom: bottomPadding,
child: ClipRRect(
borderRadius: const BorderRadius.vertical(
bottom: Radius.circular(16),
),
child: Container(
height: 60,
color: const Color(0xFF1A1F1E),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
// ⭐ 使用 navigatorKey 直接跳转
navigatorKey.currentState?.pushNamed('/player');
},
highlightColor: Colors.white.withValues(alpha: 0.05),
splashColor: Colors.white.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
child: Row(
children: [
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: const Color(0xFF2A3332),
image: song.artwork != null
? DecorationImage(
image: MemoryImage(song.artwork!),
fit: BoxFit.cover,
)
: (song.artworkPath != null &&
song.artworkPath!.isNotEmpty &&
File(song.artworkPath!)
.existsSync())
? DecorationImage(
image: FileImage(
File(song.artworkPath!)),
fit: BoxFit.cover,
)
: null,
),
child: (song.artwork == null &&
(song.artworkPath == null ||
song.artworkPath!.isEmpty ||
!File(song.artworkPath!)
.existsSync()))
? const Icon(
Icons.music_note,
color: Colors.white38,
size: 20,
)
: null,
),
const SizedBox(width: 12),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
song.title,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Colors.white,
decoration: TextDecoration.none,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Text(
song.artist,
style: TextStyle(
fontSize: 12,
color: Colors.grey[400],
decoration: TextDecoration.none,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
IconButton(
icon: Icon(
isPlaying ? Icons.pause : Icons.play_arrow,
color: Colors.white,
size: 24,
),
onPressed: () {
context.read<AudioService>().togglePlay();
},
),
IconButton(
icon: const Icon(
Icons.playlist_play_outlined,
color: Colors.grey,
size: 24,
),
onPressed: () {
// ⭐ 使用 navigatorKey 直接跳转
navigatorKey.currentState?.pushNamed('/playlist');
},
),
const SizedBox(width: 4),
],
),
),
),
),
),
),
),
],
);
}
}