api修改和更新检查器修改

modrinth_api.dart:拦截器改为只替换 options.baseUrl,path 保持相对 /v2/search、query 留在 queryParameters,避免"整串写回 path"导致的 host 缺失 + query 重复拼接。mirror_service.dart:新增 apiBase getter,返回当前镜像的 API base(official→api.modrinth.com、mcim→modrinth-api.mcimirror.top、custom→自定义 base)

Signed-off-by: driftinblock <3480428251@qq.com>
This commit is contained in:
Ventiの酒 committed 2026-11-04 17:43:57 +08:00
1 parent ddd72c43fb
commit e32125aa8e
35 files changed
+4171 -1325

No files matched your search

+339 -184
View File
@@ -6,6 +6,7 @@ import '../../providers/download_provider.dart';
import '../../core/services/download_manager.dart';
import '../settings/download_settings_page.dart';
import '../../core/l10n/strings.dart';
import '../../core/widgets/z_widgets.dart';
/// 下载中心:Modrinth 搜索 + 镜像线路选择 + 下载任务队列(M2)。
class DownloadPage extends StatefulWidget {
@@ -72,8 +73,9 @@ class _DownloadPageState extends State<DownloadPage> {
length: 2,
child: Scaffold(
backgroundColor: hasWallpaper ? Colors.transparent : null,
appBar: AppBar(
appBar: ZAppBar(
title: T('下载中心'),
backgroundColor: hasWallpaper ? Colors.transparent : null,
actions: [
IconButton(
icon: const Icon(Icons.settings_outlined),
@@ -96,33 +98,36 @@ class _DownloadPageState extends State<DownloadPage> {
);
}
/// 搜索页签:搜索框 + 版本/加载器筛选 + 结果列表。
/// 搜索页签:圆角搜索框 + 三枚筛选下拉 + 结果面板。
Widget _buildSearchTab() {
final hasWallpaper = context.watch<WallpaperProvider>().hasWallpaper;
final st = ZStyle.of(context, hasWallpaper: hasWallpaper);
return Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
Expanded(
child: TextField(
controller: _searchCtrl,
decoration: InputDecoration(
hintText: S.t('搜索 Modrinth 模组/插件,如 sodium'),
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(),
isDense: true,
padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
child: ZSearchField(
controller: _searchCtrl,
hint: '搜索 Modrinth 模组/插件,如 sodium',
onSubmitted: (_) => _search(),
trailing: _searching
? const Padding(
padding: EdgeInsets.only(right: 4),
child: SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
),
)
: IconButton(
icon: const Icon(Icons.arrow_forward_rounded, size: 20),
tooltip: S.t('搜索'),
onPressed: _search,
),
onSubmitted: (_) => _search(),
),
),
const SizedBox(width: 8),
FilledButton(onPressed: _searching ? null : _search, child: T('搜索')),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
padding: const EdgeInsets.fromLTRB(16, 0, 16, 10),
child: Row(
children: [
_dropdown<String>(
@@ -147,28 +152,77 @@ class _DownloadPageState extends State<DownloadPage> {
],
),
),
Expanded(
child: _searching
? const Center(child: CircularProgressIndicator())
: _results == null
? const Center(child: T('输入关键词开始搜索'))
: _results!.isEmpty
? const Center(child: T('没有结果'))
: 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),
);
},
),
Expanded(child: _buildResults(st)),
],
);
}
/// 搜索结果列表:整块圆角面板,结果行之间用细线分隔。
Widget _buildResults(ZStyle st) {
if (_searching) {
return const Center(child: CircularProgressIndicator());
}
if (_results == null) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.travel_explore_outlined, size: 40, color: st.subColor),
const SizedBox(height: 12),
Text(S.t('输入关键词开始搜索'),
style: TextStyle(fontSize: 13, color: st.subColor)),
],
),
);
}
if (_results!.isEmpty) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.search_off, size: 40, color: st.subColor),
const SizedBox(height: 12),
Text(S.t('没有结果'),
style: TextStyle(fontSize: 13, color: st.subColor)),
],
),
);
}
return ListView(
padding: const EdgeInsets.fromLTRB(16, 2, 16, 24),
children: [
Padding(
padding: const EdgeInsets.only(bottom: 10, left: 6),
child: Text(
S.f('共 {0} 个结果', [_results!.length]),
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w700,
color: st.subColor,
),
),
),
ZPanel(style: st, children: [
for (var i = 0; i < _results!.length; i++) ...[
if (i > 0) ZDivider(style: st),
ZRow(
style: st,
title: _results![i].title,
subtitle: _results![i].description,
value: '${_results![i].downloads}',
leading: Container(
width: 34,
height: 34,
decoration: BoxDecoration(
color: st.scheme.primary.withOpacity(0.14),
borderRadius: BorderRadius.circular(11),
),
child: Icon(Icons.extension, size: 18, color: st.scheme.primary),
),
onTap: () => _showVersionSheet(_results![i]),
),
],
]),
],
);
}
@@ -203,23 +257,37 @@ class _DownloadPageState extends State<DownloadPage> {
if (!mounted) return;
showModalBottomSheet(
context: context,
builder: (_) => SafeArea(
child: ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(S.f('选择 {0} 的版本', [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),
)),
],
),
),
builder: (ctx) {
final st = ZStyle.of(ctx, hasWallpaper: false);
final list = versions.take(15).toList();
return SafeArea(
child: ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(16),
children: [
Padding(
padding: const EdgeInsets.only(bottom: 12, left: 4),
child: Text(S.f('选择 {0} 的版本', [r.title]), style: Theme.of(context).textTheme.titleMedium),
),
ZPanel(
style: st,
children: [
for (int i = 0; i < list.length; i++) ...[
if (i > 0) ZDivider(style: st),
ZSheetRow(
icon: Icons.history,
title: list[i].versionNumber,
subtitle: '${list[i].loaders.join('/')} · ${list[i].gameVersions.take(3).join(', ')}',
trailing: Icon(Icons.download, color: kZAccent, size: 20),
onTap: () => _downloadVersion(list[i]),
),
],
],
),
],
),
);
},
);
} catch (e) {
if (mounted) {
@@ -246,164 +314,251 @@ class _DownloadPageState extends State<DownloadPage> {
}
}
/// 进度条视图:每个任务一条横向进度条 + 百分比。
Widget _buildBarList(DownloadProvider dp, List tasks) {
return ListView.builder(
padding: const EdgeInsets.symmetric(vertical: 8),
itemCount: tasks.length,
itemBuilder: (context, i) {
final t = tasks[i];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
/// 进度条视图:每个任务一条横向进度条 + 百分比,整块放进一个圆角面板。
Widget _buildBarList(DownloadProvider dp, List tasks, ZStyle st) {
return ListView(
padding: const EdgeInsets.fromLTRB(16, 10, 16, 24),
children: [
ZPanel(style: st, children: [
for (var i = 0; i < tasks.length; i++) ...[
if (i > 0) ZDivider(style: st),
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 12, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Text('${i + 1}. ${t.fileName}',
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodyMedium),
Row(
children: [
Expanded(
child: Text(
'${i + 1}. ${tasks[i].fileName}',
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 13.5,
fontWeight: FontWeight.w600,
color: st.titleColor,
),
),
),
const SizedBox(width: 8),
Text(
'${(tasks[i].progress * 100).toStringAsFixed(0)}%',
style: TextStyle(fontSize: 12, color: st.subColor),
),
const SizedBox(width: 6),
_statusBadge(tasks[i].status),
],
),
Text('${(t.progress * 100).toStringAsFixed(0)}%',
style: Theme.of(context).textTheme.bodySmall),
const SizedBox(width: 4),
_statusChip(t.status),
const SizedBox(height: 8),
ClipRRect(
borderRadius: BorderRadius.circular(4),
child: LinearProgressIndicator(
value: tasks[i].status == DownloadStatus.completed
? 1
: tasks[i].progress,
minHeight: 6,
backgroundColor: st.dividerColor,
),
),
const SizedBox(height: 6),
Row(
children: [
Expanded(
child: Text(
'${_fmt(tasks[i].receivedBytes)} / ${_fmt(tasks[i].totalBytes)}'
'${tasks[i].status == DownloadStatus.running ? " · ${_fmt(tasks[i].speed)}/s" : ""}',
style: TextStyle(fontSize: 11.5, color: st.subColor),
),
),
..._taskActions(dp, tasks[i], compact: true),
],
),
if (tasks[i].error != null)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
tasks[i].error!,
style: TextStyle(
fontSize: 11.5, color: st.scheme.error),
),
),
],
),
const SizedBox(height: 6),
LinearProgressIndicator(
value: t.status == DownloadStatus.completed ? 1 : t.progress,
minHeight: 8,
borderRadius: BorderRadius.circular(4),
),
const SizedBox(height: 4),
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, size: 20),
tooltip: S.t('暂停'),
onPressed: () => dp.manager.pause(t.id),
),
if (t.status == DownloadStatus.paused)
IconButton(
icon: const Icon(Icons.play_arrow, size: 20),
tooltip: S.t('继续'),
onPressed: () => dp.manager.resume(t.id),
),
if (t.status == DownloadStatus.failed)
IconButton(
icon: const Icon(Icons.replay, size: 20),
tooltip: S.t('重试'),
onPressed: () => dp.manager.retry(t.id),
),
IconButton(
icon: const Icon(Icons.close, size: 20),
tooltip: S.t('取消'),
onPressed: () => dp.manager.cancel(t.id),
),
],
),
if (t.error != null)
Text(t.error!,
style: TextStyle(
color: Theme.of(context).colorScheme.error, fontSize: 12)),
],
),
);
},
),
],
]),
],
);
}
Widget _buildQueueTab(DownloadProvider dp) {
return AnimatedBuilder(
animation: dp.manager,
builder: (context, _) {
final tasks = dp.manager.tasks;
if (tasks.isEmpty) return const Center(child: T('暂无下载任务'));
if (_barView) return _buildBarList(dp, tasks);
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),
/// 卡片视图:每个任务一张圆角卡片(信息更全,适合单手操作)。
Widget _buildCardList(DownloadProvider dp, List tasks, ZStyle st) {
return ListView(
padding: const EdgeInsets.fromLTRB(16, 10, 16, 24),
children: [
for (final t in tasks)
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: ZPanel(style: st, children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 13, 12, 13),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(child: Text(t.fileName, overflow: TextOverflow.ellipsis)),
_statusChip(t.status),
Expanded(
child: Text(
t.fileName,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
color: st.titleColor,
),
),
),
_statusBadge(t.status),
],
),
const SizedBox(height: 8),
LinearProgressIndicator(value: t.progress),
const SizedBox(height: 10),
ClipRRect(
borderRadius: BorderRadius.circular(4),
child: LinearProgressIndicator(
value: t.progress,
minHeight: 6,
backgroundColor: st.dividerColor,
),
),
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: S.t('暂停'),
onPressed: () => dp.manager.pause(t.id),
Expanded(
child: Text(
'${_fmt(t.receivedBytes)} / ${_fmt(t.totalBytes)}'
'${t.status == DownloadStatus.running ? " · ${_fmt(t.speed)}/s" : ""}',
style: TextStyle(fontSize: 11.5, color: st.subColor),
),
if (t.status == DownloadStatus.paused)
IconButton(
icon: const Icon(Icons.play_arrow),
tooltip: S.t('继续'),
onPressed: () => dp.manager.resume(t.id),
),
if (t.status == DownloadStatus.failed)
IconButton(
icon: const Icon(Icons.replay),
tooltip: S.t('重试'),
onPressed: () => dp.manager.retry(t.id),
),
IconButton(
icon: const Icon(Icons.close),
tooltip: S.t('取消'),
onPressed: () => dp.manager.cancel(t.id),
),
..._taskActions(dp, t, compact: false),
],
),
if (t.error != null)
Text(t.error!, style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 12)),
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(t.error!,
style: TextStyle(
fontSize: 11.5, color: st.scheme.error)),
),
],
),
),
);
},
]),
),
],
);
}
/// 任务操作按钮组:按状态决定显示暂停/继续/重试,取消始终可用。
List<Widget> _taskActions(DownloadProvider dp, dynamic t,
{required bool compact}) {
final size = compact ? 19.0 : 20.0;
final scheme = Theme.of(context).colorScheme;
return [
if (t.status == DownloadStatus.running)
IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(Icons.pause_rounded, size: size),
tooltip: S.t('暂停'),
onPressed: () => dp.manager.pause(t.id),
),
if (t.status == DownloadStatus.paused)
IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(Icons.play_arrow_rounded, size: size),
tooltip: S.t('继续'),
onPressed: () => dp.manager.resume(t.id),
),
if (t.status == DownloadStatus.failed)
IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(Icons.replay_rounded, size: size),
tooltip: S.t('重试'),
onPressed: () => dp.manager.retry(t.id),
),
IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(Icons.close_rounded, size: size, color: scheme.error),
tooltip: S.t('取消'),
onPressed: () => dp.manager.cancel(t.id),
),
];
}
Widget _buildQueueTab(DownloadProvider dp) {
final hasWallpaper = context.watch<WallpaperProvider>().hasWallpaper;
final st = ZStyle.of(context, hasWallpaper: hasWallpaper);
return AnimatedBuilder(
animation: dp.manager,
builder: (context, _) {
final tasks = dp.manager.tasks;
if (tasks.isEmpty) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.cloud_download_outlined,
size: 40, color: st.subColor),
const SizedBox(height: 12),
Text(S.t('暂无下载任务'),
style: TextStyle(fontSize: 13, color: st.subColor)),
const SizedBox(height: 6),
Text(S.t('在上方「搜索下载」页签中选择模组即可开始'),
style: TextStyle(fontSize: 12, color: st.subColor)),
],
),
);
}
// 顶部任务计数
return Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(22, 10, 22, 0),
child: Row(
children: [
Text(
S.f('共 {0} 个任务', [tasks.length]),
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w700,
color: st.subColor,
),
),
],
),
),
Expanded(
child: _barView
? _buildBarList(dp, tasks, st)
: _buildCardList(dp, tasks, st),
),
],
);
},
);
}
Widget _statusChip(DownloadStatus s) {
/// 任务状态胶囊(沿用 ZL2 的小圆角标签样式)。
Widget _statusBadge(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),
DownloadStatus.queued => ('排队中', const Color(0xFF90A4AE)),
DownloadStatus.running => ('下载中', const Color(0xFF42A5F5)),
DownloadStatus.paused => ('已暂停', const Color(0xFFFFA726)),
DownloadStatus.completed => ('已完成', kZAccent),
DownloadStatus.failed => ('失败', const Color(0xFFEF5350)),
DownloadStatus.canceled => ('已取消', const Color(0xFF90A4AE)),
};
return Chip(label: Text(label, style: const TextStyle(fontSize: 12)), backgroundColor: color.withOpacity(0.15), visualDensity: VisualDensity.compact);
return ZBadge(label: label, color: color);
}
String _fmt(num bytes) {