release 1.3.1
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../../../constants/app_constants.dart';
|
||||
|
||||
/// 时间: 2026-03-27
|
||||
@@ -17,6 +20,13 @@ class AppDataPage extends StatefulWidget {
|
||||
class _AppDataPageState extends State<AppDataPage> {
|
||||
int _sharedPrefsCount = 0;
|
||||
bool _isLoading = true;
|
||||
String _packageSize = '计算中...';
|
||||
String _cacheSize = '计算中...';
|
||||
String _dataSize = '计算中...';
|
||||
String _totalSize = '计算中...';
|
||||
int _packageBytes = 0;
|
||||
int _cacheBytes = 0;
|
||||
int _dataBytes = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -27,12 +37,137 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
Future<void> _loadDataInfo() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final keys = prefs.getKeys();
|
||||
|
||||
await Future.wait([
|
||||
_calculatePackageSize(),
|
||||
_calculateCacheSize(),
|
||||
_calculateDataSize(),
|
||||
]);
|
||||
|
||||
_calculateTotalSize();
|
||||
|
||||
setState(() {
|
||||
_sharedPrefsCount = keys.length;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<int> _getDirectorySize(Directory dir) async {
|
||||
int totalSize = 0;
|
||||
try {
|
||||
if (await dir.exists()) {
|
||||
await for (FileSystemEntity entity in dir.list(recursive: true)) {
|
||||
if (entity is File) {
|
||||
try {
|
||||
totalSize += await entity.length();
|
||||
} catch (e) {
|
||||
debugPrint('获取文件大小失败: ${entity.path}, $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('计算目录大小失败: ${dir.path}, $e');
|
||||
}
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
String _formatBytes(int bytes) {
|
||||
if (bytes <= 0) return '0 B';
|
||||
const suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
int i = (bytes.bitLength - 1) ~/ 10;
|
||||
double size = bytes / (1 << (i * 10));
|
||||
return '${size.toStringAsFixed(i == 0 ? 0 : 2)} ${suffixes[i]}';
|
||||
}
|
||||
|
||||
Future<void> _calculatePackageSize() async {
|
||||
try {
|
||||
int totalSize = 0;
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
final supportDir = await getApplicationSupportDirectory();
|
||||
|
||||
final parentDir = tempDir.parent;
|
||||
if (await parentDir.exists()) {
|
||||
await for (FileSystemEntity entity in parentDir.list()) {
|
||||
if (entity is Directory) {
|
||||
final dirName = entity.path.split(Platform.pathSeparator).last;
|
||||
if (!dirName.startsWith('.')) {
|
||||
totalSize += await _getDirectorySize(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_packageBytes = totalSize;
|
||||
_packageSize = _formatBytes(totalSize);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('计算软件包大小失败: $e');
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_packageSize = '获取失败';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _calculateCacheSize() async {
|
||||
try {
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final size = await _getDirectorySize(tempDir);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_cacheBytes = size;
|
||||
_cacheSize = _formatBytes(size);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('计算缓存大小失败: $e');
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_cacheSize = '获取失败';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _calculateDataSize() async {
|
||||
try {
|
||||
int totalSize = 0;
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
final supportDir = await getApplicationSupportDirectory();
|
||||
|
||||
totalSize += await _getDirectorySize(appDocDir);
|
||||
totalSize += await _getDirectorySize(supportDir);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_dataBytes = totalSize;
|
||||
_dataSize = _formatBytes(totalSize);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('计算数据大小失败: $e');
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_dataSize = '获取失败';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _calculateTotalSize() {
|
||||
const int appBaseSize = 10 * 1024 * 1024; // 10MB
|
||||
final totalBytes = _packageBytes + _cacheBytes + _dataBytes + appBaseSize;
|
||||
setState(() {
|
||||
_totalSize = _formatBytes(totalBytes);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _clearSharedPreferences() async {
|
||||
final confirmed = await _showConfirmDialog(
|
||||
'清空配置数据',
|
||||
@@ -140,6 +275,55 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _nativeClearData() async {
|
||||
final confirmed = await _showConfirmDialog(
|
||||
'系统层清理数据',
|
||||
'⚠️ 危险操作 ⚠️\n\n确定要系统层清理数据吗?\n\n这将删除:\n• 应用文档目录所有文件\n• 应用支持目录所有文件\n• 应用缓存目录所有文件\n\n此操作将直接删除文件系统数据,不可撤销!',
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
final doubleConfirmed = await _showConfirmDialog(
|
||||
'再次确认',
|
||||
'这是系统层面文件系统删除!\n\n删除后数据将无法恢复,确定继续吗?',
|
||||
);
|
||||
|
||||
if (doubleConfirmed != true) return;
|
||||
|
||||
try {
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
final supportDir = await getApplicationSupportDirectory();
|
||||
|
||||
debugPrint('开始清理...');
|
||||
debugPrint('临时目录: ${tempDir.path}');
|
||||
debugPrint('文档目录: ${appDocDir.path}');
|
||||
debugPrint('支持目录: ${supportDir.path}');
|
||||
|
||||
await _deleteDirectory(tempDir);
|
||||
await _deleteDirectory(appDocDir);
|
||||
await _deleteDirectory(supportDir);
|
||||
|
||||
debugPrint('原生清理完成');
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
Future.delayed(const Duration(milliseconds: 300), () {
|
||||
if (mounted) {
|
||||
_showExitConfirmDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('原生清理失败: $e');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('清理失败: $e'), backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _clearCache() async {
|
||||
final confirmed = await _showConfirmDialog(
|
||||
'清空缓存',
|
||||
@@ -148,13 +332,49 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text('缓存已清空'),
|
||||
backgroundColor: AppConstants.primaryColor,
|
||||
),
|
||||
);
|
||||
try {
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
if (await tempDir.exists()) {
|
||||
await _deleteDirectory(tempDir);
|
||||
}
|
||||
|
||||
await _calculateCacheSize();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text('缓存已清空'),
|
||||
backgroundColor: AppConstants.primaryColor,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('清空缓存失败: $e');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('清空失败: $e'), backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteDirectory(Directory dir) async {
|
||||
try {
|
||||
if (await dir.exists()) {
|
||||
await for (FileSystemEntity entity in dir.list()) {
|
||||
if (entity is File) {
|
||||
try {
|
||||
await entity.delete();
|
||||
} catch (e) {
|
||||
debugPrint('删除文件失败: ${entity.path}, $e');
|
||||
}
|
||||
} else if (entity is Directory) {
|
||||
await _deleteDirectory(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('删除目录失败: ${dir.path}, $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +441,46 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showExitConfirmDialog() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
title: Row(
|
||||
children: [
|
||||
Icon(Icons.power_settings_new, color: Colors.deepOrange[700]),
|
||||
const SizedBox(width: 8),
|
||||
const Expanded(child: Text('关闭应用', style: TextStyle(fontSize: 18))),
|
||||
],
|
||||
),
|
||||
content: const Text('数据清理完成!\n\n建议关闭应用后重新启动,\n以确保应用正常运行。\n\n是否现在关闭应用?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: Text('稍后关闭', style: TextStyle(color: Colors.grey[600])),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.deepOrange[700],
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
child: const Text('立即关闭'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true) {
|
||||
if (mounted) {
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _showSnackBar(String message) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
@@ -303,13 +563,15 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
],
|
||||
),
|
||||
),
|
||||
_buildDataItem('📦 软件包', '情景诗词 v1.2.19'),
|
||||
_buildDataItem('📦 软件包', _packageSize),
|
||||
_buildDivider(),
|
||||
_buildDataItem('📱 配置项数量', '$_sharedPrefsCount 项'),
|
||||
_buildDivider(),
|
||||
_buildDataItem('💾 缓存大小', '计算中...'),
|
||||
_buildDataItem('💾 缓存大小', _cacheSize),
|
||||
_buildDivider(),
|
||||
_buildDataItem('📊 数据库大小', '计算中...'),
|
||||
_buildDataItem('📊 数据大小', _dataSize),
|
||||
_buildDivider(),
|
||||
_buildDataItem('📁 占用空间', _totalSize),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -506,6 +768,14 @@ class _AppDataPageState extends State<AppDataPage> {
|
||||
Colors.red,
|
||||
_clearAllData,
|
||||
),
|
||||
_buildDivider(),
|
||||
_buildActionButton(
|
||||
'🔧 原生清理数据',
|
||||
'删除应用数据目录所有文件',
|
||||
Icons.system_security_update_warning,
|
||||
Colors.orange,
|
||||
_nativeClearData,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user