Files
QingTing-Player/lib/pages/player_page.dart
T

298 lines
10 KiB
Dart

// lib/pages/player_page.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/audio_service.dart';
class PlayerPage extends StatefulWidget {
const PlayerPage({super.key});
@override
State<PlayerPage> createState() => _PlayerPageState();
}
class _PlayerPageState extends State<PlayerPage> {
double? _dragProgress; // 拖动时的临时进度
String _formatDuration(Duration d) {
if (d.inMilliseconds < 0) return '00:00';
final minutes = d.inMinutes;
final seconds = d.inSeconds % 60;
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
}
@override
Widget build(BuildContext context) {
final service = context.watch<AudioService>();
final song = service.currentSong;
if (song == null) {
return Scaffold(
backgroundColor: const Color(0xFF0E1211),
body: const Center(
child: Text(
'没有正在播放的歌曲',
style: TextStyle(color: Colors.grey),
),
),
);
}
final isPlaying = service.isPlaying;
final position = service.position;
final duration = service.duration;
final bufferedPosition = service.bufferedPosition;
final progress = duration.inMilliseconds > 0
? position.inMilliseconds / duration.inMilliseconds
: 0.0;
final bufferProgress = duration.inMilliseconds > 0
? (bufferedPosition.inMilliseconds / duration.inMilliseconds)
.clamp(0.0, 1.0)
: 0.0;
// 拖动时显示拖动的进度,否则显示实际播放进度
final displayProgress = _dragProgress ?? progress;
return Scaffold(
backgroundColor: const Color(0xFF0E1211),
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios_new, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
title: const Text(
'正在播放',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w400,
),
),
centerTitle: true,
actions: [
IconButton(
icon: const Icon(Icons.more_vert, color: Colors.white54),
onPressed: () {},
),
],
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// ---- 封面占位 ----
Container(
width: 240,
height: 240,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: const Color(0xFF2A3332),
boxShadow: [
BoxShadow(
color: const Color(0xFF7C9A9E).withOpacity(0.15),
blurRadius: 60,
spreadRadius: 10,
),
],
),
child: const Icon(
Icons.music_note,
size: 64,
color: Colors.white24,
),
),
const SizedBox(height: 48),
// ---- 歌曲信息 ----
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
song.title,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Colors.white,
),
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 8),
Text(
song.artist,
style: TextStyle(
fontSize: 15,
color: Colors.grey[400],
),
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
const SizedBox(height: 40),
// ---- 双层进度条(视觉与交互分离) ----
Column(
children: [
SizedBox(
height: 20,
child: LayoutBuilder(
builder: (context, constraints) {
final width = constraints.maxWidth;
final bufferWidth =
width * bufferProgress.clamp(0.0, 1.0);
final progressWidth = width * progress.clamp(0.0, 1.0);
return Stack(
alignment: Alignment.centerLeft,
children: [
// ① 底层:整首歌曲(暗灰色)
Container(
width: width,
height: 4,
decoration: BoxDecoration(
color: Colors.grey[800],
borderRadius: BorderRadius.circular(2),
),
),
// ② 缓冲层(深青色)
Container(
width: bufferWidth,
height: 4,
decoration: BoxDecoration(
color: const Color(0xFF4A7A7A),
borderRadius: BorderRadius.circular(2),
),
),
// ③ 播放进度层(亮青色)
Container(
width: progressWidth,
height: 4,
decoration: BoxDecoration(
color: const Color(0xFFB8D4D0),
borderRadius: BorderRadius.circular(2),
),
),
// ④ 交互层(只负责手势,不显示任何轨道)
Positioned.fill(
child: SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: 0,
activeTrackColor: Colors.transparent,
inactiveTrackColor: Colors.transparent,
thumbColor: Colors.transparent, // ✅ 完全隐藏小球
overlayColor: Colors.transparent,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 0,
),
),
child: Slider(
value: progress.clamp(0.0, 1.0),
min: 0.0,
max: 1.0,
onChanged: (value) {
// 拖动时只更新本地状态,不触发 seek
setState(() {
_dragProgress = value;
});
},
onChangeEnd: (value) {
final newPosition = Duration(
milliseconds:
(value * duration.inMilliseconds)
.round(),
);
service.seekTo(newPosition);
setState(() {
_dragProgress = null;
});
},
),
),
),
],
);
},
),
),
// 时间显示
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_formatDuration(position),
style: TextStyle(
fontSize: 12,
color: Colors.grey[500],
),
),
Text(
_formatDuration(duration),
style: TextStyle(
fontSize: 12,
color: Colors.grey[500],
),
),
],
),
],
),
const SizedBox(height: 32),
// ---- 播放控制 ----
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.skip_previous, size: 28),
color: Colors.white60,
padding: const EdgeInsets.all(12),
),
const SizedBox(width: 20),
Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: const Color(0xFFB8D4D0),
shape: BoxShape.circle,
),
child: IconButton(
icon: Icon(
isPlaying ? Icons.pause : Icons.play_arrow,
color: Colors.black87,
size: 28,
),
padding: EdgeInsets.zero,
onPressed: () {
service.togglePlay();
},
),
),
const SizedBox(width: 20),
IconButton(
onPressed: () {},
icon: const Icon(Icons.skip_next, size: 28),
color: Colors.white60,
padding: const EdgeInsets.all(12),
),
],
),
const SizedBox(height: 16),
],
),
),
);
}
}