界面部分阶段性达成效果

This commit is contained in:
2026-08-16 21:58:18 +08:00
parent 1dd669287c
commit 457a9bf1e5
6 changed files with 562 additions and 299 deletions
+65 -65
View File
@@ -1,3 +1,4 @@
// lib/pages/home_page.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/audio_service.dart';
@@ -5,16 +6,17 @@ import '../services/webdav_service.dart';
import '../services/playback_service.dart';
import '../widgets/mini_player_bar.dart';
import 'webdav_setup_page.dart';
import 'webdav_file_list_page.dart';
// ==================================================
// 歌曲数据模型(含元数据状态)
// ==================================================
class SongItem {
final String path; // WebDAV 完整路径
final String fileName; // 文件名
final String? title; // 元数据标题
final String? artist; // 元数据艺术家
final String sourceTag; // 来源标签
final String path;
final String fileName;
final String? title;
final String? artist;
final String sourceTag;
final String metadataState; // "unknown" | "loading" | "success" | "failed"
SongItem({
@@ -135,8 +137,6 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
List<SongItem> _favorites = [];
bool _isWebDAVConnected = false;
String _webDAVUsername = '';
bool _isLoading = true;
@override
@@ -150,19 +150,12 @@ class _HomePageState extends State<HomePage> {
try {
final hasCred = await WebDAVService.instance.loadCredentials();
if (hasCred) {
_isWebDAVConnected = true;
// 从 BaseUrl 中提取用户名(简化展示)
final baseUrl = WebDAVService.instance.baseUrl;
_webDAVUsername =
baseUrl?.replaceAll(RegExp(r'^https?://'), '').split('/').first ??
'已连接';
await _loadMusicList();
// ✅ 不再自动加载音乐到收藏
_favorites = [];
} else {
_isWebDAVConnected = false;
_favorites = [];
}
} catch (e) {
_isWebDAVConnected = false;
_favorites = [];
} finally {
if (mounted) setState(() => _isLoading = false);
@@ -185,23 +178,12 @@ class _HomePageState extends State<HomePage> {
}).toList();
});
} catch (e) {
// 加载失败保持空列表
setState(() => _favorites = []);
}
}
// 刷新列表(从 WebDAV 设置页返回时调用)
Future<void> _refreshFromWebDAV() async {
final hasCred = await WebDAVService.instance.loadCredentials();
setState(() {
_isWebDAVConnected = hasCred;
if (hasCred) {
final baseUrl = WebDAVService.instance.baseUrl;
_webDAVUsername =
baseUrl?.replaceAll(RegExp(r'^https?://'), '').split('/').first ??
'已连接';
}
});
if (hasCred) {
await _loadMusicList();
} else {
@@ -213,7 +195,6 @@ class _HomePageState extends State<HomePage> {
try {
final url = WebDAVService.instance.getFileUrl(song.path);
await PlaybackService().play(url);
// 更新 AudioService 状态
context.read<AudioService>().playSong(Song(
id: song.path,
title: song.displayTitle,
@@ -234,6 +215,9 @@ class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
// 直接从 WebDAVService 读取实时状态
final isConnected = WebDAVService.instance.isConnected;
final username = WebDAVService.instance.username ?? '点击连接';
final audioService = context.watch<AudioService>();
final showMiniBar = audioService.currentSong != null;
@@ -243,7 +227,7 @@ class _HomePageState extends State<HomePage> {
children: [
CustomScrollView(
slivers: [
// ---------- 顶部安全区域 + 标题 ----------
// ---- 顶部标题 ----
SliverToBoxAdapter(
child: SafeArea(
child: Padding(
@@ -270,7 +254,7 @@ class _HomePageState extends State<HomePage> {
),
),
// ---------- 媒体库区块 ----------
// ---- 媒体库 ----
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
@@ -290,14 +274,29 @@ class _HomePageState extends State<HomePage> {
// ---- WebDAV 入口 ----
_ClickableTile(
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const WebDAVSetupPage(),
),
);
if (result == true) {
await _refreshFromWebDAV();
if (WebDAVService.instance.isConnected) {
// 已连接 → 直接进入文件列表
await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const WebDAVFileListPage(),
),
);
// 返回后刷新界面(可能状态变化)
setState(() {});
} else {
// 未连接 → 进入设置页
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const WebDAVSetupPage(),
),
);
// 从设置页返回后刷新
setState(() {});
if (result == true) {
await _refreshFromWebDAV();
}
}
},
child: Row(
@@ -328,10 +327,10 @@ class _HomePageState extends State<HomePage> {
),
const SizedBox(width: 16),
Text(
_isWebDAVConnected ? '● 已连接' : '● 未连接',
isConnected ? '● 已连接' : '● 未连接',
style: TextStyle(
fontSize: 13,
color: _isWebDAVConnected
color: isConnected
? const Color(0xFF4CAF50)
: Colors.grey[500],
fontWeight: FontWeight.w400,
@@ -341,12 +340,10 @@ class _HomePageState extends State<HomePage> {
),
const SizedBox(height: 4),
Text(
_isWebDAVConnected
? _webDAVUsername
: '点击连接',
isConnected ? username : '点击连接',
style: TextStyle(
fontSize: 14,
color: _isWebDAVConnected
color: isConnected
? Colors.grey[400]
: Colors.grey[600],
),
@@ -450,34 +447,37 @@ class _HomePageState extends State<HomePage> {
),
),
// ---------- “我的收藏” Sticky Header ----------
// 在 home_page.dart 中,修改 SliverPersistentHeader 的 delegate
SliverPersistentHeader(
pinned: true,
delegate: _StickyHeaderDelegate(
child: Container(
height: 48,
color: const Color(0xFF0E1211),
padding: const EdgeInsets.symmetric(horizontal: 20),
child: const Row(
children: [
Icon(Icons.favorite,
color: Color(0xFFB8D4D0), size: 20),
SizedBox(width: 8),
Text(
'我的收藏',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w500,
color: Color(0xFFB8D4D0),
child: SafeArea(
bottom: false,
child: Container(
height: 48,
color: const Color(0xFF0E1211),
padding: const EdgeInsets.symmetric(horizontal: 20),
child: const Row(
children: [
Icon(Icons.favorite,
color: Color(0xFFB8D4D0), size: 20),
SizedBox(width: 8),
Text(
'我的收藏',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w500,
color: Color(0xFFB8D4D0),
),
),
),
],
],
),
),
),
),
),
// ---------- 收藏列表 ----------
// ---- 收藏列表 ----
SliverPadding(
padding: EdgeInsets.only(
left: 20,
@@ -505,7 +505,7 @@ class _HomePageState extends State<HomePage> {
),
const SizedBox(height: 16),
Text(
_isWebDAVConnected
isConnected
? '还没有收藏歌曲\n去媒体库发现音乐'
: '请先连接 WebDAV',
textAlign: TextAlign.center,
@@ -563,7 +563,7 @@ class _HomePageState extends State<HomePage> {
],
),
// ---------- 底部 MiniPlayer ----------
// ---- 底部 MiniPlayer ----
if (showMiniBar)
const Positioned(
left: 0,