Files
MEIZU-Battery-Healthy/lib/widgets/charge_entry.dart
T

115 lines
2.9 KiB
Dart

import 'package:flutter/material.dart';
import '../theme/app_colors.dart';
class ChargeEntry extends StatelessWidget {
final String summary;
final bool isCharging;
final VoidCallback onTap;
const ChargeEntry({
super.key,
required this.summary,
required this.isCharging,
required this.onTap,
});
@override
Widget build(BuildContext context) {
final c = context.colors;
return GestureDetector(
onTap: isCharging ? onTap : null,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: RichText(
overflow: TextOverflow.ellipsis,
text: TextSpan(
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: c.textPrimary,
),
children: _buildSpans(summary),
),
),
),
Row(
children: [
Text(
'充电信息',
style: TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
fontSize: 16,
fontWeight: FontWeight.w500,
color: c.textTertiary,
),
),
const SizedBox(width: 4),
Text(
'>',
style: TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
fontSize: 16,
fontWeight: FontWeight.w500,
color: c.textTertiary,
),
),
],
),
],
),
);
}
/// 按 CJK / 非 CJK 拆分字符串,分别应用 HarmonyOS / Flyme
List<TextSpan> _buildSpans(String text) {
final spans = <TextSpan>[];
if (text.isEmpty) return spans;
final buffer = StringBuffer();
bool? isCJK;
void flush() {
if (buffer.isEmpty) return;
spans.add(
TextSpan(
text: buffer.toString(),
style: TextStyle(
fontFamily: (isCJK ?? false) ? 'HarmonyOS_Sans_SC' : 'Flyme',
),
),
);
buffer.clear();
}
for (final rune in text.runes) {
final cjk = _isCJKRune(rune);
if (isCJK == null || cjk == isCJK) {
buffer.writeCharCode(rune);
isCJK = cjk;
} else {
flush();
buffer.writeCharCode(rune);
isCJK = cjk;
}
}
flush();
return spans;
}
bool _isCJKRune(int rune) {
// CJK 统一表意文字
if (rune >= 0x4E00 && rune <= 0x9FFF) return true;
// CJK 扩展 A
if (rune >= 0x3400 && rune <= 0x4DBF) return true;
// 中文标点(。、,:;等)
if (rune >= 0x3000 && rune <= 0x303F) return true;
// CJK 兼容符号
if (rune >= 0xFE30 && rune <= 0xFE4F) return true;
// 全角字符
if (rune >= 0xFF00 && rune <= 0xFFEF) return true;
return false;
}
}