界面部分阶段性达成效果
This commit is contained in:
@@ -1,46 +1,52 @@
|
||||
// ============================================================
|
||||
// 文件名: webdav_file_list_page.dart
|
||||
// 功能: WebDAV 文件浏览页面,支持文件夹导航和音乐播放
|
||||
// 调用方式: Navigator.push(context, MaterialPageRoute(builder: (_) => WebDAVFileListPage()))
|
||||
// ============================================================
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../services/webdav_service.dart';
|
||||
import '../services/playback_service.dart';
|
||||
import '../services/audio_service.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class WebDAVFileListPage extends StatefulWidget {
|
||||
const WebDAVFileListPage({super.key, this.currentPath = '/'});
|
||||
|
||||
/// 当前浏览路径,默认为根目录 '/'
|
||||
final String currentPath;
|
||||
|
||||
const WebDAVFileListPage({super.key, this.currentPath = '/'});
|
||||
|
||||
@override
|
||||
State<WebDAVFileListPage> createState() => _WebDAVFileListPageState();
|
||||
}
|
||||
|
||||
class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
||||
List<WebDAVFileItem> _files = [];
|
||||
List<WebDAVFileItem> _directories = [];
|
||||
List<WebDAVItem> _items = [];
|
||||
bool _isLoading = true;
|
||||
String _errorMessage = '';
|
||||
String _currentPath = '/';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadFiles();
|
||||
_currentPath = widget.currentPath;
|
||||
_loadDirectory();
|
||||
}
|
||||
|
||||
Future<void> _loadFiles() async {
|
||||
// -------------------------------------------------------------
|
||||
// 加载当前目录内容
|
||||
// -------------------------------------------------------------
|
||||
Future<void> _loadDirectory() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_errorMessage = '';
|
||||
});
|
||||
|
||||
try {
|
||||
final allItems =
|
||||
await WebDAVService.instance.getMusicFiles(path: widget.currentPath);
|
||||
|
||||
// 分离目录和文件(这里 getMusicFiles 只返回音乐文件,但如果有目录逻辑需要扩展)
|
||||
// 由于 getMusicFiles 只返回音乐文件,我们需要获取目录列表
|
||||
// 这里先简化:只显示音乐文件
|
||||
final items =
|
||||
await WebDAVService.instance.listDirectory(path: _currentPath);
|
||||
setState(() {
|
||||
_files = allItems;
|
||||
_directories = [];
|
||||
_items = items;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -51,12 +57,26 @@ class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
||||
}
|
||||
}
|
||||
|
||||
void _playSong(WebDAVFileItem file) async {
|
||||
// -------------------------------------------------------------
|
||||
// 进入子目录
|
||||
// -------------------------------------------------------------
|
||||
void _enterDirectory(WebDAVItem dir) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => WebDAVFileListPage(currentPath: dir.path),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 播放音乐文件
|
||||
// -------------------------------------------------------------
|
||||
void _playSong(WebDAVItem file) async {
|
||||
try {
|
||||
final url = WebDAVService.instance.getFileUrl(file.path);
|
||||
await PlaybackService().play(url);
|
||||
|
||||
// 更新 AudioService 状态
|
||||
final song = Song(
|
||||
id: file.path,
|
||||
title: file.name.replaceAll(RegExp(r'\.[^.]*$'), ''),
|
||||
@@ -65,15 +85,7 @@ class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
||||
);
|
||||
context.read<AudioService>().playSong(song);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('正在播放: ${file.name}'),
|
||||
backgroundColor: const Color(0xFF4CAF50),
|
||||
duration: const Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
}
|
||||
// ✅ 移除 SnackBar,直接显示 MiniPlayer
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -86,12 +98,25 @@ class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 构建面包屑路径显示(只显示最后两级,避免过长)
|
||||
// -------------------------------------------------------------
|
||||
String _getDisplayPath() {
|
||||
if (_currentPath == '/') return '根目录';
|
||||
final parts = _currentPath.split('/').where((s) => s.isNotEmpty).toList();
|
||||
if (parts.length <= 2) return parts.join(' / ');
|
||||
return '... / ${parts.sublist(parts.length - 2).join(' / ')}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFF0E1211),
|
||||
appBar: AppBar(
|
||||
title: const Text('音乐库'),
|
||||
title: Text(
|
||||
_getDisplayPath(),
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
foregroundColor: Colors.white,
|
||||
@@ -102,104 +127,155 @@ class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _loadFiles,
|
||||
onPressed: _loadDirectory,
|
||||
tooltip: '刷新',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: Color(0xFFB8D4D0),
|
||||
),
|
||||
)
|
||||
: _errorMessage.isNotEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error_outline,
|
||||
size: 48, color: Colors.grey[600]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_errorMessage,
|
||||
style: TextStyle(color: Colors.grey[400]),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _loadFiles,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFFB8D4D0),
|
||||
foregroundColor: Colors.black87,
|
||||
),
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: _files.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.music_note,
|
||||
size: 48, color: Colors.grey[600]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'没有找到音乐文件',
|
||||
style: TextStyle(color: Colors.grey[400]),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'支持格式: MP3, FLAC, M4A, APE, WAV, OPUS',
|
||||
style: TextStyle(
|
||||
color: Colors.grey[600], fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 8),
|
||||
itemCount: _files.length,
|
||||
itemBuilder: (context, index) {
|
||||
final file = _files[index];
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 4, vertical: 2),
|
||||
leading: const Icon(
|
||||
Icons.audiotrack,
|
||||
color: Color(0xFFB8D4D0),
|
||||
size: 28,
|
||||
),
|
||||
title: Text(
|
||||
file.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w400,
|
||||
color: Colors.white,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
file.size != null
|
||||
? '${(file.size! / 1024 / 1024).toStringAsFixed(1)} MB'
|
||||
: '',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.play_arrow,
|
||||
color: Color(0xFFB8D4D0)),
|
||||
onPressed: () => _playSong(file),
|
||||
),
|
||||
onTap: () => _playSong(file),
|
||||
);
|
||||
},
|
||||
),
|
||||
body: _buildBody(),
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 构建主体内容
|
||||
// -------------------------------------------------------------
|
||||
Widget _buildBody() {
|
||||
if (_isLoading) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: Color(0xFFB8D4D0),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_errorMessage.isNotEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error_outline, size: 48, color: Colors.grey[600]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_errorMessage,
|
||||
style: TextStyle(color: Colors.grey[400]),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _loadDirectory,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFFB8D4D0),
|
||||
foregroundColor: Colors.black87,
|
||||
),
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_items.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.folder_open, size: 48, color: Colors.grey[600]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'此目录为空',
|
||||
style: TextStyle(color: Colors.grey[400]),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'支持格式: MP3, FLAC, M4A, APE, WAV, OPUS',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
itemCount: _items.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = _items[index];
|
||||
return _buildListItem(item);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 构建单个列表项(区分目录和文件)
|
||||
// -------------------------------------------------------------
|
||||
Widget _buildListItem(WebDAVItem item) {
|
||||
if (item.isDirectory) {
|
||||
// ---------- 目录项 ----------
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
leading: const Icon(
|
||||
Icons.folder_outlined,
|
||||
color: Color(0xFFB8D4D0),
|
||||
size: 32,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.white,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
'文件夹',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
trailing: const Icon(
|
||||
Icons.chevron_right,
|
||||
color: Colors.grey,
|
||||
),
|
||||
onTap: () => _enterDirectory(item),
|
||||
);
|
||||
} else {
|
||||
// ---------- 音乐文件项 ----------
|
||||
final sizeStr = item.size != null
|
||||
? '${(item.size! / 1024 / 1024).toStringAsFixed(1)} MB'
|
||||
: '';
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
leading: const Icon(
|
||||
Icons.audiotrack,
|
||||
color: Color(0xFFB8D4D0),
|
||||
size: 28,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w400,
|
||||
color: Colors.white,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
sizeStr,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.play_arrow, color: Color(0xFFB8D4D0)),
|
||||
onPressed: () => _playSong(item),
|
||||
),
|
||||
onTap: () => _playSong(item),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user