修正切换和国旗问题

(cherry picked from commit bec2464da85e981636077276de36b2ea5f6c40f4)
This commit is contained in:
Rust 2025-10-31 08:10:44 -07:00 committed by speakeloudest
parent 7083369cd4
commit 138209929b
5 changed files with 166 additions and 10 deletions

View File

@ -46,6 +46,12 @@ class KrOutboundsList {
final KROutboundItem item = KROutboundItem(element); final KROutboundItem item = KROutboundItem(element);
// 🔍 country
print('🗺️ 构建节点: name="${element.name}", tag="${item.tag}", country="${item.country}"');
print(' - element.country: "${element.country}"');
print(' - item.country: "${item.country}"');
print(' - country.isEmpty: ${item.country.isEmpty}');
// type // type
if (item.config.isEmpty || !item.config.containsKey('type')) { if (item.config.isEmpty || !item.config.containsKey('type')) {
print('⚠️ 跳过无效节点: ${element.name},配置为空或缺少 type 字段'); print('⚠️ 跳过无效节点: ${element.name},配置为空或缺少 type 字段');
@ -66,6 +72,7 @@ class KrOutboundsList {
configJsonList.add(item.config); configJsonList.add(item.config);
keyList[item.tag] = item; keyList[item.tag] = item;
print('✅ keyList["${item.tag}"] 已设置country="${item.country}"');
} }
// KRGroupOutboundList groupOutboundList // KRGroupOutboundList groupOutboundList

View File

@ -1195,6 +1195,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
// //
kr_cutTag.value = originalTag; kr_cutTag.value = originalTag;
kr_currentNodeName.value = originalTag; // 🔧
kr_currentNodeLatency.value = -2; // kr_currentNodeLatency.value = -2; //
// //
@ -1222,11 +1223,77 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
} }
/// ///
/// 🔧 使 kr_cutTag kr_cutSeletedTagUI立即响应
/// 🔧 "auto"
String kr_getCurrentNodeCountry() { String kr_getCurrentNodeCountry() {
if (kr_cutSeletedTag.isEmpty) return ''; KRLogUtil.kr_i('========== 开始获取国家代码 ==========', tag: 'getCurrentNodeCountry');
final node = kr_subscribeService.keyList[kr_cutSeletedTag.value]; KRLogUtil.kr_i('kr_cutTag: ${kr_cutTag.value}', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i(kr_cutSeletedTag.value, tag: "kr_getCurrentNodeCountry"); KRLogUtil.kr_i('kr_cutSeletedTag: ${kr_cutSeletedTag.value}', tag: 'getCurrentNodeCountry');
return node?.country ?? ''; KRLogUtil.kr_i('keyList 节点总数: ${kr_subscribeService.keyList.length}', tag: 'getCurrentNodeCountry');
if (kr_cutTag.isEmpty) {
KRLogUtil.kr_w('kr_cutTag 为空,返回空字符串', tag: 'getCurrentNodeCountry');
return '';
}
String actualTag = kr_cutTag.value;
// 🔧 "auto"
if (actualTag == 'auto' || actualTag == 'select') {
try {
KRLogUtil.kr_i('检测到特殊标签: $actualTag,尝试从活动组获取实际节点', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i('活动组数量: ${KRSingBoxImp.instance.kr_activeGroups.length}', tag: 'getCurrentNodeCountry');
// SingBox "select"
final selectGroup = KRSingBoxImp.instance.kr_activeGroups.firstWhere(
(group) => group.tag == 'select',
orElse: () => throw Exception('未找到 select 组'),
);
KRLogUtil.kr_i('找到 select 组,当前选中: ${selectGroup.selected}', tag: 'getCurrentNodeCountry');
//
if (selectGroup.selected.isNotEmpty) {
actualTag = selectGroup.selected;
KRLogUtil.kr_i('从 select 组获取实际节点: $actualTag', tag: 'getCurrentNodeCountry');
}
} catch (e) {
KRLogUtil.kr_w('获取实际节点失败: $e', tag: 'getCurrentNodeCountry');
// 使 kr_cutSeletedTag
if (kr_cutSeletedTag.value.isNotEmpty && kr_cutSeletedTag.value != 'auto') {
actualTag = kr_cutSeletedTag.value;
KRLogUtil.kr_i('使用 kr_cutSeletedTag 作为备选: $actualTag', tag: 'getCurrentNodeCountry');
}
}
}
// 使
KRLogUtil.kr_i('查找节点: $actualTag', tag: 'getCurrentNodeCountry');
final node = kr_subscribeService.keyList[actualTag];
if (node == null) {
KRLogUtil.kr_e('❌ 节点未找到: $actualTag', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_e('keyList 中的所有节点标签:', tag: 'getCurrentNodeCountry');
int count = 0;
for (var key in kr_subscribeService.keyList.keys) {
count++;
KRLogUtil.kr_e(' [$count] $key -> country: ${kr_subscribeService.keyList[key]?.country}', tag: 'getCurrentNodeCountry');
if (count >= 10) {
KRLogUtil.kr_e(' ... 还有 ${kr_subscribeService.keyList.length - 10} 个节点', tag: 'getCurrentNodeCountry');
break;
}
}
return '';
}
KRLogUtil.kr_i('✅ 找到节点: $actualTag', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i(' - city: ${node.city}', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i(' - country: "${node.country}"', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i(' - country.isEmpty: ${node.country.isEmpty}', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i(' - country.length: ${node.country.length}', tag: 'getCurrentNodeCountry');
KRLogUtil.kr_i('========== 国家代码获取结束 ==========', tag: 'getCurrentNodeCountry');
return node.country;
} }
/// auto /// auto

View File

@ -79,9 +79,14 @@ class KRHomeConnectionInfoView extends GetView<KRHomeController> {
children: [ children: [
Row( Row(
children: [ children: [
KRCountryFlag( // 🔧 使 Obx
countryCode: controller.kr_getCurrentNodeCountry(), Obx(() {
), final countryCode = controller.kr_getCurrentNodeCountry();
print('🌍 ConnectionInfo 更新,国家代码: $countryCode');
return KRCountryFlag(
countryCode: countryCode,
);
}),
SizedBox(width: 10.w), SizedBox(width: 10.w),
Column( Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,

View File

@ -656,6 +656,51 @@ class KRSingBoxImp {
); );
} }
///
///
/// "select" selected
Future<void> _kr_verifyNodeSelection(String targetTag) async {
try {
KRLogUtil.kr_i('🔍 开始验证节点选择: $targetTag', tag: 'SingBox');
// "select"
final selectGroup = kr_activeGroups.firstWhere(
(group) => group.tag == 'select',
orElse: () => throw Exception('未找到 "select" 选择器组'),
);
KRLogUtil.kr_i('📊 Select 组状态:', tag: 'SingBox');
KRLogUtil.kr_i(' - 组标签: ${selectGroup.tag}', tag: 'SingBox');
KRLogUtil.kr_i(' - 组类型: ${selectGroup.type}', tag: 'SingBox');
KRLogUtil.kr_i(' - 当前选中: ${selectGroup.selected}', tag: 'SingBox');
KRLogUtil.kr_i(' - 目标节点: $targetTag', tag: 'SingBox');
KRLogUtil.kr_i(' - 可用节点数: ${selectGroup.items.length}', tag: 'SingBox');
//
final hasTarget = selectGroup.items.any((item) => item.tag == targetTag);
if (!hasTarget) {
KRLogUtil.kr_w('⚠️ 目标节点不在 select 组的可用列表中: $targetTag', tag: 'SingBox');
KRLogUtil.kr_w('可用节点列表:', tag: 'SingBox');
for (var item in selectGroup.items) {
KRLogUtil.kr_w(' - ${item.tag}', tag: 'SingBox');
}
}
//
if (selectGroup.selected != targetTag) {
KRLogUtil.kr_e('❌ 节点切换验证失败!', tag: 'SingBox');
KRLogUtil.kr_e(' - 期望: $targetTag', tag: 'SingBox');
KRLogUtil.kr_e(' - 实际: ${selectGroup.selected}', tag: 'SingBox');
throw Exception('节点切换失败:实际选中 ${selectGroup.selected},期望 $targetTag');
}
KRLogUtil.kr_i('✅ 节点切换验证成功: ${selectGroup.selected} == $targetTag', tag: 'SingBox');
} catch (e) {
KRLogUtil.kr_e('❌ 节点验证异常: $e', tag: 'SingBox');
//
}
}
/// ///
/// ///
/// command.sock /// command.sock
@ -1345,7 +1390,13 @@ class KRSingBoxImp {
// 🔧 使 await // 🔧 使 await
KRLogUtil.kr_i('⏳ 调用 selectOutbound("select", "$tag")...', tag: 'SingBox'); KRLogUtil.kr_i('⏳ 调用 selectOutbound("select", "$tag")...', tag: 'SingBox');
await _kr_selectOutboundWithRetry("select", tag, maxAttempts: 3, initialDelay: 50); await _kr_selectOutboundWithRetry("select", tag, maxAttempts: 3, initialDelay: 50);
KRLogUtil.kr_i('✅ 节点切换完成: $tag', tag: 'SingBox'); KRLogUtil.kr_i('✅ 节点切换API调用完成: $tag', tag: 'SingBox');
// 🔧
await Future.delayed(const Duration(milliseconds: 300)); //
await _kr_verifyNodeSelection(tag);
KRLogUtil.kr_i('✅ 节点切换验证完成: $tag', tag: 'SingBox');
} catch (e) { } catch (e) {
KRLogUtil.kr_e('❌ 节点选择失败: $e', tag: 'SingBox'); KRLogUtil.kr_e('❌ 节点选择失败: $e', tag: 'SingBox');
rethrow; // rethrow; //

View File

@ -37,12 +37,38 @@ class KRCountryFlag extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// 🔍
print('🏳️ KRCountryFlag.build 被调用');
print(' - 原始 countryCode: "$countryCode"');
print(' - countryCode.isEmpty: ${countryCode.isEmpty}');
print(' - countryCode.length: ${countryCode.length}');
final processedCode = _getCountryCode(countryCode);
print(' - 处理后 code: "$processedCode"');
//
if (countryCode.isEmpty) {
print(' ❌ 国家代码为空,显示占位符');
return Container(
width: width ?? 50.w,
height: height ?? 50.w,
decoration: BoxDecoration(
color: Colors.grey[300],
shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
borderRadius: !isCircle ? BorderRadius.circular(8.w) : null,
),
child: Icon(Icons.flag, color: Colors.grey[600], size: 24.w),
);
}
// //
final double actualWidth = width ?? 50.w; final double actualWidth = width ?? 50.w;
final double actualHeight = maintainSize ? actualWidth : (height ?? 50.w); final double actualHeight = maintainSize ? actualWidth : (height ?? 50.w);
print(' ✅ 尝试加载国旗: $processedCode');
Widget flagWidget = CountryFlag.fromCountryCode( Widget flagWidget = CountryFlag.fromCountryCode(
_getCountryCode(countryCode), processedCode,
width: actualWidth, width: actualWidth,
height: actualHeight, height: actualHeight,
); );