From c72617aaf0991a6ffad5a86e5b337ef418b519d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=9A=E5=81=9A=E9=A5=AD=E7=9A=84=E4=BA=8C=E5=93=88?= Date: Mon, 17 Mar 2025 18:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/config/app_config.dart | 11 +++++++++++ lib/screens/settings.dart | 17 +++++++++-------- lib/utils/ping_util.dart | 13 ++++++++----- pubspec.yaml | 2 +- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/config/app_config.dart b/lib/config/app_config.dart index 33473a6..29452b5 100644 --- a/lib/config/app_config.dart +++ b/lib/config/app_config.dart @@ -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('system.enablePing') ?? true; + } + + Future setEnablePing(bool enabled) async { + _configManager.set('system.enablePing', enabled); + await _configManager.save(); + } } diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 3cc1fb3..e12edae 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -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 { - bool _notificationsEnabled = true; - double _fontSize = 16.0; late List _serverList; late String _currentServer; final _appConfig = AppConfig(); @@ -40,6 +37,8 @@ class _SettingsPageState extends State { _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 { // 取消之前的计时器(如果存在) _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 { 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 { } }); } - + // 添加服务器对话框 Future _showAddServerDialog() async { final controller = TextEditingController(); @@ -142,7 +142,7 @@ class _SettingsPageState extends State { }); } } - + // 编辑服务器对话框 Future _showEditServerDialog(int index) async { final controller = TextEditingController(text: _serverList[index]); @@ -294,7 +294,8 @@ class _SettingsPageState extends State { onTap: () { setState(() { _currentServer = server; - Provider.of(context, listen: false).serverIP = server; + Provider.of(context, listen: false).serverIP = + server; _appConfig.setCurrentServer(_currentServer); }); ScaffoldMessenger.of(context).showSnackBar( diff --git a/lib/utils/ping_util.dart b/lib/utils/ping_util.dart index 5f784b6..d2b5e90 100644 --- a/lib/utils/ping_util.dart +++ b/lib/utils/ping_util.dart @@ -3,25 +3,28 @@ import 'dart:async'; class PingUtil { static Future 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; } } } diff --git a/pubspec.yaml b/pubspec.yaml index 50597b6..449ca40 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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