206 lines
6.9 KiB
Dart
206 lines
6.9 KiB
Dart
import 'package:flutter/material.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;
|
|
|
|
@override
|
|
State<WebDAVFileListPage> createState() => _WebDAVFileListPageState();
|
|
}
|
|
|
|
class _WebDAVFileListPageState extends State<WebDAVFileListPage> {
|
|
List<WebDAVFileItem> _files = [];
|
|
List<WebDAVFileItem> _directories = [];
|
|
bool _isLoading = true;
|
|
String _errorMessage = '';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadFiles();
|
|
}
|
|
|
|
Future<void> _loadFiles() async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
_errorMessage = '';
|
|
});
|
|
|
|
try {
|
|
final allItems =
|
|
await WebDAVService.instance.getMusicFiles(path: widget.currentPath);
|
|
|
|
// 分离目录和文件(这里 getMusicFiles 只返回音乐文件,但如果有目录逻辑需要扩展)
|
|
// 由于 getMusicFiles 只返回音乐文件,我们需要获取目录列表
|
|
// 这里先简化:只显示音乐文件
|
|
setState(() {
|
|
_files = allItems;
|
|
_directories = [];
|
|
_isLoading = false;
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_errorMessage = '加载失败: $e';
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
void _playSong(WebDAVFileItem 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'\.[^.]*$'), ''),
|
|
artist: '未知艺术家',
|
|
url: url,
|
|
);
|
|
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),
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('播放失败: $e'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF0E1211),
|
|
appBar: AppBar(
|
|
title: const Text('音乐库'),
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0,
|
|
foregroundColor: Colors.white,
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back_ios_new),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh),
|
|
onPressed: _loadFiles,
|
|
),
|
|
],
|
|
),
|
|
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),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|