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>
194 lines
7.2 KiB
Dart
194 lines
7.2 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:provider/provider.dart';
|
||
import 'package:image_picker/image_picker.dart';
|
||
import 'package:path_provider/path_provider.dart';
|
||
import 'dart:io';
|
||
import '../../providers/wallpaper_provider.dart';
|
||
import '../../core/l10n/strings.dart';
|
||
import '../../core/widgets/z_widgets.dart';
|
||
|
||
class WallpaperSettings extends StatelessWidget {
|
||
const WallpaperSettings({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final provider = context.watch<WallpaperProvider>();
|
||
final theme = Theme.of(context);
|
||
|
||
return Scaffold(
|
||
appBar: ZAppBar(
|
||
title: T('壁纸设置'),
|
||
backgroundColor: theme.colorScheme.primary,
|
||
foregroundColor: Colors.white,
|
||
systemOverlayStyle: const SystemUiOverlayStyle(
|
||
statusBarIconBrightness: Brightness.light,
|
||
statusBarBrightness: Brightness.dark,
|
||
),
|
||
),
|
||
body: ListView(
|
||
padding: const EdgeInsets.all(16),
|
||
children: [
|
||
// 预览区域
|
||
Container(
|
||
height: 200,
|
||
decoration: BoxDecoration(
|
||
borderRadius: BorderRadius.circular(16),
|
||
color: Colors.grey[200],
|
||
image: provider.wallpaperPath != null
|
||
? DecorationImage(
|
||
image: FileImage(File(provider.wallpaperPath!)),
|
||
fit: BoxFit.cover,
|
||
)
|
||
: null,
|
||
),
|
||
child: provider.wallpaperPath == null
|
||
? const Center(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Icon(Icons.wallpaper, size: 48, color: Colors.grey),
|
||
SizedBox(height: 8),
|
||
T('当前为默认壁纸', style: TextStyle(color: Colors.grey)),
|
||
T('点击下方「选择图片」自定义', style: TextStyle(color: Colors.grey, fontSize: 12)),
|
||
],
|
||
),
|
||
)
|
||
: const Center(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Icon(Icons.check_circle, size: 48, color: Colors.white),
|
||
SizedBox(height: 8),
|
||
T('自定义壁纸已应用', style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600)),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
const SizedBox(height: 24),
|
||
|
||
// 选择图片
|
||
T('选择壁纸', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
|
||
const SizedBox(height: 4),
|
||
T('从相册选择图片作为应用背景', style: TextStyle(fontSize: 12, color: Colors.grey)),
|
||
const SizedBox(height: 12),
|
||
FilledButton.icon(
|
||
onPressed: () async {
|
||
final picker = ImagePicker();
|
||
final image = await picker.pickImage(source: ImageSource.gallery);
|
||
if (image != null && context.mounted) {
|
||
final savedPath = await _saveFileToAssets(context, image.path);
|
||
if (savedPath != null && context.mounted) {
|
||
// Provider 变更会同步到 BackgroundWidget.spec,
|
||
// 背景层自己完成交叉淡入,这里不再需要手动刷新
|
||
await provider.setCustomWallpaper(savedPath);
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(content: T('壁纸已更新'), backgroundColor: Colors.green),
|
||
);
|
||
}
|
||
}
|
||
}
|
||
},
|
||
icon: const Icon(Icons.photo_library),
|
||
label: T('选择图片'),
|
||
style: FilledButton.styleFrom(
|
||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
|
||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||
),
|
||
),
|
||
const SizedBox(height: 24),
|
||
|
||
// 重置
|
||
T('重置壁纸', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
|
||
const SizedBox(height: 4),
|
||
T('恢复默认壁纸', style: TextStyle(fontSize: 12, color: Colors.grey)),
|
||
const SizedBox(height: 12),
|
||
OutlinedButton.icon(
|
||
onPressed: () {
|
||
_showResetDialog(context, provider);
|
||
},
|
||
icon: const Icon(Icons.restore),
|
||
label: T('恢复默认'),
|
||
style: OutlinedButton.styleFrom(
|
||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
|
||
side: const BorderSide(color: Colors.red),
|
||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Future<String?> _saveFileToAssets(BuildContext context, String sourcePath) async {
|
||
try {
|
||
final dir = await getApplicationDocumentsDirectory();
|
||
final ext = sourcePath.split('.').last;
|
||
final fileName = 'background_image_${DateTime.now().millisecondsSinceEpoch}.$ext';
|
||
final destPath = '${dir.path}/$fileName';
|
||
|
||
final file = File(sourcePath);
|
||
await file.copy(destPath);
|
||
|
||
// 清理旧壁纸,避免每次换图都在应用目录留一份副本
|
||
await _cleanupOldWallpapers(dir.path, keep: destPath);
|
||
|
||
return destPath;
|
||
} catch (e) {
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(content: T('保存失败,请重试')),
|
||
);
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// 删除应用目录下除 [keep] 之外的壁纸文件。
|
||
Future<void> _cleanupOldWallpapers(String dirPath, {required String keep}) async {
|
||
try {
|
||
final dir = Directory(dirPath);
|
||
if (!dir.existsSync()) return;
|
||
await for (final entity in dir.list()) {
|
||
if (entity is! File) continue;
|
||
final name = entity.path.split(Platform.pathSeparator).last;
|
||
if (!name.startsWith('background_image_')) continue;
|
||
if (entity.path == keep) continue;
|
||
await entity.delete();
|
||
}
|
||
} catch (_) {
|
||
// 清理失败不影响主流程
|
||
}
|
||
}
|
||
|
||
void _showResetDialog(BuildContext context, WallpaperProvider provider) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: T('确认重置'),
|
||
content: T('确定要恢复默认壁纸吗?'),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: T('取消'),
|
||
),
|
||
FilledButton(
|
||
onPressed: () async {
|
||
await provider.resetWallpaper();
|
||
if (context.mounted) {
|
||
Navigator.pop(context);
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(content: T('已恢复默认壁纸'), backgroundColor: Colors.green),
|
||
);
|
||
}
|
||
},
|
||
style: FilledButton.styleFrom(backgroundColor: Colors.red),
|
||
child: T('重置'),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|