Files
ZV-Manager/lib/features/settings/download_settings_page.dart
T

154 lines
5.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../providers/download_provider.dart';
import '../../core/l10n/strings.dart';
/// 下载与镜像设置页:镜像线路选择、测速自动选线、并发数(原下载页内设置移入此处)。
class DownloadSettingsPage extends StatefulWidget {
const DownloadSettingsPage({super.key});
@override
State<DownloadSettingsPage> createState() => _DownloadSettingsPageState();
}
class _DownloadSettingsPageState extends State<DownloadSettingsPage> {
bool _testing = false;
@override
void initState() {
super.initState();
context.read<DownloadProvider>().init();
}
@override
Widget build(BuildContext context) {
final dp = context.watch<DownloadProvider>();
final mirror = dp.mirror;
return Scaffold(
appBar: AppBar(title: T('下载与镜像设置')),
body: ListView(
children: [
const SectionHeader('镜像线路'),
RadioListTile<String>(
title: T('MCIM 国内镜像(推荐)'),
subtitle: T('modrinth.mcimirror.top,无需科学上网'),
value: 'mcim',
groupValue: mirror.currentId,
onChanged: (v) async {
await mirror.select(v!);
setState(() {});
},
),
RadioListTile<String>(
title: T('Modrinth 官方'),
subtitle: T('直连官方源,适合海外网络'),
value: 'official',
groupValue: mirror.currentId,
onChanged: (v) async {
await mirror.select(v!);
setState(() {});
},
),
RadioListTile<String>(
title: T('自定义镜像'),
subtitle: Text(mirror.currentId == 'custom' && mirror.customUrl.isNotEmpty
? mirror.customUrl
: '使用自建/第三方镜像地址'),
value: 'custom',
groupValue: mirror.currentId,
onChanged: (v) async => {await _editCustomUrl(), setState(() {})},
),
ListTile(
leading: _testing
? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))
: const Icon(Icons.speed),
title: T('一键测速并选择最快线路'),
subtitle: Text(mirror.lastSpeedMs > 0
? S.f('上次测速:{0}ms({1})', [mirror.lastSpeedMs, mirror.currentName])
: '自动在官方与镜像之间选择延迟最低的线路'),
onTap: _testing
? null
: () async {
final messenger = ScaffoldMessenger.of(context);
setState(() => _testing = true);
await mirror.autoSelect();
if (!mounted) return;
setState(() => _testing = false);
messenger.showSnackBar(SnackBar(
content: Text(S.f('已选择最快线路:{0}({1}ms)', [mirror.currentName, mirror.lastSpeedMs])),
));
},
),
const Divider(),
const SectionHeader('下载队列'),
ListTile(
leading: const Icon(Icons.playlist_play),
title: T('同时下载任务数'),
subtitle: Text(S.f('当前:{0} 个(越大越占带宽,建议 1-3)', [dp.concurrency])),
trailing: DropdownButton<int>(
value: dp.concurrency,
underline: const SizedBox.shrink(),
items: const [1, 2, 3, 4, 5, 6]
.map((n) => DropdownMenuItem(value: n, child: Text('$n')))
.toList(),
onChanged: (n) async {
if (n == null) return;
await dp.setConcurrency(n);
setState(() {});
},
),
),
const Divider(),
const Padding(
padding: EdgeInsets.all(16),
child: Text(
'说明:下载完成的文件保存在应用目录 downloads/ 下;'
'若服务器配置了 SSH,会自动上传到对应服务器的 mods/plugins 目录。',
style: TextStyle(fontSize: 13),
),
),
],
),
);
}
Future<void> _editCustomUrl() async {
final dp = context.read<DownloadProvider>();
final ctrl = TextEditingController(text: dp.mirror.customUrl);
final url = await showDialog<String>(
context: context,
builder: (_) => AlertDialog(
title: T('自定义镜像地址'),
content: TextField(
controller: ctrl,
decoration: InputDecoration(hintText: 'https://your-mirror.example.com'),
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: T('取消')),
FilledButton(onPressed: () => Navigator.pop(context, ctrl.text.trim()), child: T('确定')),
],
),
);
if (url != null && url.isNotEmpty) {
await dp.mirror.select('custom', customUrl: url);
}
}
}
class SectionHeader extends StatelessWidget {
final String text;
const SectionHeader(this.text, {super.key});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 14, 16, 6),
child: Text(text,
style: Theme.of(context)
.textTheme
.titleSmall
?.copyWith(color: Theme.of(context).colorScheme.primary)),
);
}
}