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

231 lines
7.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../models/data_abnormal_type.dart';
class BatteryInfoPanel extends StatelessWidget {
final double batteryVolt;
final double batteryTemp;
final double maxVolt;
final double minVolt;
final double maxTemp;
final double minTemp;
final DataAbnormalType abnormalType;
const BatteryInfoPanel({
super.key,
required this.batteryVolt,
required this.batteryTemp,
required this.maxVolt,
required this.minVolt,
required this.maxTemp,
required this.minTemp,
this.abnormalType = DataAbnormalType.none,
});
@override
Widget build(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.dark;
final bool isVoltageAbnormal =
abnormalType == DataAbnormalType.voltageAbnormal;
final Color defaultTextColor = Theme.of(context).colorScheme.onSurface;
final Color voltColor = isVoltageAbnormal ? Colors.red : defaultTextColor;
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildInfoItem(
context, // 🆕
_buildIcon('voltage', isDark),
'电压',
'${batteryVolt.toStringAsFixed(3)}V',
textColor: voltColor,
),
const SizedBox(height: 8),
Row(
children: [
RichText(
text: TextSpan(
style: TextStyle(
fontFamily: 'Flyme',
fontSize: 14,
fontWeight: FontWeight.w500,
color: isVoltageAbnormal
? Colors.red
: defaultTextColor,
),
children: [
TextSpan(
text: '↑ ',
style: const TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
color: Colors.red,
),
),
TextSpan(
text: '${maxVolt.toStringAsFixed(3)}V',
style: TextStyle(
color: isVoltageAbnormal ? Colors.red : Colors.red,
),
),
],
),
),
const SizedBox(width: 12),
RichText(
text: TextSpan(
style: TextStyle(
fontFamily: 'Flyme',
fontSize: 14,
fontWeight: FontWeight.w500,
color: isVoltageAbnormal
? Colors.red
: defaultTextColor,
),
children: [
TextSpan(
text: '↓ ',
style: const TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
color: Colors.blue,
),
),
TextSpan(
text: '${minVolt.toStringAsFixed(3)}V',
style: TextStyle(
color: isVoltageAbnormal ? Colors.red : Colors.blue,
),
),
],
),
),
],
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildInfoItem(
context, // 🆕
_buildIcon('temperature', isDark),
'温度',
'${batteryTemp.toStringAsFixed(0)}℃',
textColor: defaultTextColor,
),
const SizedBox(height: 8),
Row(
children: [
RichText(
text: TextSpan(
style: TextStyle(
fontFamily: 'Flyme',
fontSize: 14,
fontWeight: FontWeight.w500,
color: defaultTextColor,
),
children: [
const TextSpan(
text: '↑ ',
style: TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
color: Colors.red,
),
),
TextSpan(text: '${maxTemp.toStringAsFixed(0)}℃'),
],
),
),
const SizedBox(width: 12),
RichText(
text: TextSpan(
style: TextStyle(
fontFamily: 'Flyme',
fontSize: 14,
fontWeight: FontWeight.w500,
color: defaultTextColor,
),
children: [
const TextSpan(
text: '↓ ',
style: TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
color: Colors.blue,
),
),
TextSpan(text: '${minTemp.toStringAsFixed(0)}℃'),
],
),
),
],
),
],
),
),
],
);
}
Widget _buildIcon(String type, bool isDark) {
final color = isDark ? Colors.white54 : Colors.grey;
switch (type) {
case 'voltage':
return SvgPicture.asset(
'assets/icons/si--lightning-fill.svg',
width: 18,
height: 18,
colorFilter: ColorFilter.mode(color, BlendMode.srcIn),
);
case 'temperature':
return SvgPicture.asset(
'assets/icons/mdi--temperature.svg',
width: 18,
height: 18,
colorFilter: ColorFilter.mode(color, BlendMode.srcIn),
);
default:
return Icon(Icons.help_outline, size: 18, color: color);
}
}
Widget _buildInfoItem(
BuildContext context, // 🆕
Widget icon,
String label,
String value, {
Color textColor = Colors.black,
}) {
final labelColor = Theme.of(context).colorScheme.onSurface.withOpacity(0.7);
return Row(
children: [
icon,
const SizedBox(width: 6),
Text(
label,
style: TextStyle(
fontFamily: 'HarmonyOS_Sans_SC',
fontSize: 14,
color: labelColor,
),
),
const SizedBox(width: 6),
Text(
value,
style: TextStyle(
fontFamily: 'Flyme',
fontSize: 16,
fontWeight: FontWeight.w500,
color: textColor,
),
),
],
);
}
}