120 lines
3.3 KiB
Dart
Executable File
120 lines
3.3 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:country_flags/country_flags.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
class KRCountryFlag extends StatelessWidget {
|
|
final String countryCode;
|
|
final double? width;
|
|
final double? height;
|
|
final bool isCircle;
|
|
final BoxFit fit; // 填充模式
|
|
final bool maintainSize; // 是否保持宽高比
|
|
final Color? bgColor; // 背景色
|
|
final bool clip; // 是否裁剪
|
|
|
|
const KRCountryFlag({
|
|
Key? key,
|
|
required this.countryCode,
|
|
this.width,
|
|
this.height,
|
|
this.isCircle = true,
|
|
this.fit = BoxFit.cover, // 默认填充
|
|
this.maintainSize = true, // 默认保持宽高比
|
|
this.bgColor = Colors.white,
|
|
this.clip = true, // 默认裁剪
|
|
}) : super(key: key);
|
|
|
|
String _getCountryCode(String code) {
|
|
// 处理特殊国家代码
|
|
final Map<String, String> specialCases = {
|
|
'UK': 'gb',
|
|
'USA': 'us',
|
|
// 添加其他特殊情况...
|
|
};
|
|
|
|
return specialCases[code.toUpperCase()] ?? code.toLowerCase();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 🔍 调试日志
|
|
// if (kDebugMode) {
|
|
// print('🏳️ KRCountryFlag.build 被调用');
|
|
// }
|
|
// if (kDebugMode) {
|
|
// print(' - 原始 countryCode: "$countryCode"');
|
|
// }
|
|
// if (kDebugMode) {
|
|
// print(' - countryCode.isEmpty: ${countryCode.isEmpty}');
|
|
// }
|
|
// if (kDebugMode) {
|
|
// print(' - countryCode.length: ${countryCode.length}');
|
|
// }
|
|
|
|
final processedCode = _getCountryCode(countryCode);
|
|
// if (kDebugMode) {
|
|
// print(' - 处理后 code: "$processedCode"');
|
|
// }
|
|
|
|
// 如果国家代码为空,显示占位符
|
|
if (countryCode.isEmpty) {
|
|
if (kDebugMode) {
|
|
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 actualHeight = maintainSize ? actualWidth : (height ?? 50.w);
|
|
|
|
if (kDebugMode) {
|
|
print(' ✅ 尝试加载国旗: $processedCode');
|
|
}
|
|
|
|
Widget flagWidget = CountryFlag.fromCountryCode(
|
|
processedCode,
|
|
width: actualWidth,
|
|
height: actualHeight,
|
|
);
|
|
|
|
// 添加填充模式
|
|
flagWidget = SizedBox(
|
|
width: actualWidth,
|
|
height: actualHeight,
|
|
child: FittedBox(
|
|
fit: fit,
|
|
child: flagWidget,
|
|
),
|
|
);
|
|
|
|
// 添加背景和形状
|
|
flagWidget = Container(
|
|
width: actualWidth,
|
|
height: actualHeight,
|
|
decoration: BoxDecoration(
|
|
color: bgColor,
|
|
shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
|
|
borderRadius: !isCircle ? BorderRadius.circular(8.w) : null,
|
|
),
|
|
child: flagWidget,
|
|
);
|
|
|
|
// 最后添加裁剪
|
|
if (clip && isCircle) {
|
|
flagWidget = ClipOval(child: flagWidget);
|
|
}
|
|
|
|
return flagWidget;
|
|
}
|
|
} |