17 Commits

20 changed files with 1805 additions and 1099 deletions
+4 -5
View File
@@ -24,9 +24,7 @@
| 版本 | 主题 | 状态 | | 版本 | 主题 | 状态 |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| 1.3.0 | 时间窗口 + real_type 优先级 | ✅ 已发布 | | 1.5.0 | 功率计算优化 | ✅ 已发布 |
| 1.4.0 | SAF 权限改造 | 📋 规划中 |
| 1.5.0 | 功率计算优化 | 📋 规划中 |
| 2.0.0 | 数据看板扩展(月/季/年趋势) | 📋 远景 | | 2.0.0 | 数据看板扩展(月/季/年趋势) | 📋 远景 |
--- ---
@@ -35,7 +33,7 @@
- Flutter SDK: ^3.9.2 - Flutter SDK: ^3.9.2
- Android: 8.0+ (API 26+) - Android: 8.0+ (API 26+)
- 仅支持魅族手机(15 系列 ~ 25 系列) - 仅支持魅族手机(15 系列 ~ 22 系列)
--- ---
@@ -49,4 +47,5 @@ flutter pub get
flutter run flutter run
# Release APK # Release APK
flutter build apk --release flutter build apk --release --target-platform android-arm64
```
+73
View File
@@ -0,0 +1,73 @@
plugins {
id("com.android.application")
id("kotlin-android")
id("dev.flutter.flutter-gradle-plugin")
}
android {
namespace = "com.lxh.battery_health"
compileSdk = 34
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
defaultConfig {
applicationId = "com.lxh.battery_health"
minSdk = flutter.minSdkVersion
targetSdk = 34
versionCode = flutter.versionCode
versionName = flutter.versionName
multiDexEnabled = true
}
signingConfigs {
// 如果没有正式签名,可以用 debug 签名代替(测试用)
// 正式发布时请替换为自己的密钥
getByName("debug") {
// debug 签名已经存在,无需额外配置
}
create("release") {
// 如果有正式签名,在这里配置
// keyAlias = "..."
// keyPassword = "..."
// storeFile = file("...")
// storePassword = "..."
}
}
buildTypes {
debug {
// 🟢 关键:debug 版本包名加 .dev 后缀
applicationIdSuffix = ".dev"
signingConfig = signingConfigs.getByName("debug")
isDebuggable = true
isMinifyEnabled = false
isShrinkResources = false
}
release {
// release 版本不加后缀,保持原包名
signingConfig = signingConfigs.getByName("debug") // 若没有正式签名,先用 debug 签名
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
flutter {
source = "../.."
}
dependencies {
// 无需额外添加 Activity KTX,因为已改用 startActivityForResult
}
+16 -14
View File
@@ -1,20 +1,22 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />--> <!--<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />-->
<application <application
android:label="@string/app_name" android:label="@string/app_name"
android:name="${applicationName}" android:name="${applicationName}"
android:icon="@drawable/logo" android:icon="@drawable/logo"
android:requestLegacyExternalStorage="true"> android:requestLegacyExternalStorage="true"
<activity android:theme="@style/LaunchTheme"> <!-- 全局先用 LaunchTheme -->
android:name=".MainActivity"
android:exported="true" <activity
android:launchMode="singleTop" android:name=".MainActivity"
android:taskAffinity="" android:exported="true"
android:theme="@style/LaunchTheme" android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:taskAffinity=""
android:hardwareAccelerated="true" android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustResize"> android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as <!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues while the Flutter UI initializes. After that, this theme continues
@@ -0,0 +1,50 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />-->
<application
android:label="@string/app_name"
android:name="${applicationName}"
android:icon="@drawable/logo"
android:requestLegacyExternalStorage="true"
android:theme="@style/LaunchTheme"> <!-- 全局先用 LaunchTheme -->
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/AppTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import android.os.BatteryManager import android.os.BatteryManager
import android.os.Build import android.os.Build
@@ -20,18 +21,17 @@ import java.io.BufferedReader
import java.io.InputStreamReader import java.io.InputStreamReader
class MainActivity : FlutterActivity() { class MainActivity : FlutterActivity() {
// ---------- 充电状态 ----------
private val CHARGE_CHANNEL = "com.lxh.battery_health/charge" private val CHARGE_CHANNEL = "com.lxh.battery_health/charge"
private val EVENT_CHANNEL = "com.lxh.battery_health/charge_events" private val EVENT_CHANNEL = "com.lxh.battery_health/charge_events"
private var eventSink: EventChannel.EventSink? = null private var eventSink: EventChannel.EventSink? = null
// ---------- SAF 相关 ----------
private val SAF_CHANNEL = "com.lxh.battery_health/saf" private val SAF_CHANNEL = "com.lxh.battery_health/saf"
private var safResult: MethodChannel.Result? = null private var safResult: MethodChannel.Result? = null
private var safUri: Uri? = null private var safUri: Uri? = null
private val FOLDER_PICKER_REQUEST = 1001 private val FOLDER_PICKER_REQUEST = 1001
private val PREF_NAME = "battery_health_prefs"
private val KEY_SAF_URI = "saf_uri"
// ---------- 充电广播接收器 ----------
private val chargeReceiver = object : BroadcastReceiver() { private val chargeReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
intent ?: return intent ?: return
@@ -42,6 +42,46 @@ class MainActivity : FlutterActivity() {
} }
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
android.util.Log.d("BatteryHealth", "onCreate 开始")
try {
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
val uriString = prefs.getString(KEY_SAF_URI, null)
android.util.Log.d("BatteryHealth", "SharedPreferences URI: $uriString")
if (uriString != null) {
safUri = Uri.parse(uriString)
android.util.Log.d("BatteryHealth", "恢复 SAF URI 成功: $safUri")
}
} catch (e: Exception) {
android.util.Log.e("BatteryHealth", "恢复 URI 异常: ${e.message}")
}
android.util.Log.d("BatteryHealth", "onCreate 结束,safUri = $safUri")
}
private fun saveUri(uri: Uri) {
try {
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
prefs.edit().putString(KEY_SAF_URI, uri.toString()).apply()
android.util.Log.d("BatteryHealth", "保存 SAF URI: $uri")
} catch (e: Exception) {
android.util.Log.e("BatteryHealth", "保存 URI 失败: ${e.message}")
}
}
private fun restoreUri(uriString: String) {
try {
safUri = Uri.parse(uriString)
android.util.Log.d("BatteryHealth", "通过 MethodChannel 恢复 URI: $safUri")
// 同时保存到 SharedPreferences
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
prefs.edit().putString(KEY_SAF_URI, uriString).apply()
} catch (e: Exception) {
android.util.Log.e("BatteryHealth", "restoreUri 失败: ${e.message}")
}
}
private fun sendChargeState(isCharging: Boolean) { private fun sendChargeState(isCharging: Boolean) {
val chargeType = if (isCharging) { val chargeType = if (isCharging) {
val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
@@ -66,7 +106,6 @@ class MainActivity : FlutterActivity() {
status == BatteryManager.BATTERY_STATUS_FULL status == BatteryManager.BATTERY_STATUS_FULL
} }
// ---------- SAF 方法 ----------
private fun openFolderPicker() { private fun openFolderPicker() {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
@@ -100,7 +139,8 @@ class MainActivity : FlutterActivity() {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(uri, takeFlags) contentResolver.takePersistableUriPermission(uri, takeFlags)
safUri = uri safUri = uri
android.util.Log.d("BatteryHealth", "SAF URI: $uri") saveUri(uri)
android.util.Log.d("BatteryHealth", "SAF 授权成功 URI: $uri")
safResult?.success(uri.toString()) safResult?.success(uri.toString())
} catch (e: Exception) { } catch (e: Exception) {
android.util.Log.e("BatteryHealth", "SAF 授权失败: ${e.message}") android.util.Log.e("BatteryHealth", "SAF 授权失败: ${e.message}")
@@ -118,9 +158,13 @@ class MainActivity : FlutterActivity() {
} }
private fun listFiles(): List<String> { private fun listFiles(): List<String> {
android.util.Log.d("BatteryHealth", "listFiles 被调用,safUri = $safUri")
val result = mutableListOf<String>() val result = mutableListOf<String>()
val docId = getTreeDocId() val docId = getTreeDocId()
if (docId == null || docId.isEmpty()) return result if (docId == null || docId.isEmpty()) {
android.util.Log.e("BatteryHealth", "docId 为空")
return result
}
try { try {
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(safUri!!, docId) val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(safUri!!, docId)
@@ -134,7 +178,7 @@ class MainActivity : FlutterActivity() {
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
android.util.Log.e("BatteryHealth", "列出文件失败: ${e.message}") android.util.Log.e("BatteryHealth", "列出文件失败: ${e.message}", e)
} }
return result return result
} }
@@ -178,11 +222,9 @@ class MainActivity : FlutterActivity() {
return fileUri?.let { readFile(it) } return fileUri?.let { readFile(it) }
} }
// ---------- Flutter 通道配置 ----------
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
// 充电状态通道
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHARGE_CHANNEL) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHARGE_CHANNEL)
.setMethodCallHandler { call, result -> .setMethodCallHandler { call, result ->
if (call.method == "getChargeState") { if (call.method == "getChargeState") {
@@ -205,10 +247,18 @@ class MainActivity : FlutterActivity() {
} }
} }
// SAF 通道
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SAF_CHANNEL) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SAF_CHANNEL)
.setMethodCallHandler { call, result -> .setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
"restoreUri" -> {
val uriString = call.argument<String>("uri")
if (uriString != null) {
restoreUri(uriString)
result.success(true)
} else {
result.error("INVALID_ARGUMENT", "uri is required", null)
}
}
"selectFolder" -> { "selectFolder" -> {
safResult = result safResult = result
openFolderPicker() openFolderPicker()
@@ -233,7 +283,6 @@ class MainActivity : FlutterActivity() {
} }
} }
// 充电状态事件通道
EventChannel(flutterEngine.dartExecutor.binaryMessenger, EVENT_CHANNEL) EventChannel(flutterEngine.dartExecutor.binaryMessenger, EVENT_CHANNEL)
.setStreamHandler( .setStreamHandler(
object : EventChannel.StreamHandler { object : EventChannel.StreamHandler {
+15 -13
View File
@@ -1,18 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off --> <!-- 应用主主题(浅色) -->
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar"> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Show a splash screen on the activity. Automatically removed when <item name="android:statusBarColor">@android:color/white</item>
the Flutter engine draws its first frame --> <item name="android:windowLightStatusBar">true</item>
<item name="android:windowBackground">@drawable/launch_background</item> <item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:navigationBarColor">@android:color/white</item>
<item name="android:windowLightNavigationBar">true</item>
</style> </style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. --> <!-- 启动主题(用于闪屏,保持透明/深色以避免白屏闪烁) -->
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar"> <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">?android:colorBackground</item> <!-- 启动时可保持透明 -->
<item name="android:windowBackground">@android:color/white</item>
<item name="android:statusBarColor">@android:color/white</item>
<item name="android:windowLightStatusBar">true</item>
</style> </style>
</resources> </resources>
+54
View File
@@ -0,0 +1,54 @@
/// 容量提取工具
/// 支持两种日志格式:
/// 1. learned_cap21 系列):单位 µAh,除以 1000 得到 mAh
/// 🟢 优先级最高,不受 status 限制(BMS 可能在任意状态行更新此值)
/// 2. remaining_cap20 系列):仅从 status=Full 行提取,单位智能识别(µAh/mAh)
class CapacityExtractor {
static int? extractCapacity(List<String> lines) {
final validLines = lines.where((line) => line.contains('tbat=')).toList();
if (validLines.isEmpty) return null;
// ============================================================
// 🟢 第一优先级:learned_cap21 系列专属)
// 不限制 status,在所有有效行中搜索,取最新的一条
// ============================================================
final learnedReg = RegExp(r'learned_cap=(\d+)');
for (String line in validLines.reversed) {
final match = learnedReg.firstMatch(line);
if (match != null) {
final value = int.parse(match.group(1)!);
// learned_cap 单位固定为 µAh,直接除以 1000 转 mAh
return value ~/ 1000;
}
}
// ============================================================
// 🟢 第二优先级:remaining_cap20 系列回退方案)
// 仅从 status=Full 行提取(充满状态下 BMS 最准确)
// ============================================================
final fullLines = validLines
.where((line) => line.contains('status=Full'))
.toList();
if (fullLines.isEmpty) return null;
final remainingReg = RegExp(r'remaining_cap=(\d+)');
int? maxRemaining;
for (String line in fullLines) {
final match = remainingReg.firstMatch(line);
if (match != null) {
final value = int.parse(match.group(1)!);
if (maxRemaining == null || value > maxRemaining) {
maxRemaining = value;
}
}
}
if (maxRemaining == null) return null;
// 🟢 智能单位识别:如果数值大于 100000(约 100 mAh),视为 µAh 需除以 1000
if (maxRemaining > 100000) {
return maxRemaining ~/ 1000;
} else {
return maxRemaining;
}
}
}
+140
View File
@@ -0,0 +1,140 @@
import 'dart:developer' as dev;
import '../utils/time_utils.dart';
/// 充电日志解析器
class ChargeParser {
/// 解析充电数据,返回解析结果
static ParseResult parse(List<String> lines, DateTime? chargingStartTime) {
dev.log('[ChargeParser] 开始解析,共 ${lines.length}', name: 'BatteryHealth');
final validLines = _collectValidLines(lines, chargingStartTime);
if (validLines.isEmpty) {
dev.log('[ChargeParser] 无有效时间窗口内的数据行', name: 'BatteryHealth');
return ParseResult.empty();
}
// 按时间戳从新到旧排序
validLines.sort((a, b) => b['timestamp'].compareTo(a['timestamp']));
final latest = validLines.first;
final latestLine = latest['line'] as String;
final latestType = latest['type'] as String;
dev.log(
'[ChargeParser] 最新有效数据: $latestType, 时间: ${latest['timestamp']}',
name: 'BatteryHealth',
);
if (latestType == 'wls') {
return _parseWireless(latestLine);
} else {
return _parseWired(latestLine);
}
}
static List<Map<String, dynamic>> _collectValidLines(
List<String> lines,
DateTime? chargingStartTime,
) {
final result = <Map<String, dynamic>>[];
for (String line in lines) {
final timestamp = TimeUtils.parseLogTimestamp(line);
if (!TimeUtils.isLogTimestampValid(timestamp, chargingStartTime)) {
continue;
}
final isUsb =
line.contains('chg_usb') &&
(line.contains('ibus=') || line.contains('real_type='));
final isWls = line.contains('chg_wls') && line.contains('wls_online=1');
if (isUsb || isWls) {
result.add({
'line': line,
'timestamp': timestamp,
'type': isWls ? 'wls' : 'usb',
});
}
}
return result;
}
static ParseResult _parseWireless(String line) {
final typeReg = RegExp(r'wls_type=(\w+)');
final voltReg = RegExp(r'wls_volt=(\d+)');
final currReg = RegExp(r'wls_curr=(\d+)');
final wlsType = typeReg.firstMatch(line)?.group(1);
final volt = int.parse(voltReg.firstMatch(line)?.group(1) ?? '0');
final curr = int.parse(currReg.firstMatch(line)?.group(1) ?? '0');
return ParseResult(
isWireless: true,
chargeType: wlsType ?? '无线',
power: (volt * curr) / 1000000.0,
volt: volt / 1000.0,
curr: curr / 1000.0,
wiredLevel: null,
);
}
static ParseResult _parseWired(String line) {
final ibusReg = RegExp(r'ibus=(\d+)');
final vbusReg = RegExp(r'vbus=(\d+)');
final levelReg = RegExp(r'wired_level=(\d+)');
final typeReg = RegExp(r'real_type=(\w+)');
final ibus = int.parse(ibusReg.firstMatch(line)?.group(1) ?? '0');
final vbus = int.parse(vbusReg.firstMatch(line)?.group(1) ?? '0');
final realType = typeReg.firstMatch(line)?.group(1);
final level = int.parse(levelReg.firstMatch(line)?.group(1) ?? '0');
String chargeType;
if (realType == 'PPS') {
chargeType = 'PPS';
} else if (realType == 'SDP' || realType == 'FLOAT') {
chargeType = 'SDP';
} else if (level >= 3) {
chargeType = 'PPS';
} else {
chargeType = 'SDP';
}
return ParseResult(
isWireless: false,
chargeType: chargeType,
power: (vbus * ibus) / 1000000000000.0,
volt: vbus / 1000000.0,
curr: ibus / 1000000.0,
wiredLevel: chargeType == 'PPS' ? level : null,
);
}
}
class ParseResult {
final bool isWireless;
final String chargeType;
final double power;
final double volt;
final double curr;
final int? wiredLevel;
ParseResult({
required this.isWireless,
required this.chargeType,
required this.power,
required this.volt,
required this.curr,
this.wiredLevel,
});
factory ParseResult.empty() {
return ParseResult(
isWireless: false,
chargeType: '',
power: 0.0,
volt: 0.0,
curr: 0.0,
wiredLevel: null,
);
}
}
File diff suppressed because it is too large Load Diff
+107
View File
@@ -0,0 +1,107 @@
import 'dart:developer' as dev;
import '../services/database_service.dart';
import '../logic/capacity_extractor.dart';
import '../models/saf_permission_state.dart';
import '../services/saf_storage_service.dart';
/// 电池数据服务:扫描日志、更新数据库
class BatteryDataService {
final SafStorageService _safService = SafStorageService();
final DatabaseService _dbService = DatabaseService();
/// 扫描所有日志文件并更新数据库
Future<void> scanAndUpdateDB() async {
dev.log('🔵 scanAndUpdateDB 开始', name: 'BatteryHealth');
final state = await _safService.getPermissionState();
if (state != SafPermissionState.authorized) {
dev.log('🔵 scanAndUpdateDB: 未授权', name: 'BatteryHealth');
return;
}
try {
final files = await _safService.listFiles();
final filePattern = RegExp(r'mbattery_charger_log_(\d{4}_\d{2}_\d{2})$');
for (String fileName in files) {
final match = filePattern.firstMatch(fileName);
if (match == null) continue;
final dateStr = match.group(1)!.replaceAll('_', '-');
dev.log('🔵 处理文件: $fileName, 日期: $dateStr', name: 'BatteryHealth');
try {
final lines = await _safService.readLogFile(fileName);
if (lines.isEmpty) continue;
// 🟢 1.4.5 改进:使用 CapacityExtractor 提取容量(仅从 Full 状态提取)
int? cap = CapacityExtractor.extractCapacity(lines);
// 🟢 如果今天没有充满电(cap == null),尝试从昨天复制数据
if (cap == null) {
dev.log('🔵 日期 $dateStr 无充满记录,尝试复制前一天数据', name: 'BatteryHealth');
// 查询最近一条已有数据
final recentData = await _dbService.queryRecentData(1);
if (recentData.isNotEmpty) {
final yesterdayCap = recentData.first['capacity'] as int;
final yesterdayMaxVolt =
recentData.first['volt_max'] as double? ?? 0.0;
final yesterdayMinVolt =
recentData.first['volt_min'] as double? ?? 0.0;
dev.log(
'🔵 复制前一天数据: cap=$yesterdayCap, date=$dateStr',
name: 'BatteryHealth',
);
// 🟢 复制昨天的容量和电压极值到今天
await _dbService.insertOrUpdate(
dateStr,
yesterdayCap,
yesterdayMaxVolt,
yesterdayMinVolt,
);
} else {
dev.log('🔵 无历史数据可复制,跳过日期 $dateStr', name: 'BatteryHealth');
}
continue; // 已处理,跳过后续电压提取逻辑
}
// 提取电压极值(从所有有效行提取,不限于 Full)
final voltReg = RegExp(r'vbat=(\d+)');
double? maxVolt, minVolt;
for (String line in lines) {
if (!line.contains('tbat=')) continue;
final match = voltReg.firstMatch(line);
if (match != null) {
final v = int.parse(match.group(1)!) / 1000000;
if (maxVolt == null || v > maxVolt) maxVolt = v;
if (minVolt == null || v < minVolt) minVolt = v;
}
}
if (maxVolt == null || minVolt == null) continue;
await _dbService.insertOrUpdate(dateStr, cap, maxVolt, minVolt);
dev.log('🔵 插入数据库: $dateStr, cap=$cap', name: 'BatteryHealth');
} catch (e) {
dev.log('🔵 处理文件 $fileName 异常: $e', name: 'BatteryHealth');
}
}
} catch (e) {
dev.log('🔵 scanAndUpdateDB 异常: $e', name: 'BatteryHealth');
}
}
/// 获取趋势数据(最近 N 天)
Future<List<Map<String, dynamic>>> getTrendData(int days) async {
return await _dbService.queryRecentData(days);
}
/// 获取最近一条数据
Future<Map<String, dynamic>?> getLatestData() async {
final data = await _dbService.queryRecentData(1);
if (data.isNotEmpty) return data.first;
return null;
}
}
+130
View File
@@ -0,0 +1,130 @@
import 'dart:developer' as dev;
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
/// 数据库服务:管理电池数据持久化
class DatabaseService {
static Database? _database;
static const String _tableName = 'battery_data';
/// 获取数据库实例(单例)
Future<Database> get database async {
if (_database != null) return _database!;
_database = await _initDatabase();
return _database!;
}
/// 初始化数据库
Future<Database> _initDatabase() async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, 'battery_health.db');
return await openDatabase(
path,
version: 1,
onCreate: (db, version) async {
await db.execute('''
CREATE TABLE $_tableName (
date TEXT PRIMARY KEY,
capacity INTEGER NOT NULL,
volt_max REAL NOT NULL,
volt_min REAL NOT NULL,
updated_at TEXT NOT NULL
)
''');
await db.execute('''
CREATE INDEX idx_date ON $_tableName (date DESC)
''');
dev.log('✅ 数据库表创建成功', name: 'DatabaseService');
},
);
}
/// 插入或更新数据
Future<void> insertOrUpdate(
String date,
int capacity,
double voltMax,
double voltMin,
) async {
final db = await database;
final now = DateTime.now().toIso8601String();
await db.insert(_tableName, {
'date': date,
'capacity': capacity,
'volt_max': voltMax,
'volt_min': voltMin,
'updated_at': now,
}, conflictAlgorithm: ConflictAlgorithm.replace);
dev.log('📝 数据已保存: $date, cap=$capacity', name: 'DatabaseService');
}
/// 查询最近 N 条数据(按日期倒序)
Future<List<Map<String, dynamic>>> queryRecentData(int limit) async {
final db = await database;
try {
final result = await db.query(
_tableName,
orderBy: 'date DESC',
limit: limit,
);
return result;
} catch (e) {
dev.log('⚠️ 查询数据失败: $e', name: 'DatabaseService');
return [];
}
}
/// 🟢 1.4.5 新增:查询指定日期数据
Future<Map<String, dynamic>?> queryByDate(String date) async {
final db = await database;
try {
final result = await db.query(
_tableName,
where: 'date = ?',
whereArgs: [date],
);
return result.isNotEmpty ? result.first : null;
} catch (e) {
dev.log('⚠️ 查询单日数据失败: $e', name: 'DatabaseService');
return null;
}
}
/// 删除指定日期数据
Future<void> deleteByDate(String date) async {
final db = await database;
await db.delete(_tableName, where: 'date = ?', whereArgs: [date]);
}
/// 获取数据库中所有日期列表
Future<List<String>> getAllDates() async {
final db = await database;
try {
final result = await db.query(
_tableName,
columns: ['date'],
orderBy: 'date DESC',
);
return result.map((row) => row['date'] as String).toList();
} catch (e) {
return [];
}
}
/// 清空所有数据
Future<void> clearAll() async {
final db = await database;
await db.delete(_tableName);
dev.log('🗑️ 所有数据已清空', name: 'DatabaseService');
}
/// 关闭数据库
Future<void> close() async {
if (_database != null) {
await _database!.close();
_database = null;
}
}
}
+54 -7
View File
@@ -3,6 +3,8 @@ import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import '../models/saf_permission_state.dart'; import '../models/saf_permission_state.dart';
/// SAF 存储服务
/// 负责管理通过 SAF 授权的文件夹访问
class SafStorageService { class SafStorageService {
static final SafStorageService _instance = SafStorageService._internal(); static final SafStorageService _instance = SafStorageService._internal();
factory SafStorageService() => _instance; factory SafStorageService() => _instance;
@@ -14,7 +16,7 @@ class SafStorageService {
static const String _prefKeySafUri = 'saf_storage_uri'; static const String _prefKeySafUri = 'saf_storage_uri';
String? _cachedUri; String? _cachedUri;
// 🟢 初始化从 SharedPreferences 恢复 URI /// 初始化从 SharedPreferences 恢复 URI
Future<void> init() async { Future<void> init() async {
try { try {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
@@ -30,14 +32,16 @@ class SafStorageService {
} }
} }
/// 获取当前授权状态(简化版,直接根据缓存判断)
Future<SafPermissionState> getPermissionState() async { Future<SafPermissionState> getPermissionState() async {
if (_cachedUri == null || _cachedUri!.isEmpty) { if (_cachedUri == null || _cachedUri!.isEmpty) {
return SafPermissionState.unauthorized; return SafPermissionState.unauthorized;
} }
// 🟢 简化:直接返回 authorized,相信本地缓存的 URI // 直接返回 authorized,相信缓存
return SafPermissionState.authorized; return SafPermissionState.authorized;
} }
/// 请求 SAF 授权(引导用户选择文件夹)
Future<SafAuthorizationResult> requestPermission() async { Future<SafAuthorizationResult> requestPermission() async {
try { try {
final String? uriString = await _channel.invokeMethod('selectFolder'); final String? uriString = await _channel.invokeMethod('selectFolder');
@@ -62,23 +66,57 @@ class SafStorageService {
} }
} }
/// 🟢 将 URI 传递给 Kotlin 端(用于恢复 safUri
Future<bool> restoreUri() async {
if (_cachedUri == null || _cachedUri!.isEmpty) {
if (kDebugMode) {
print('[SAF] restoreUri: 缓存 URI 为空');
}
return false;
}
try {
final bool? result = await _channel.invokeMethod('restoreUri', {
'uri': _cachedUri,
});
if (result == true) {
if (kDebugMode) {
print('[SAF] restoreUri 成功: $_cachedUri');
}
return true;
} else {
if (kDebugMode) {
print('[SAF] restoreUri 失败: 返回 false');
}
return false;
}
} catch (e) {
if (kDebugMode) {
print('[SAF] restoreUri 异常: $e');
}
return false;
}
}
/// 检查指定日期的日志文件是否存在
Future<bool> logFileExists(String fileName) async { Future<bool> logFileExists(String fileName) async {
final state = await getPermissionState(); final state = await getPermissionState();
if (state != SafPermissionState.authorized) return false; if (state != SafPermissionState.authorized) return false;
try { try {
final files = await _channel.invokeMethod('listFiles'); final files = await listFiles();
if (files is List) { return files.contains(fileName);
return files.contains(fileName);
}
return false;
} catch (e) { } catch (e) {
return false; return false;
} }
} }
/// 读取日志文件内容(返回行列表)
Future<List<String>> readLogFile(String fileName) async { Future<List<String>> readLogFile(String fileName) async {
// 🟢 先尝试恢复 URI
await restoreUri();
final state = await getPermissionState(); final state = await getPermissionState();
if (state != SafPermissionState.authorized) return []; if (state != SafPermissionState.authorized) return [];
try { try {
final String? content = await _channel.invokeMethod('readLogFile', { final String? content = await _channel.invokeMethod('readLogFile', {
'fileName': fileName, 'fileName': fileName,
@@ -93,9 +131,14 @@ class SafStorageService {
} }
} }
/// 获取文件夹中的文件列表
Future<List<String>> listFiles() async { Future<List<String>> listFiles() async {
// 🟢 先尝试恢复 URI
await restoreUri();
final state = await getPermissionState(); final state = await getPermissionState();
if (state != SafPermissionState.authorized) return []; if (state != SafPermissionState.authorized) return [];
try { try {
final result = await _channel.invokeMethod('listFiles'); final result = await _channel.invokeMethod('listFiles');
if (result is List) { if (result is List) {
@@ -110,16 +153,19 @@ class SafStorageService {
} }
} }
/// 验证授权是否有效(简化版)
Future<bool> validatePermission() async { Future<bool> validatePermission() async {
final state = await getPermissionState(); final state = await getPermissionState();
return state == SafPermissionState.authorized; return state == SafPermissionState.authorized;
} }
/// 清除缓存
void _clearCache() { void _clearCache() {
_cachedUri = null; _cachedUri = null;
_saveUri(null); _saveUri(null);
} }
/// 保存 URI 到 SharedPreferences
Future<void> _saveUri(String? uri) async { Future<void> _saveUri(String? uri) async {
try { try {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
@@ -141,5 +187,6 @@ class SafStorageService {
} }
} }
/// 获取缓存的 URI(用于调试)
String? get cachedUri => _cachedUri; String? get cachedUri => _cachedUri;
} }
+53 -6
View File
@@ -1,6 +1,7 @@
import '../constants/charge_maps.dart'; import '../constants/charge_maps.dart';
class ChargeUtils { class ChargeUtils {
// ============ 无线充电(保持不变) ============
static String getDisplayPowerWireless(double power) { static String getDisplayPowerWireless(double power) {
if (power == 0) return '读取中...'; if (power == 0) return '读取中...';
const List<int> levels = [5, 7, 15, 27, 40, 50, 66]; const List<int> levels = [5, 7, 15, 27, 40, 50, 66];
@@ -12,21 +13,17 @@ class ChargeUtils {
return '${matchedLevel}W'; return '${matchedLevel}W';
} }
// ============ 原有有线逻辑(保留兼容,但主界面不再直接调用) ============
static String getDisplayPowerWired( static String getDisplayPowerWired(
double power, double power,
int? wiredLevel, int? wiredLevel,
String chargeType, String chargeType,
) { ) {
// USB 慢充锁定 7.5W
if (chargeType == "SDP") return "≈7.5W"; if (chargeType == "SDP") return "≈7.5W";
// 🟢 优先使用 wiredLevel 精确匹配
if (wiredLevel != null && ChargeMaps.wiredPower.containsKey(wiredLevel)) { if (wiredLevel != null && ChargeMaps.wiredPower.containsKey(wiredLevel)) {
double p = ChargeMaps.wiredPower[wiredLevel]!; double p = ChargeMaps.wiredPower[wiredLevel]!;
if (p > 0) return '${p.toInt()}W'; if (p > 0) return '${p.toInt()}W';
} }
// 🟢 匹配阈值收窄到 ±3W,减少跳变
double best = 0; double best = 0;
for (var entry in ChargeMaps.wiredPower.entries) { for (var entry in ChargeMaps.wiredPower.entries) {
if (entry.value <= power + 3 && entry.value > best) { if (entry.value <= power + 3 && entry.value > best) {
@@ -37,11 +34,61 @@ class ChargeUtils {
return best > 0 ? '${best.toInt()}W' : '${power.round()}W'; return best > 0 ? '${best.toInt()}W' : '${power.round()}W';
} }
// ============ 🟢 1.5.0 新增:主界面显示(实际/标定) ============
/// 示例:≈30w/65w
static String getMainDisplayPowerWired(
double power,
int? wiredLevel,
String chargeType,
) {
// USB 慢充锁定 7.5W
if (chargeType == "SDP") return "≈7.5w";
if (power == 0) return "读取中...";
final actual = power.round();
// 有档位且能查到标定值
if (wiredLevel != null && ChargeMaps.wiredPower.containsKey(wiredLevel)) {
final requested = ChargeMaps.wiredPower[wiredLevel]!.round();
return "${actual}w/${requested}w";
}
// 无档位信息,仅显示实际功率
return "${actual}w";
}
// ============ 🟢 1.5.0 新增:二级页面标定档位显示 ============
/// 示例:≈80w/L-10
static String getDetailRequestedPowerWired(
double power,
int? wiredLevel,
String chargeType,
) {
if (chargeType == "SDP") return "≈7.5w";
if (power == 0) return "读取中...";
if (wiredLevel != null && ChargeMaps.wiredPower.containsKey(wiredLevel)) {
final requested = ChargeMaps.wiredPower[wiredLevel]!.round();
return "${requested}w/L-$wiredLevel";
}
return "${power.round()}w";
}
// ============ 🟢 1.5.0 新增:二级页面实际功率显示 ============
static String getDetailActualPowerWired(double power, String chargeType) {
if (chargeType == "SDP") return "≈7.5w";
if (power == 0) return "读取中...";
return "${power.round()}w";
}
// ============ 充电类型标签(保持不变) ============
static String getChargeTypeLabel(bool isWireless, String chargeType) { static String getChargeTypeLabel(bool isWireless, String chargeType) {
if (isWireless) { if (isWireless) {
return chargeType == "无线" ? "无线" : chargeType; return chargeType == "无线" ? "无线" : chargeType;
} }
// 🟢 空类型显示"识别中...",比"等待log信息"更友好
if (chargeType.isEmpty) { if (chargeType.isEmpty) {
return "识别中..."; return "识别中...";
} }
+46
View File
@@ -0,0 +1,46 @@
class TimeUtils {
/// 解析日志行中的时间戳,强制解析为 UTC
static DateTime? parseLogTimestamp(String line) {
final reg = RegExp(r'\[(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})\]');
final match = reg.firstMatch(line);
if (match == null) return null;
try {
final dateParts = match.group(1)!.split('-');
final timeParts = match.group(2)!.split(':');
return DateTime.utc(
int.parse(dateParts[0]),
int.parse(dateParts[1]),
int.parse(dateParts[2]),
int.parse(timeParts[0]),
int.parse(timeParts[1]),
int.parse(timeParts[2]),
);
} catch (_) {
return null;
}
}
/// 判断日志时间戳是否在有效窗口内
static bool isLogTimestampValid(
DateTime? logTime,
DateTime? chargingStartTime,
) {
if (logTime == null) return false;
if (chargingStartTime == null) {
final nowUtc = DateTime.now().toUtc();
final diff = nowUtc.difference(logTime);
return diff.abs().inMinutes <= 3;
}
final endTime = chargingStartTime.add(const Duration(minutes: 3));
return logTime.isAfter(chargingStartTime) && logTime.isBefore(endTime);
}
/// 生成今日日志文件名(UTC
static String getTodayFileName() {
final nowUtc = DateTime.now().toUtc();
final year = nowUtc.year.toString();
final month = nowUtc.month.toString().padLeft(2, '0');
final day = nowUtc.day.toString().padLeft(2, '0');
return 'mbattery_charger_log_${year}_${month}_$day';
}
}
+135
View File
@@ -0,0 +1,135 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
/// 电池信息面板(电压 + 温度 + 极值)
class BatteryInfoPanel extends StatelessWidget {
final double batteryVolt;
final double batteryTemp;
final double maxVolt;
final double minVolt;
final double maxTemp;
final double minTemp;
const BatteryInfoPanel({
super.key,
required this.batteryVolt,
required this.batteryTemp,
required this.maxVolt,
required this.minVolt,
required this.maxTemp,
required this.minTemp,
});
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildInfoItem(
_buildIcon('voltage'),
'电压',
'${batteryVolt.toStringAsFixed(3)}V',
),
const SizedBox(height: 8),
Row(
children: [
Text(
'${maxVolt.toStringAsFixed(3)}V',
style: const TextStyle(
color: Colors.red,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
const SizedBox(width: 12),
Text(
'${minVolt.toStringAsFixed(3)}V',
style: const TextStyle(
color: Colors.blue,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
],
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildInfoItem(
_buildIcon('temperature'),
'温度',
'${batteryTemp.toStringAsFixed(0)}°C',
),
const SizedBox(height: 8),
Row(
children: [
Text(
'${maxTemp.toStringAsFixed(0)}°C',
style: const TextStyle(
color: Colors.red,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
const SizedBox(width: 12),
Text(
'${minTemp.toStringAsFixed(0)}°C',
style: const TextStyle(
color: Colors.blue,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
],
),
],
),
),
],
);
}
Widget _buildIcon(String type) {
switch (type) {
case 'voltage':
return SvgPicture.asset(
'assets/icons/si--lightning-fill.svg',
width: 18,
height: 18,
colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.srcIn),
);
case 'temperature':
return SvgPicture.asset(
'assets/icons/mdi--temperature.svg',
width: 18,
height: 18,
colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.srcIn),
);
default:
return const Icon(Icons.help_outline, size: 18, color: Colors.grey);
}
}
Widget _buildInfoItem(Widget icon, String label, String value) {
return Row(
children: [
icon,
const SizedBox(width: 6),
Text(label, style: const TextStyle(fontSize: 14, color: Colors.grey)),
const SizedBox(width: 6),
Text(
value,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
),
],
);
}
}
+101
View File
@@ -0,0 +1,101 @@
import 'package:flutter/material.dart';
/// 电池健康度摘要组件(健康度 + cycles + mAh
class BatterySummary extends StatelessWidget {
final double healthPercent;
final int cycleCount;
final double currentCap;
final int designCap;
const BatterySummary({
super.key,
required this.healthPercent,
required this.cycleCount,
required this.currentCap,
required this.designCap,
});
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
// 健康度大数字
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
healthPercent.toStringAsFixed(1),
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w500,
height: 1.0,
color: healthPercent < 70 ? Colors.red : Colors.black,
),
),
const SizedBox(width: 2),
Text(
'%',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
height: 1.0,
color: healthPercent < 70 ? Colors.red : Colors.grey.shade600,
),
),
],
),
const Spacer(),
// cycles + mAh
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'$cycleCount cycles', // ✅ 修正:去掉下划线,使用 cycleCount
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
height: 1.2,
),
),
const SizedBox(height: 4),
Text.rich(
TextSpan(
children: [
TextSpan(
text: currentCap.toStringAsFixed(0),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
),
),
TextSpan(
text: ' / ',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
color: Colors.grey.shade500,
),
),
TextSpan(
text: '${designCap}mAh',
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
),
),
],
),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
height: 1.0,
),
),
],
),
],
);
}
}
+74 -42
View File
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../utils/charge_utils.dart'; import '../utils/charge_utils.dart';
/// 充电详情页 /// 充电详情页
@@ -11,65 +12,96 @@ class ChargeDetailPage extends StatelessWidget {
final int? wiredLevel; final int? wiredLevel;
const ChargeDetailPage({ const ChargeDetailPage({
super.key, Key? key,
required this.chargeType, required this.chargeType,
required this.isWireless, required this.isWireless,
required this.power, required this.power,
required this.volt, required this.volt,
required this.curr, required this.curr,
this.wiredLevel, this.wiredLevel,
}); }) : super(key: key);
String _getDisplayPower() {
if (isWireless) {
return ChargeUtils.getDisplayPowerWireless(power);
} else {
return ChargeUtils.getDisplayPowerWired(power, wiredLevel, chargeType);
}
}
String _getProtocolDisplay() { String _getProtocolDisplay() {
if (isWireless) return '无线充电($chargeType'; if (isWireless) return '无线充电($chargeType';
if (chargeType == 'PPS') return 'PPS 快充'; if (chargeType == 'PPS') return 'PPS 快充';
if (chargeType == 'SDP') return 'USB 充电'; // 🟢 改成和主页面一致 if (chargeType == 'SDP') return 'USB 充电';
return chargeType; return chargeType;
} }
String _getRequestedDisplay() {
return ChargeUtils.getDetailRequestedPowerWired(
power,
wiredLevel,
chargeType,
);
}
String _getActualDisplay() {
return ChargeUtils.getDetailActualPowerWired(power, chargeType);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return AnnotatedRegion<SystemUiOverlayStyle>(
appBar: AppBar( // 🟢 1.5.0 与主界面统一状态栏样式
title: const Text('充电详情'), value: const SystemUiOverlayStyle(
backgroundColor: const Color.fromARGB(0, 255, 255, 255), statusBarColor: Color.fromRGBO(157, 212, 237, 0.5),
elevation: 0, statusBarIconBrightness: Brightness.dark,
foregroundColor: Colors.black, statusBarBrightness: Brightness.dark,
iconTheme: const IconThemeData(color: Colors.black),
), ),
body: Padding( child: Scaffold(
padding: const EdgeInsets.all(20.0), backgroundColor: Colors.white,
child: Column( appBar: AppBar(
crossAxisAlignment: CrossAxisAlignment.start, title: const Text('充电详情'),
children: [ backgroundColor: Colors.white, // 🟢 与主界面统一
_buildDetailRow('协议', _getProtocolDisplay()), elevation: 0,
const Divider(), scrolledUnderElevation: 0,
_buildDetailRow('功率', _getDisplayPower()), shadowColor: Colors.transparent,
const Divider(), surfaceTintColor: Colors.transparent,
_buildDetailRow('电压', '${volt.toStringAsFixed(2)}V'), foregroundColor: Colors.black,
const Divider(), iconTheme: const IconThemeData(color: Colors.black),
_buildDetailRow('电流', '${curr.toStringAsFixed(2)}A'), ),
const Divider(), body: Padding(
if (!isWireless && wiredLevel != null) padding: const EdgeInsets.all(20.0),
_buildDetailRow('档位', '$wiredLevel'), child: Column(
if (!isWireless && wiredLevel == null) _buildDetailRow('档位', '自动'), crossAxisAlignment: CrossAxisAlignment.start,
if (isWireless) _buildDetailRow('类型', '无线'), children: [
const Spacer(), _buildDetailRow('协议', _getProtocolDisplay()),
Center( const Divider(),
child: Text(
'数据来自 BMS 实时日志', // 有线充电:标定档位 + 实际功率
style: TextStyle(fontSize: 12, color: Colors.grey.shade500), if (!isWireless) ...[
_buildDetailRow('标定档位', _getRequestedDisplay()),
const Divider(),
_buildDetailRow('实际功率', _getActualDisplay()),
const Divider(),
] else ...[
_buildDetailRow(
'功率',
ChargeUtils.getDisplayPowerWireless(power),
),
const Divider(),
],
_buildDetailRow('电压', '${volt.toStringAsFixed(2)}V'),
const Divider(),
_buildDetailRow('电流', '${curr.toStringAsFixed(2)}A'),
const Divider(),
// 辅助信息
if (isWireless) _buildDetailRow('类型', '无线'),
if (!isWireless && wiredLevel == null)
_buildDetailRow('档位', '自动'),
const Spacer(),
Center(
child: Text(
'数据来自 BMS 实时日志',
style: TextStyle(fontSize: 12, color: Colors.grey.shade500),
),
), ),
), ],
], ),
), ),
), ),
); );
+56
View File
@@ -0,0 +1,56 @@
import 'package:flutter/material.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) {
return GestureDetector(
onTap: isCharging ? onTap : null,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
summary,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.black87,
),
),
Row(
children: [
Text(
'充电信息',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.grey.shade700,
),
),
const SizedBox(width: 4),
Text(
'>',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.grey.shade700,
),
),
],
),
],
),
);
}
}
+222
View File
@@ -0,0 +1,222 @@
import 'package:flutter/material.dart';
// --- 潘通色定义 ---
const Color kHealthGreen = Color(0xFF00B140);
const Color kHealthYellow = Color(0xFFEFDF00);
const Color kHealthOrange = Color(0xFFD75A16);
/// 趋势图柱状图组件
class TrendChart extends StatelessWidget {
final List<Map<String, dynamic>> data;
final int designCap;
const TrendChart({super.key, required this.data, required this.designCap});
@override
Widget build(BuildContext context) {
List<Map<String, dynamic>> displayData = data.isNotEmpty
? data
: _getMockData();
List<String> dates = displayData.map((e) {
String date = e['date'] as String;
var parts = date.split('-');
return '${parts[1]}/${parts[2]}';
}).toList();
List<double> percents = displayData.map((e) {
int cap = e['capacity'] as int;
return (cap / designCap) * 100;
}).toList();
while (percents.length < 15) {
percents.insert(0, 0);
dates.insert(0, '');
}
if (percents.length > 15) {
percents = percents.sublist(percents.length - 15);
dates = dates.sublist(dates.length - 15);
}
int todayIndex = percents.length - 1;
int sevenDaysAgoIndex = todayIndex - 7;
bool showSevenDaysAgo = sevenDaysAgoIndex >= 0;
String sevenDaysAgoDate = showSevenDaysAgo ? dates[sevenDaysAgoIndex] : '';
String todayDate = dates[todayIndex];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'容量百分比变动趋势(近15天)',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
),
const SizedBox(height: 8),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 180,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
Text(
'100',
style: TextStyle(
fontSize: 10,
color: Colors.grey,
height: 1.0,
),
),
Text(
'70',
style: TextStyle(
fontSize: 10,
color: Colors.grey,
height: 1.0,
),
),
],
),
),
const SizedBox(width: 4),
Expanded(
child: LayoutBuilder(
builder: (context, constraints) {
double totalWidth = constraints.maxWidth;
double step = totalWidth / 15;
return SizedBox(
height: 180,
child: Stack(
children: [
Padding(
padding: const EdgeInsets.only(bottom: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: List.generate(percents.length, (index) {
double value = percents[index];
Color barColor;
if (value > 0) {
if (value >= 80) {
barColor = kHealthGreen;
} else if (value >= 70) {
barColor = kHealthYellow;
} else {
barColor = Colors.red;
}
} else {
barColor = Colors.grey.shade300;
}
double clampedValue = value.clamp(70.0, 100.0);
double heightFactor =
(clampedValue - 70.0) / 30.0;
return Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 2,
),
height: 20 + (100 * heightFactor),
width: 18,
decoration: BoxDecoration(
color: barColor,
borderRadius: BorderRadius.circular(4),
),
),
),
);
}),
),
),
if (showSevenDaysAgo && sevenDaysAgoDate.isNotEmpty)
Positioned(
left: sevenDaysAgoIndex * step + (step / 2) - 14,
bottom: 2,
child: Text(
sevenDaysAgoDate,
style: const TextStyle(
fontSize: 9,
color: Colors.grey,
height: 1.0,
),
),
),
if (todayDate.isNotEmpty)
Positioned(
left: todayIndex * step + (step / 2) - 14,
bottom: 2,
child: Text(
todayDate,
style: const TextStyle(
fontSize: 9,
color: Colors.black,
height: 1.0,
fontWeight: FontWeight.w600,
),
),
),
],
),
);
},
),
),
],
),
if (percents.every((v) => v == 0))
const Padding(
padding: EdgeInsets.only(top: 8.0),
child: Text(
'暂无历史数据,请点击刷新按钮更新',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
),
],
);
}
List<Map<String, dynamic>> _getMockData() {
List<String> mockDates = [
'2026-06-14',
'2026-06-15',
'2026-06-16',
'2026-06-17',
'2026-06-18',
'2026-06-19',
'2026-06-20',
'2026-06-21',
'2026-06-22',
'2026-06-23',
'2026-06-24',
'2026-06-25',
'2026-06-26',
'2026-06-27',
'2026-06-28',
];
List<int> mockCap = [
4635,
4632,
4628,
4625,
4620,
4618,
4615,
4610,
4608,
4605,
4600,
4598,
4595,
4590,
4585,
];
return List.generate(
15,
(i) => {'date': mockDates[i], 'capacity': mockCap[i]},
);
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.3.0-dev.4 version: 1.5.0
environment: environment:
sdk: ^3.9.2 sdk: ^3.9.2