feat: 节点测速调整

This commit is contained in:
2025-11-26 19:23:15 -08:00
parent 909020654f
commit b059d01556
6 changed files with 513 additions and 740 deletions
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';
import 'package:kaer_with_panels/app/services/kr_subscribe_service.dart';
import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
import 'package:kaer_with_panels/app/utils/kr_secure_storage.dart';
import '../../../localization/app_translations.dart';
import 'package:kaer_with_panels/app/modules/kr_home/controllers/kr_home_controller.dart';
@@ -40,52 +41,34 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
KRLogUtil.kr_i('连接类型已更新为: $type', tag: 'HINodeListController');
// 这里可以添加其他需要的逻辑
}
// 如果当前有选择的国家,触发重选检查
if (homeController.currentSelectedCountry.isNotEmpty) {
KRLogUtil.kr_i('连接类型更新后,检查是否需要重选节点', tag: 'HINodeListController');
// 延迟一下让连接类型更新完成
Future.delayed(const Duration(milliseconds: 500), () {
// homeController.checkCountryReselection(KRSingBoxImp.instance.kr_activeGroups);
});
}
}
/// 处理模式按钮点击(用于激活调试模式)
void kr_handleModeButtonClick() {
final now = DateTime.now();
// 检查是否在2秒内连续点击
if (lastModeButtonClickTime != null &&
if (lastModeButtonClickTime != null &&
now.difference(lastModeButtonClickTime!).inSeconds > 2) {
// 超过2秒,重置计数器
modeButtonClickCount = 0;
}
modeButtonClickCount++;
lastModeButtonClickTime = now;
KRLogUtil.kr_i('模式按钮点击次数: $modeButtonClickCount', tag: 'HINodeListController');
KRLogUtil.kr_i('模式按钮点击次数: $modeButtonClickCount',
tag: 'HINodeListController');
if (modeButtonClickCount >= 5) {
// 激活调试模式
isDebugMode.value = true;
modeButtonClickCount = 0; // 重置计数器
KRLogUtil.kr_i('🐛 调试模式已激活!', tag: 'HINodeListController');
}
}
/// 获取要显示的节点列表(根据调试模式过滤)
List<dynamic> kr_getFilteredNodeList() {
if (isDebugMode.value) {
// 调试模式:显示所有节点
return kr_subscribeService.allList;
} else {
// 正常模式:只显示 country-auto 节点
return kr_subscribeService.allList.where((node) => node.tag.endsWith('-auto')).toList();
}
}
/// 重置调试模式
void kr_resetDebugMode() {
@@ -97,6 +80,15 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
Future<void> kr_handleRefresh() async {
await kr_subscribeService.kr_refreshAll();
try {
final savedNode =
await KRSecureStorage().kr_readData(key: 'SELECTED_NODE_TAG');
if (savedNode != null && savedNode.isNotEmpty) {
homeController.kr_currentNodeName.value = savedNode;
homeController.kr_cutTag.value = savedNode;
homeController.kr_cutSeletedTag.value = savedNode;
}
} catch (_) {}
if (!homeController.kr_isLatency.value) {
homeController.kr_urlTest();
}
@@ -106,11 +98,6 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
void onInit() {
super.onInit();
WidgetsBinding.instance.addObserver(this);
ever(homeController.kr_cutTag, (tag) {
if (homeController.kr_isLatency.value) return;
KRLogUtil.kr_i('🔄 节点切换成功 - 自动触发延迟测试', tag: 'HINodeListView');
homeController.kr_urlTest();
});
}
@override
@@ -126,7 +113,7 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
super.didChangeAppLifecycleState(state);
// 当应用从后台切换回前台时
if (state == AppLifecycleState.resumed) {
if (homeController.kr_isLatency.value) return;
if (homeController.kr_isLatency.value) return;
KRLogUtil.kr_i('🔄 节点列表显示 - 自动触发延迟测试', tag: 'HINodeListView');
homeController.kr_urlTest();
}
@@ -137,5 +124,4 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
WidgetsBinding.instance.removeObserver(this);
super.onClose();
}
}
@@ -26,13 +26,14 @@ class HINodeListView extends GetView<HINodeListController> {
/// 获取分组内最快节点的延迟值(单位:ms)
/// 如果列表为空,返回 0
int getFastestNodeDelay(
HINodeListController controller,
List<KROutboundItem> outboundList,
) {
HINodeListController controller,
List<KROutboundItem> outboundList,
) {
if (outboundList.isEmpty) return 0;
// 返回最小延迟值
outboundList.sort((a, b) => a.urlTestDelay.value.compareTo(b.urlTestDelay.value));
outboundList
.sort((a, b) => a.urlTestDelay.value.compareTo(b.urlTestDelay.value));
return outboundList.first.urlTestDelay.value;
}
@@ -152,36 +153,39 @@ class HINodeListView extends GetView<HINodeListController> {
],
),
),
Obx(() => controller.homeController.kr_coutryText.value == 'auto'
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
Obx(() =>
controller.homeController.kr_cutSeletedTag.value ==
'auto'
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
],
),
),
),
...controller.kr_subscribeService.countryOutboundList.map((country) {
...controller.kr_subscribeService.countryOutboundList
.map((country) {
return InkWell(
onTap: () async {
try {
final success =
await controller.homeController.kr_performNodeSwitch('auto');
final fastest = findFastestNode(country.outboundList);
final success = await controller.homeController
.kr_performNodeSwitch(fastest.tag);
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
}
} catch (e) {
KRLogUtil.kr_e('Auto选项切换异常: $e',
tag: 'NodeListView');
KRLogUtil.kr_e('国家选择切换异常: $e', tag: 'NodeListView');
}
},
child: _kr_buildCountryListItem(context, country: country),
@@ -189,86 +193,87 @@ class HINodeListView extends GetView<HINodeListController> {
}).toList(),
]
] //
),
),
);
});
}
/// 构建默认的订阅节点列表
Widget _buildSubscribeList(BuildContext context) {
return Obx(() {
return _kr_buildListContainer(
context,
child: ListView(
padding: EdgeInsets.symmetric(vertical: 8.w),
// 2. 使用 children 属性,并一次性构建所有列表项
children: [
...[
InkWell(
// 🔧 修复:改为 async,等待节点切换完成后再关闭列表
onTap: () async {
try {
final success =
await controller.homeController.kr_performNodeSwitch('auto');
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
}
} catch (e) {
KRLogUtil.kr_e('Auto选项切换异常: $e',
tag: 'NodeListView');
}
},
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.white.withOpacity(0.3),
width: 1.0,
),
),
),
padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 32.w,
height: 22.w,
padding: EdgeInsets.symmetric(vertical: 8.w),
// 2. 使用 children 属性,并一次性构建所有列表项
children: [
...[
InkWell(
// 🔧 修复:改为 async,等待节点切换完成后再关闭列表
onTap: () async {
try {
final success = await controller.homeController
.kr_performNodeSwitch('auto');
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
}
} catch (e) {
KRLogUtil.kr_e('Auto选项切换异常: $e', tag: 'NodeListView');
}
},
child: Container(
decoration: BoxDecoration(
// 2. 设置背景色为主题色
color: Theme.of(context).primaryColor,
),
// 4. 使用 Center 来确保内部的图片水平和垂直居中
child: Center(
child: KrLocalImage(
imageName: "hi-home-logo",
width: 14.w,
height: 14.h,
border: Border(
bottom: BorderSide(
color: Colors.white.withOpacity(0.3),
width: 1.0,
),
),
),
),
SizedBox(width: 8.w),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
padding:
EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'自动匹配最快网络', // 您指定的文本
style: KrAppTextStyle(
fontSize: 14,
color: Colors.white,
fontWeight: FontWeight.w600, // 600 加粗
Container(
width: 32.w,
height: 22.w,
decoration: BoxDecoration(
// 2. 设置背景色为主题色
color: Theme.of(context).primaryColor,
),
// 4. 使用 Center 来确保内部的图片水平和垂直居中
child: Center(
child: KrLocalImage(
imageName: "hi-home-logo",
width: 14.w,
height: 14.h,
),
),
),
// 2. 第二个 Text: "根据网络IP自动匹配最快线路"
Text(
'根据网络IP自动匹配最快线路', // 默认文本
style: KrAppTextStyle(
fontSize: 10,
color: Colors.white,
),
),
/* Obx(() {
SizedBox(width: 8.w),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'自动匹配最快网络', // 您指定的文本
style: KrAppTextStyle(
fontSize: 14,
color: Colors.white,
fontWeight: FontWeight.w600, // 600 加粗
),
),
// 2. 第二个 Text: "根据网络IP自动匹配最快线路"
Text(
'根据网络IP自动匹配最快线路', // 默认文本
style: KrAppTextStyle(
fontSize: 10,
color: Colors.white,
),
),
/* Obx(() {
// 当选择全局 auto 时,显示当前选中的节点信息
if (controller.homeController.kr_cutTag.value == 'auto') {
final autoNodeInfo = controller.homeController.kr_getGlobalAutoSelectedNode();
@@ -290,51 +295,49 @@ class HINodeListView extends GetView<HINodeListController> {
),
);
}),*/
],
),
),
Obx(() =>
controller.homeController.kr_cutTag.value == 'auto'
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
],
),
),
Obx(() => controller.homeController.kr_cutTag.value == 'auto'
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
],
),
),
...controller.kr_subscribeService.allList().map((item) {
return InkWell(
// 🔧 修复:改为 async,等待节点切换完成后再关闭列表
onTap: () async {
try {
KRLogUtil.kr_i('🔄 用户点击节点: ${item.tag}');
final success = await controller.homeController
.kr_performNodeSwitch(item.tag);
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
}
} catch (e) {
KRLogUtil.kr_e('节点切换异常: $e', tag: 'NodeListView');
}
},
child: _kr_buildNodeListItem(context, item: item),
);
}).toList(),
]
] //
),
),
...controller.kr_subscribeService.allList().map((item) {
return InkWell(
// 🔧 修复:改为 async,等待节点切换完成后再关闭列表
onTap: () async {
try {
KRLogUtil.kr_i(
'🔄 用户点击节点: ${item.tag}');
final success = await controller.homeController
.kr_performNodeSwitch(item.tag);
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
}
} catch (e) {
KRLogUtil.kr_e(
'节点切换异常: $e',
tag: 'NodeListView');
}
},
child: _kr_buildNodeListItem(context, item: item),
);
}).toList(),
]
] //
),
);
});
}
@@ -346,7 +349,8 @@ class HINodeListView extends GetView<HINodeListController> {
alignment: Alignment.center,
child: Text(
text,
style: KrAppTextStyle(fontSize: 14, color: Theme.of(context).textTheme.bodySmall?.color),
style: KrAppTextStyle(
fontSize: 14, color: Theme.of(context).textTheme.bodySmall?.color),
),
);
}
@@ -363,7 +367,8 @@ class HINodeListView extends GetView<HINodeListController> {
}
/// 构建单个节点列表项的UI
Widget _kr_buildNodeListItem(BuildContext context, {required KROutboundItem item}) {
Widget _kr_buildNodeListItem(BuildContext context,
{required KROutboundItem item}) {
return Container(
key: ValueKey(item.id),
decoration: BoxDecoration(
@@ -378,7 +383,12 @@ class HINodeListView extends GetView<HINodeListController> {
padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
child: Row(
children: [
KRCountryFlag(countryCode: item.country, width: 30.w, height: 20.w, isCircle: false, maintainSize: false),
KRCountryFlag(
countryCode: item.country,
width: 30.w,
height: 20.w,
isCircle: false,
maintainSize: false),
SizedBox(width: 12.w),
Expanded(
child: Obx(() {
@@ -404,7 +414,8 @@ class HINodeListView extends GetView<HINodeListController> {
bool isTesting = controller.homeController.kr_isLatency.value;
// 极简逻辑:只根据节点类型决定显示方式
print('🔄 _kr_buildNodeListItem节点: item.tag=${item.tag}, country=${item.country}');
print(
'🔄 _kr_buildNodeListItem节点: item.tag=${item.tag}, country=${item.country}');
// 普通节点,直接显示节点自身的速度
displayDelay = item.urlTestDelay.value;
@@ -441,19 +452,23 @@ class HINodeListView extends GetView<HINodeListController> {
);
}),
SizedBox(width: 12.w),
Obx(() => controller.homeController.kr_cutTag.value == item.tag
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
Obx(() {
final selected =
controller.homeController.kr_cutSeletedTag.value == item.tag;
return selected
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w,
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w,
height: 16.h,
);
}),
],
),
);
@@ -461,7 +476,6 @@ class HINodeListView extends GetView<HINodeListController> {
/// 构建国家列表项的UI
Widget _kr_buildCountryListItem(BuildContext context, {required country}) {
return Container(
key: ValueKey(country),
decoration: BoxDecoration(
@@ -476,17 +490,26 @@ class HINodeListView extends GetView<HINodeListController> {
padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
child: Row(
children: [
KRCountryFlag(countryCode: country.country, width: 30.w, height: 20.w, isCircle: false, maintainSize: false),
KRCountryFlag(
countryCode: country.country,
width: 30.w,
height: 20.w,
isCircle: false,
maintainSize: false),
SizedBox(width: 12.w),
Expanded(
child: Text(
controller.homeController.kr_getCountryFullName(country.country),
style: KrAppTextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.white),
style: KrAppTextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.white),
),
),
Obx(() {
// 1. 获取延迟值和测速状态
int displayDelay = getFastestNodeDelay(controller, country.outboundList);
int displayDelay =
getFastestNodeDelay(controller, country.outboundList);
bool isTesting = controller.homeController.kr_isLatency.value;
// 2. 声明文本和颜色变量
@@ -520,21 +543,36 @@ class HINodeListView extends GetView<HINodeListController> {
);
}),
SizedBox(width: 12.w),
Obx(() => controller.homeController.kr_cutTag.value == '${country.country}-auto'
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w, // 适当缩小 SVG 尺寸,留出白边
height: 16.h,
)),
Obx(() {
final selectedTag =
controller.homeController.kr_cutSeletedTag.value;
if (selectedTag == 'auto') {
return KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w,
height: 16.h,
);
}
final node = controller.kr_subscribeService.keyList[selectedTag];
final selectedCountry = node?.country ?? '';
final selected = selectedCountry == country.country;
return selected
? KrLocalImage(
imageName: 'radio-active-icon',
imageType: ImageType.svg,
width: 16.w,
height: 16.h,
)
: KrLocalImage(
imageName: 'radio-icon',
imageType: ImageType.svg,
width: 16.w,
height: 16.h,
);
}),
],
),
);
}
}
}