修复内存

This commit is contained in:
会做饭的二哈
2025-03-17 18:18:49 +08:00
parent b1e48999c3
commit c72617aaf0
4 changed files with 29 additions and 14 deletions
+11
View File
@@ -44,6 +44,7 @@ class AppConfig {
},
'system': {
'closeToTray': true,
'enablePing': true, // 添加全局ping开关默认配置
},
},
);
@@ -158,4 +159,14 @@ class AppConfig {
_configManager.set('network.dynamicIP', enabled);
await _configManager.save();
}
// 全局ping开关设置
bool get enablePing {
return _configManager.get<bool>('system.enablePing') ?? true;
}
Future<void> setEnablePing(bool enabled) async {
_configManager.set('system.enablePing', enabled);
await _configManager.save();
}
}
+9 -8
View File
@@ -1,5 +1,4 @@
import 'dart:async';
import 'package:astral/utils/app_info.dart';
import 'package:astral/utils/up.dart';
import 'package:flutter/material.dart';
@@ -21,8 +20,6 @@ final updateChecker = UpdateChecker(
);
class _SettingsPageState extends State<SettingsPage> {
bool _notificationsEnabled = true;
double _fontSize = 16.0;
late List<String> _serverList;
late String _currentServer;
final _appConfig = AppConfig();
@@ -40,6 +37,8 @@ class _SettingsPageState extends State<SettingsPage> {
_currentServer = _appConfig.currentServer;
serverIP = _appConfig.currentServer;
_closeToTray = _appConfig.closeToTray; // 初始化托盘设置
// 初始化全局ping开关
_pingEnabled = _appConfig.enablePing;
// 初始化 ping 状态
for (var server in _serverList) {
@@ -58,8 +57,8 @@ class _SettingsPageState extends State<SettingsPage> {
// 取消之前的计时器(如果存在)
_pingTimer?.cancel();
// 创建新的计时器,每秒执行一次 ping
_pingTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
// 创建新的计时器,使用设置的间隔时间
_pingTimer = Timer.periodic(Duration(milliseconds: 1000), (timer) {
if (!mounted) {
timer.cancel();
return;
@@ -96,6 +95,7 @@ class _SettingsPageState extends State<SettingsPage> {
void _togglePingStatus(bool value) {
setState(() {
_pingEnabled = value;
_appConfig.setEnablePing(_pingEnabled);
if (!_pingEnabled) {
// 如果关闭ping,清空所有结果
for (var server in _serverList) {
@@ -104,7 +104,7 @@ class _SettingsPageState extends State<SettingsPage> {
}
});
}
// 添加服务器对话框
Future<void> _showAddServerDialog() async {
final controller = TextEditingController();
@@ -142,7 +142,7 @@ class _SettingsPageState extends State<SettingsPage> {
});
}
}
// 编辑服务器对话框
Future<void> _showEditServerDialog(int index) async {
final controller = TextEditingController(text: _serverList[index]);
@@ -294,7 +294,8 @@ class _SettingsPageState extends State<SettingsPage> {
onTap: () {
setState(() {
_currentServer = server;
Provider.of<KM>(context, listen: false).serverIP = server;
Provider.of<KM>(context, listen: false).serverIP =
server;
_appConfig.setCurrentServer(_currentServer);
});
ScaffoldMessenger.of(context).showSnackBar(
+8 -5
View File
@@ -3,25 +3,28 @@ import 'dart:async';
class PingUtil {
static Future<int?> ping(String host) async {
Socket? socket;
try {
// 从 host:port 格式中提取主机名
final hostname = host.split(':')[0];
final post = host.split(':')[1];
final port = host.split(':')[1]; // 修正变量名 post -> port
// 使用 Socket 连接来测量实际网络延迟
final startTime = DateTime.now();
final socket = await Socket.connect(hostname, int.parse(post),
socket = await Socket.connect(hostname, int.parse(port),
timeout: const Duration(seconds: 2));
final endTime = DateTime.now();
// 关闭连接
await socket.close();
// 计算延迟时间
return endTime.difference(startTime).inMilliseconds;
} on SocketException {
return null;
} catch (e) {
return null;
} finally {
// 确保在所有情况下都关闭 socket 连接
socket?.destroy();
socket = null;
}
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.2
version: 1.0.3
environment:
sdk: ^3.5.4