v2.0.0: 模组插件管理、下载引擎(镜像+队列)、SSH终端、UI改版与依赖仓库
This commit is contained in:
1 parent
b8f163aa3a
commit
33d137e33c
29 files changed
+3061
-30
No files matched your search
@@ -0,0 +1,396 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../core/services/modrinth_api.dart';
|
||||
import '../../providers/download_provider.dart';
|
||||
import '../../core/services/download_manager.dart';
|
||||
|
||||
/// 下载中心:Modrinth 搜索 + 镜像线路选择 + 下载任务队列(M2)。
|
||||
class DownloadPage extends StatefulWidget {
|
||||
const DownloadPage({super.key});
|
||||
|
||||
@override
|
||||
State<DownloadPage> createState() => _DownloadPageState();
|
||||
}
|
||||
|
||||
class _DownloadPageState extends State<DownloadPage> {
|
||||
final _searchCtrl = TextEditingController();
|
||||
List<ModrinthSearchResult>? _results;
|
||||
bool _searching = false;
|
||||
String _projectType = 'mod';
|
||||
String? _mcVersion;
|
||||
String? _loader;
|
||||
|
||||
static const _mcVersions = ['1.21.4', '1.21.1', '1.20.6', '1.20.4', '1.20.1', '1.19.4', '1.18.2', '1.16.5'];
|
||||
static const _loaders = ['fabric', 'forge', 'quilt', 'neoforge', 'paper', 'bukkit', 'spigot'];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<DownloadProvider>().init();
|
||||
}
|
||||
|
||||
Future<void> _search() async {
|
||||
final query = _searchCtrl.text.trim();
|
||||
if (query.isEmpty) return;
|
||||
setState(() {
|
||||
_searching = true;
|
||||
_results = null;
|
||||
});
|
||||
try {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
final results = await dp.search(
|
||||
query,
|
||||
projectType: _projectType,
|
||||
mcVersion: _mcVersion,
|
||||
loader: _loader,
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_results = results;
|
||||
_searching = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() => _searching = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('搜索失败:$e')));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
return DefaultTabController(
|
||||
length: 2,
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
appBar: AppBar(
|
||||
title: const Text('下载中心'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.speed),
|
||||
tooltip: '镜像线路:${dp.mirror.currentName}',
|
||||
onPressed: _showMirrorDialog,
|
||||
),
|
||||
],
|
||||
bottom: const TabBar(tabs: [Tab(text: '搜索下载'), Tab(text: '任务队列')]),
|
||||
),
|
||||
body: TabBarView(children: [_buildSearchTab(), _buildQueueTab(dp)]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSearchTab() {
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _searchCtrl,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '搜索 Modrinth 模组/插件,如 sodium',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
border: OutlineInputBorder(),
|
||||
isDense: true,
|
||||
),
|
||||
onSubmitted: (_) => _search(),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FilledButton(onPressed: _searching ? null : _search, child: const Text('搜索')),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
_dropdown<String>(
|
||||
value: _projectType,
|
||||
items: const {'mod': '模组', 'plugin': '插件'},
|
||||
onChanged: (v) => setState(() => _projectType = v!),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_dropdown<String>(
|
||||
value: _mcVersion,
|
||||
hint: 'MC 版本',
|
||||
items: {for (final v in _mcVersions) v: v},
|
||||
onChanged: (v) => setState(() => _mcVersion = v),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_dropdown<String>(
|
||||
value: _loader,
|
||||
hint: '加载器',
|
||||
items: {for (final v in _loaders) v: v},
|
||||
onChanged: (v) => setState(() => _loader = v),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _searching
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _results == null
|
||||
? const Center(child: Text('输入关键词开始搜索'))
|
||||
: _results!.isEmpty
|
||||
? const Center(child: Text('没有结果'))
|
||||
: ListView.builder(
|
||||
itemCount: _results!.length,
|
||||
itemBuilder: (context, i) {
|
||||
final r = _results![i];
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.extension),
|
||||
title: Text(r.title),
|
||||
subtitle: Text(r.description, maxLines: 2, overflow: TextOverflow.ellipsis),
|
||||
trailing: Text('${r.downloads}',
|
||||
style: Theme.of(context).textTheme.bodySmall),
|
||||
onTap: () => _showVersionSheet(r),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _dropdown<T>({
|
||||
required T? value,
|
||||
required Map<String, String> items,
|
||||
String? hint,
|
||||
required ValueChanged<T?> onChanged,
|
||||
}) {
|
||||
return Expanded(
|
||||
child: DropdownButtonFormField<T>(
|
||||
value: value,
|
||||
isDense: true,
|
||||
decoration: InputDecoration(hintText: hint, border: const OutlineInputBorder(), isDense: true),
|
||||
items: items.entries
|
||||
.map((e) => DropdownMenuItem<T>(value: e.key as T, child: Text(e.value, overflow: TextOverflow.ellipsis)))
|
||||
.toList(),
|
||||
onChanged: onChanged,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 版本选择底部弹层。
|
||||
Future<void> _showVersionSheet(ModrinthSearchResult r) async {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('正在获取版本列表...'), duration: Duration(seconds: 1)),
|
||||
);
|
||||
try {
|
||||
final versions = await dp.versions(r.projectId);
|
||||
if (!mounted) return;
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (_) => SafeArea(
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Text('选择 ${r.title} 的版本', style: Theme.of(context).textTheme.titleMedium),
|
||||
),
|
||||
...versions.take(15).map((v) => ListTile(
|
||||
title: Text(v.versionNumber),
|
||||
subtitle: Text('${v.loaders.join('/')} · ${v.gameVersions.take(3).join(', ')}'),
|
||||
trailing: const Icon(Icons.download),
|
||||
onTap: () => _downloadVersion(v),
|
||||
)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('获取版本失败:$e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _downloadVersion(ModrinthVersion v) async {
|
||||
Navigator.pop(context);
|
||||
final dp = context.read<DownloadProvider>();
|
||||
// 取主 jar 文件
|
||||
final files = v.files;
|
||||
if (files.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('该版本没有可用文件')));
|
||||
return;
|
||||
}
|
||||
final primary = files.firstWhere((f) => f['primary'] == true, orElse: () => files.first);
|
||||
final url = primary['url'] as String;
|
||||
final fileName = primary['filename'] as String;
|
||||
await dp.download(url: url, fileName: fileName, projectType: _projectType);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('已加入下载队列:$fileName')));
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildQueueTab(DownloadProvider dp) {
|
||||
return AnimatedBuilder(
|
||||
animation: dp.manager,
|
||||
builder: (context, _) {
|
||||
final tasks = dp.manager.tasks;
|
||||
if (tasks.isEmpty) return const Center(child: Text('暂无下载任务'));
|
||||
return ListView.builder(
|
||||
itemCount: tasks.length,
|
||||
itemBuilder: (context, i) {
|
||||
final t = tasks[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: Text(t.fileName, overflow: TextOverflow.ellipsis)),
|
||||
_statusChip(t.status),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
LinearProgressIndicator(value: t.progress),
|
||||
const SizedBox(height: 6),
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'${_fmt(t.receivedBytes)} / ${_fmt(t.totalBytes)}'
|
||||
'${t.status == DownloadStatus.running ? " · ${_fmt(t.speed)}/s" : ""}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const Spacer(),
|
||||
if (t.status == DownloadStatus.running)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.pause),
|
||||
tooltip: '暂停',
|
||||
onPressed: () => dp.manager.pause(t.id),
|
||||
),
|
||||
if (t.status == DownloadStatus.paused)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
tooltip: '继续',
|
||||
onPressed: () => dp.manager.resume(t.id),
|
||||
),
|
||||
if (t.status == DownloadStatus.failed)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.replay),
|
||||
tooltip: '重试',
|
||||
onPressed: () => dp.manager.retry(t.id),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: '取消',
|
||||
onPressed: () => dp.manager.cancel(t.id),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (t.error != null)
|
||||
Text(t.error!, style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 12)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _statusChip(DownloadStatus s) {
|
||||
final (label, color) = switch (s) {
|
||||
DownloadStatus.queued => ('排队中', Colors.grey),
|
||||
DownloadStatus.running => ('下载中', Colors.blue),
|
||||
DownloadStatus.paused => ('已暂停', Colors.orange),
|
||||
DownloadStatus.completed => ('已完成', Colors.green),
|
||||
DownloadStatus.failed => ('失败', Colors.red),
|
||||
DownloadStatus.canceled => ('已取消', Colors.grey),
|
||||
};
|
||||
return Chip(label: Text(label, style: const TextStyle(fontSize: 12)), backgroundColor: color.withOpacity(0.15), visualDensity: VisualDensity.compact);
|
||||
}
|
||||
|
||||
String _fmt(num bytes) {
|
||||
if (bytes < 1024) return '$bytes B';
|
||||
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
|
||||
return '${(bytes / 1024 / 1024).toStringAsFixed(1)} MB';
|
||||
}
|
||||
|
||||
Future<void> _showMirrorDialog() async {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
final mirror = dp.mirror;
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (_) => StatefulBuilder(
|
||||
builder: (context, setDialogState) => AlertDialog(
|
||||
title: const Text('镜像线路'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
RadioListTile<String>(
|
||||
title: const Text('Modrinth 官方'),
|
||||
value: 'official',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
title: const Text('MCIM 国内镜像'),
|
||||
value: 'mcim',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
title: const Text('自定义镜像'),
|
||||
value: 'custom',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
final ctrl = TextEditingController(text: mirror.customUrl);
|
||||
final url = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('自定义镜像地址'),
|
||||
content: TextField(controller: ctrl, decoration: const InputDecoration(hintText: 'https://your-mirror.example.com')),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
||||
FilledButton(onPressed: () => Navigator.pop(context, ctrl.text.trim()), child: const Text('确定')),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (url != null && url.isNotEmpty) {
|
||||
await mirror.select('custom', customUrl: url);
|
||||
}
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
OutlinedButton.icon(
|
||||
icon: const Icon(Icons.bolt),
|
||||
label: const Text('一键测速并选择最快线路'),
|
||||
onPressed: () async {
|
||||
await mirror.autoSelect();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('已选择最快线路:${mirror.currentName}(${mirror.lastSpeedMs}ms)')),
|
||||
);
|
||||
}
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
FilledButton(onPressed: () => Navigator.pop(context), child: const Text('完成')),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user