generated from dellevin/template
优化项目结构
This commit is contained in:
130
lib/pages/profile/app_icon_picker_page.dart
Normal file
130
lib/pages/profile/app_icon_picker_page.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../utils/user_prefs.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/app_icon_channel.dart';
|
||||
|
||||
/// 应用图标选择页面
|
||||
class AppIconPickerPage extends StatefulWidget {
|
||||
const AppIconPickerPage({super.key});
|
||||
|
||||
@override
|
||||
State<AppIconPickerPage> createState() => _AppIconPickerPageState();
|
||||
}
|
||||
|
||||
class _AppIconPickerPageState extends State<AppIconPickerPage> {
|
||||
final UserPrefs _userPrefs = UserPrefs();
|
||||
String _currentIconName = 'app_icon';
|
||||
|
||||
final List<Map<String, String>> _icons = [
|
||||
{'name': 'app_icon', 'label': '默认图标'},
|
||||
{'name': 'app_icon2', 'label': '风格二'},
|
||||
{'name': 'app_icon_m', 'label': '风格三'},
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadCurrentIcon();
|
||||
}
|
||||
|
||||
Future<void> _loadCurrentIcon() async {
|
||||
final nativeIcon = await AppIconChannel.getCurrentIcon();
|
||||
setState(() => _currentIconName = nativeIcon);
|
||||
}
|
||||
|
||||
Future<void> _selectIcon(String iconName) async {
|
||||
if (iconName == _currentIconName) return;
|
||||
try {
|
||||
final success = await AppIconChannel.switchIcon(iconName);
|
||||
if (success) {
|
||||
await _userPrefs.setAppIconName(iconName);
|
||||
setState(() => _currentIconName = iconName);
|
||||
if (mounted) ToastUtil.show(context, '图标已切换,请返回桌面查看');
|
||||
} else {
|
||||
if (mounted) ToastUtil.show(context, '图标切换失败');
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) ToastUtil.show(context, '切换出错: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surfaceContainerHigh,
|
||||
appBar: AppBar(
|
||||
title: const Text('应用图标'),
|
||||
),
|
||||
body: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: _icons.length,
|
||||
itemBuilder: (context, index) {
|
||||
final icon = _icons[index];
|
||||
final isSelected = _currentIconName == icon['name'];
|
||||
|
||||
return InkWell(
|
||||
onTap: () => _selectIcon(icon['name']!),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: isSelected ? colors.primary : Colors.transparent,
|
||||
width: isSelected ? 2 : 0,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border:
|
||||
Border.all(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Image.asset(
|
||||
'assets/icon/${icon['name']}${icon['name'] == 'app_icon_m' ? '.png' : '.webp'}',
|
||||
width: 40,
|
||||
height: 40,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
color: colors.surfaceContainerHighest,
|
||||
child: Icon(Icons.image_not_supported,
|
||||
size: 18,
|
||||
color: colors.onSurface.withValues(alpha: 0.3)),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
icon['label']!,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: colors.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isSelected)
|
||||
Icon(Icons.check_circle, size: 20, color: colors.primary)
|
||||
else
|
||||
Icon(Icons.radio_button_unchecked,
|
||||
size: 20,
|
||||
color: colors.onSurface.withValues(alpha: 0.2)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
325
lib/pages/profile/changelog_page.dart
Normal file
325
lib/pages/profile/changelog_page.dart
Normal file
@@ -0,0 +1,325 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../utils/changelog_service.dart';
|
||||
|
||||
/// 更新日志页面
|
||||
class ChangelogPage extends StatefulWidget {
|
||||
const ChangelogPage({super.key});
|
||||
|
||||
@override
|
||||
State<ChangelogPage> createState() => _ChangelogPageState();
|
||||
}
|
||||
|
||||
class _ChangelogPageState extends State<ChangelogPage> {
|
||||
List<ChangelogItem>? _items;
|
||||
bool _loading = true;
|
||||
bool _checking = false;
|
||||
static const _websiteUrl = 'https://mooknote.iletter.top/#/';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final items = await ChangelogService.fetchChangelog();
|
||||
if (mounted) setState(() { _items = items; _loading = false; });
|
||||
}
|
||||
|
||||
Future<void> _checkUpdate() async {
|
||||
setState(() => _checking = true);
|
||||
try {
|
||||
final hasUpdate = await ChangelogService.hasUpdate();
|
||||
if (!mounted) return;
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
final localVersion = 'v${info.version}';
|
||||
if (!mounted) return;
|
||||
if (hasUpdate) {
|
||||
final latest = _items != null && _items!.isNotEmpty
|
||||
? _items!.first.version
|
||||
: '新版本';
|
||||
final latestVersion = await ChangelogService.fetchLatestVersion();
|
||||
_showUpdateDialog(
|
||||
version: latestVersion ?? latest,
|
||||
localVersion: localVersion,
|
||||
);
|
||||
} else {
|
||||
_showNoUpdateDialog(localVersion);
|
||||
}
|
||||
} catch (_) {}
|
||||
if (mounted) setState(() => _checking = false);
|
||||
}
|
||||
|
||||
void _showNoUpdateDialog(String localVersion) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text('检查更新', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
content: Text('已是最新版本(当前 $localVersion)',
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: Text('好的', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
),
|
||||
],
|
||||
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showUpdateDialog({required String version, String? localVersion}) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text('发现新版本', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (localVersion != null) ...[
|
||||
Text('当前版本:$localVersion',
|
||||
style: TextStyle(fontSize: 12, color: colors.onSurface.withValues(alpha: 0.35))),
|
||||
const SizedBox(height: 6),
|
||||
],
|
||||
Text('最新版本 $version 已发布,是否下载更新?',
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: Text('稍后再说', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.6))),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(ctx);
|
||||
try {
|
||||
await launchUrl(Uri.parse(_websiteUrl), mode: LaunchMode.externalApplication);
|
||||
} catch (_) {}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: colors.primary, foregroundColor: colors.onPrimary, elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
child: const Text('去官网下载'),
|
||||
),
|
||||
],
|
||||
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('更新日志'),
|
||||
actions: [
|
||||
_checking
|
||||
? const Padding(
|
||||
padding: EdgeInsets.only(right: 16),
|
||||
child: SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2)))
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.refresh, size: 20),
|
||||
tooltip: '检查更新',
|
||||
onPressed: _checkUpdate,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _loading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _items == null || _items!.isEmpty
|
||||
? Center(
|
||||
child: Text('暂无更新日志',
|
||||
style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4))))
|
||||
: ListView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
children: [
|
||||
_buildWebsiteCard(colors),
|
||||
const SizedBox(height: 20),
|
||||
..._items!.map((item) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16),
|
||||
child: _buildCard(item, colors),
|
||||
)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWebsiteCard(ColorScheme colors) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(18, 16, 18, 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.language, size: 18, color: colors.primary),
|
||||
const SizedBox(width: 8),
|
||||
Text('官方网站',
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18),
|
||||
child: GestureDetector(
|
||||
onTap: () async {
|
||||
try {
|
||||
await launchUrl(Uri.parse(_websiteUrl), mode: LaunchMode.externalApplication);
|
||||
} catch (_) {}
|
||||
},
|
||||
child: Text(_websiteUrl,
|
||||
style: TextStyle(fontSize: 13, color: colors.primary)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
Divider(height: 1, color: colors.outlineVariant),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Clipboard.setData(const ClipboardData(text: _websiteUrl));
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text('已复制到剪贴板'),
|
||||
duration: Duration(seconds: 1),
|
||||
));
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
alignment: Alignment.center,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.copy, size: 16, color: colors.onSurface.withValues(alpha: 0.5)),
|
||||
const SizedBox(width: 6),
|
||||
Text('复制链接',
|
||||
style: TextStyle(fontSize: 13, color: colors.onSurface.withValues(alpha: 0.5))),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Container(width: 1, height: 24, color: colors.outlineVariant),
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
try {
|
||||
await launchUrl(Uri.parse(_websiteUrl), mode: LaunchMode.externalApplication);
|
||||
} catch (_) {}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
alignment: Alignment.center,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.open_in_browser, size: 16, color: colors.primary),
|
||||
const SizedBox(width: 6),
|
||||
Text('浏览器打开',
|
||||
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: colors.primary)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCard(ChangelogItem item, ColorScheme colors) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(18),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
item.version,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onPrimary,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
item.date,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: colors.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
...item.features.map((f) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 6),
|
||||
width: 6,
|
||||
height: 6,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary.withValues(alpha: 0.4),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
f,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface.withValues(alpha: 0.75),
|
||||
height: 1.5,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
402
lib/pages/profile/font_picker_page.dart
Normal file
402
lib/pages/profile/font_picker_page.dart
Normal file
@@ -0,0 +1,402 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import '../utils/font_download_manager.dart';
|
||||
import '../utils/toast_util.dart';
|
||||
import '../utils/user_prefs.dart';
|
||||
|
||||
/// 字体选择页面
|
||||
///
|
||||
/// 用户选择字体目录,遍历目录下的字体文件,点击即可加载使用。
|
||||
class FontPickerPage extends StatefulWidget {
|
||||
final String? initialFamily;
|
||||
const FontPickerPage({super.key, this.initialFamily});
|
||||
|
||||
@override
|
||||
State<FontPickerPage> createState() => _FontPickerPageState();
|
||||
}
|
||||
|
||||
class _FontPickerPageState extends State<FontPickerPage> {
|
||||
final FontDownloadManager _fontManager = FontDownloadManager();
|
||||
|
||||
List<FontFileInfo> _fonts = [];
|
||||
String? _loadingPath;
|
||||
String? _selectedFamily;
|
||||
bool _isScanning = false;
|
||||
String? _currentDirPath;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedFamily = widget.initialFamily;
|
||||
// 恢复上次选择的目录
|
||||
final savedDir = UserPrefs().lastFontDir;
|
||||
if (savedDir != null && savedDir.isNotEmpty) {
|
||||
_currentDirPath = savedDir;
|
||||
_scanDirectory(savedDir);
|
||||
}
|
||||
}
|
||||
|
||||
/// 请求存储权限(Android 11+ 需要 MANAGE_EXTERNAL_STORAGE)
|
||||
Future<bool> _requestStoragePermission() async {
|
||||
if (!Platform.isAndroid) return true;
|
||||
|
||||
var status = await Permission.manageExternalStorage.status;
|
||||
if (status.isGranted) return true;
|
||||
|
||||
status = await Permission.manageExternalStorage.request();
|
||||
if (status.isGranted) return true;
|
||||
|
||||
status = await Permission.storage.status;
|
||||
if (status.isGranted) return true;
|
||||
|
||||
status = await Permission.storage.request();
|
||||
return status.isGranted;
|
||||
}
|
||||
|
||||
/// 显示权限提示弹窗
|
||||
void _showPermissionDialog() {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text('需要存储权限',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: colors.onSurface)),
|
||||
content: Text(
|
||||
'Android 11+ 需要在系统设置中授予"所有文件访问权限"才能扫描字体文件。\n\n是否前往设置?',
|
||||
style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.6),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: Text('取消', style: TextStyle(color: colors.onSurface.withValues(alpha: 0.4))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(ctx);
|
||||
openAppSettings();
|
||||
},
|
||||
child: Text('前往设置', style: TextStyle(color: colors.primary)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 扫描目录字体
|
||||
Future<void> _scanDirectory(String dirPath) async {
|
||||
if (dirPath.isEmpty) return;
|
||||
|
||||
setState(() => _isScanning = true);
|
||||
try {
|
||||
final fonts = await _fontManager.scanFontDirectory(dirPath);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_fonts = fonts;
|
||||
_isScanning = false;
|
||||
});
|
||||
if (fonts.isEmpty) {
|
||||
ToastUtil.show(context, '未找到字体文件');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() => _isScanning = false);
|
||||
ToastUtil.show(context, '扫描失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 扫描当前目录字体
|
||||
Future<void> _rescanDirectory() async {
|
||||
if (_currentDirPath == null || _currentDirPath!.isEmpty) {
|
||||
ToastUtil.show(context, '请先选择字体目录');
|
||||
return;
|
||||
}
|
||||
await _scanDirectory(_currentDirPath!);
|
||||
}
|
||||
|
||||
/// 点击选择目录按钮
|
||||
Future<void> _onSelectDirectory() async {
|
||||
final hasPermission = await _requestStoragePermission();
|
||||
if (!hasPermission) {
|
||||
if (mounted) _showPermissionDialog();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final result = await FilePicker.platform.getDirectoryPath();
|
||||
if (result != null && result.isNotEmpty) {
|
||||
_currentDirPath = result;
|
||||
// 保存到 SharedPreferences
|
||||
await UserPrefs().setLastFontDir(result);
|
||||
await _scanDirectory(result);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) ToastUtil.show(context, '选择目录失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载并应用字体
|
||||
Future<void> _loadFont(FontFileInfo font) async {
|
||||
if (_loadingPath != null) return; // 防止重复点击
|
||||
|
||||
setState(() => _loadingPath = font.path);
|
||||
try {
|
||||
final family = await _fontManager.loadFontFile(font.path);
|
||||
if (family != null) {
|
||||
setState(() => _selectedFamily = family);
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '已应用: ${font.displayName}');
|
||||
Navigator.pop(context, family);
|
||||
}
|
||||
} else {
|
||||
if (mounted) ToastUtil.show(context, '字体加载失败');
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) ToastUtil.show(context, '加载失败: $e');
|
||||
} finally {
|
||||
setState(() => _loadingPath = null);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('选择字体'),
|
||||
actions: [
|
||||
// 默认字体按钮
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, ''),
|
||||
child: Text(
|
||||
'恢复默认',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: colors.primary,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
// 选择目录按钮区
|
||||
Container(
|
||||
padding: const EdgeInsets.fromLTRB(20, 16, 20, 12),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: colors.outlineVariant, width: 0.5),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'字体目录',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: _onSelectDirectory,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.folder_open_outlined,
|
||||
size: 18,
|
||||
color: colors.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_currentDirPath ?? '点击选择字体目录',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: _currentDirPath != null
|
||||
? colors.onSurface
|
||||
: colors.onSurface.withValues(alpha: 0.3),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
Icon(
|
||||
Icons.chevron_right,
|
||||
size: 18,
|
||||
color: colors.onSurface.withValues(alpha: 0.3),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _rescanDirectory,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: colors.primary,
|
||||
foregroundColor: colors.onPrimary,
|
||||
elevation: 0,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 10,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
child: const Text('扫描', style: TextStyle(fontSize: 13)),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 字体列表
|
||||
Expanded(
|
||||
child: _isScanning
|
||||
? Center(
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: colors.primary,
|
||||
),
|
||||
)
|
||||
: _fonts.isEmpty
|
||||
? _buildEmptyState(colors)
|
||||
: ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: _fonts.length,
|
||||
separatorBuilder: (_, __) => Divider(
|
||||
height: 0.5,
|
||||
indent: 20,
|
||||
endIndent: 20,
|
||||
color: colors.outlineVariant,
|
||||
),
|
||||
itemBuilder: (_, index) => _buildFontItem(
|
||||
_fonts[index],
|
||||
colors,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState(ColorScheme colors) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.folder_open_outlined,
|
||||
size: 48,
|
||||
color: colors.onSurface.withValues(alpha: 0.15),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'未找到字体文件',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: colors.onSurface.withValues(alpha: 0.4),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'支持 .ttf / .otf / .ttc 格式',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface.withValues(alpha: 0.25),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFontItem(FontFileInfo font, ColorScheme colors) {
|
||||
final isLoading = _loadingPath == font.path;
|
||||
final isSelected = _selectedFamily != null &&
|
||||
path.basenameWithoutExtension(font.fileName) == _selectedFamily;
|
||||
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
||||
leading: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? colors.primary.withValues(alpha: 0.1)
|
||||
: colors.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: isSelected
|
||||
? Border.all(color: colors.primary.withValues(alpha: 0.3), width: 1)
|
||||
: null,
|
||||
),
|
||||
child: isLoading
|
||||
? Center(
|
||||
child: SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: colors.primary,
|
||||
),
|
||||
),
|
||||
)
|
||||
: Icon(
|
||||
Icons.font_download_outlined,
|
||||
size: 18,
|
||||
color: isSelected
|
||||
? colors.primary
|
||||
: colors.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
font.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.w400,
|
||||
color: colors.onSurface,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
font.fileName,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: colors.onSurface.withValues(alpha: 0.35),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: isSelected
|
||||
? Icon(Icons.check_circle, size: 18, color: colors.primary)
|
||||
: null,
|
||||
onTap: isLoading ? null : () => _loadFont(font),
|
||||
);
|
||||
}
|
||||
}
|
||||
3190
lib/pages/profile/profile_page.dart
Normal file
3190
lib/pages/profile/profile_page.dart
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user