From b6ce5c498611e66a720c4990775f11a5060c670a Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Wed, 4 Mar 2026 11:08:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E8=A1=A8=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/running_status_page.dart | 535 +++++++++++++----- pubspec.lock | 20 +- pubspec.yaml | 3 +- 3 files changed, 406 insertions(+), 152 deletions(-) diff --git a/lib/features/home/presentation/pages/running_status_page.dart b/lib/features/home/presentation/pages/running_status_page.dart index 7216d8ed..baadaa01 100644 --- a/lib/features/home/presentation/pages/running_status_page.dart +++ b/lib/features/home/presentation/pages/running_status_page.dart @@ -1,8 +1,10 @@ import 'dart:async'; import 'dart:convert'; +import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:syncfusion_flutter_gauges/gauges.dart'; import '../../../../core/app/app_user_cubit.dart'; import '../../../../core/di/injection.dart'; @@ -21,6 +23,41 @@ class RunningStatusPage extends StatefulWidget { class _RunningStatusPageState extends State { bool _isCardView = true; + bool _chipGaugeMode = true; + + // 图表历史数据 + final List _leftMeasureHistory = []; + final List _rightMeasureHistory = []; + final List _leftTargetHistory = []; + final List _rightTargetHistory = []; + final List _leftCurrentHistory = []; + final List _rightCurrentHistory = []; + final List _leftTempHistory = []; + final List _rightTempHistory = []; + final List _knifeHistory = []; + final List _chipTempHistory = []; + final List _voltageHistory = []; + + double _timeIndex = 0; + + // 限制列表长度,防止数据过多 + void _limit(List list, {int max = 30}) { + if (list.length > max) list.removeAt(0); + } + + // 刷新数据方法(移到正确位置) + void _refreshDeviceData() { + final deviceState = context.read().state; + final currentDevice = deviceState.selectedDevice; + + if (currentDevice != null) { + // 触发Bloc刷新数据 + // context.read().add(RefreshDeviceStatus(deviceId: currentDevice.id)); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("数据刷新成功!"), duration: Duration(seconds: 1))); + } else { + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("暂无设备,无法刷新"), duration: Duration(seconds: 1))); + } + } // 解析定位质量数值为文字描述 String _parseLocationQuality(int locationValue) { @@ -36,161 +73,209 @@ class _RunningStatusPageState extends State { case 5: return 'RTK 浮点解'; default: - return '未知($locationValue)'; // 处理未定义的数值,避免显示空值 + return '未知($locationValue)'; } } - @override - Widget build(BuildContext context) { - final deviceState = context.read().state; - final currentDevice = deviceState.selectedDevice; + // 追加图表数据 + void _appendChartData(DeviceStatusUpdated state) { + final status = state.status; - if (currentDevice == null) { - return const Scaffold(body: Center(child: Text("加载中..."))); - } + // 添加速度数据 + _leftTargetHistory.add(FlSpot(_timeIndex, status.leftTargetSpeed)); + _rightTargetHistory.add(FlSpot(_timeIndex, status.rightTargetSpeed)); + _leftMeasureHistory.add(FlSpot(_timeIndex, status.leftMeasureSpeed)); + _rightMeasureHistory.add(FlSpot(_timeIndex, status.rightMeasureSpeed)); - return Scaffold( - backgroundColor: const Color(0xFFF7F7F7), - body: CustomScrollView( - physics: const BouncingScrollPhysics(), - slivers: [ - SliverAppBar( - backgroundColor: const Color(0xFF1677FF), - pinned: true, - centerTitle: true, - elevation: 0, - title: const Text( - "设备数据监控", - style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold), - ), - leading: IconButton( - icon: const Icon(Icons.arrow_back, color: Colors.white), - onPressed: () => Navigator.pop(context), - ), - ), + // 添加电流数据 + _leftCurrentHistory.add(FlSpot(_timeIndex, status.leftCurrent)); + _rightCurrentHistory.add(FlSpot(_timeIndex, status.rightCurrent)); - BlocBuilder( - builder: (context, state) { - String qual = '--'; - String satelliteCnt = '--'; - String headingStatus = "--"; + // 添加温度数据 + _leftTempHistory.add(FlSpot(_timeIndex, status.leftMotorTemp)); + _rightTempHistory.add(FlSpot(_timeIndex, status.rightMotorTemp)); - if (state is DeviceStatusUpdated) { - headingStatus = state.status.headingStatus == 0 ? '未初始化' : '已初始化'; - // 强制转换为整型并解析定位质量 - int qualValue = 0; - try { - qualValue = int.parse(state.status.qual.toString()); - } catch (e) { - qualValue = 0; - } - qual = _parseLocationQuality(qualValue); - satelliteCnt = state.status.satelliteCnt.toString(); - } else if (state is DeviceStatusError) { - qual = '-'; - satelliteCnt = '-'; - headingStatus = '-'; - } + // 添加割刀速度 + _knifeHistory.add(FlSpot(_timeIndex, double.tryParse(status.knifeCuttingSpeed) ?? 0)); - return SliverToBoxAdapter( - child: Container( - color: Colors.white, - padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), // 减小内边距 - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - // 优化1:减小字体大小到12,设置最大宽度,防止溢出 - Expanded( - flex: 3, - child: Text("航向角状态:$headingStatus", style: const TextStyle(fontSize: 12), maxLines: 1, overflow: TextOverflow.ellipsis), - ), - // 优化2:定位质量列,减小字体,设置最大宽度 - Expanded( - flex: 4, - child: Text( - "定位质量:$qual", - style: const TextStyle(fontSize: 12), - maxLines: 1, - overflow: TextOverflow.ellipsis, - textAlign: TextAlign.center, - ), - ), - // 优化3:卫星数列,减小字体,设置最大宽度 - Expanded( - flex: 2, - child: Text( - "卫星数:$satelliteCnt", - style: const TextStyle(fontSize: 12), - maxLines: 1, - overflow: TextOverflow.ellipsis, - textAlign: TextAlign.right, - ), - ), - ], - ), - ), - ); - }, - ), + // 添加芯片温度和电压 + _chipTempHistory.add(FlSpot(_timeIndex, status.chipTemp)); + _voltageHistory.add(FlSpot(_timeIndex, double.tryParse(status.battery) ?? 0)); - SliverToBoxAdapter( - child: Container( - color: Colors.white, - margin: const EdgeInsets.only(top: 8), - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Row( - children: [ - GestureDetector( - onTap: () => setState(() => _isCardView = true), - child: _buildTab("卡片", isActive: _isCardView), - ), - const SizedBox(width: 24), - GestureDetector( - onTap: () => setState(() => _isCardView = false), - child: _buildTab("图表", isActive: !_isCardView), - ), - ], - ), - ElevatedButton( - style: ElevatedButton.styleFrom( - backgroundColor: const Color(0xFF1677FF), - foregroundColor: Colors.white, - shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), - ), - onPressed: _refreshDeviceData, - child: const Text("刷新数据", style: TextStyle(fontSize: 14)), - ), - ], + _timeIndex += 1; + + // 限制数据长度 + _limit(_leftTargetHistory); + _limit(_rightTargetHistory); + _limit(_leftMeasureHistory); + _limit(_rightMeasureHistory); + _limit(_leftCurrentHistory); + _limit(_rightCurrentHistory); + _limit(_leftTempHistory); + _limit(_rightTempHistory); + _limit(_knifeHistory); + _limit(_chipTempHistory); + _limit(_voltageHistory); + } + + // 构建间距组件 + Widget _gap({double height = 20}) { + return SizedBox(height: height); + } + + // 构建折线图 + Widget _buildLineChart({required String title, required List lines, double? minY, double? maxY}) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), + const SizedBox(height: 12), + SizedBox( + height: 220, + child: LineChart( + LineChartData( + minY: minY, + maxY: maxY, + lineBarsData: lines, + gridData: const FlGridData(show: true), + borderData:FlBorderData(show: true), + titlesData: const FlTitlesData( + leftTitles: AxisTitles(sideTitles: SideTitles(showTitles: true)), + bottomTitles: AxisTitles(sideTitles: SideTitles(showTitles: true)), + rightTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)), + topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)), ), ), ), + ), + ], + ); + } - SliverFillRemaining( - child: BlocBuilder( - builder: (context, state) { - return Container( - margin: const EdgeInsets.all(16), - decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)), - child: _isCardView ? _buildCardContentView(state) : _buildChartContentView(), - ); - }, - ), + // 构建折线 + LineChartBarData _line(List data, Color color) { + return LineChartBarData( + spots: data, + color: color, + isCurved: true, + barWidth: 2, + dotData: const FlDotData(show: false), + belowBarData: BarAreaData(show: false), + ); + } + + // 左右轮测量速度图表 + Widget _buildSpeedMeasureChart() { + return _buildLineChart( + title: "左右轮测量速度 (rpm)", + minY: -3000, + maxY: 3000, + lines: [_line(_leftMeasureHistory, Colors.blue), _line(_rightMeasureHistory, Colors.red)], + ); + } + + // 左右轮目标速度图表 + Widget _buildSpeedTargetChart() { + return _buildLineChart( + title: "左右轮目标速度 (rpm)", + minY: -3000, + maxY: 3000, + lines: [_line(_leftTargetHistory, Colors.green), _line(_rightTargetHistory, Colors.orange)], + ); + } + + // 电流图表 + Widget _buildCurrentChart() { + return _buildLineChart( + title: "左右轮电流 (A)", + minY: 0, + maxY: 100, + lines: [_line(_leftCurrentHistory, Colors.purple), _line(_rightCurrentHistory, Colors.teal)], + ); + } + + // 电机温度图表 + Widget _buildMotorTempChart() { + return _buildLineChart( + title: "左右轮电机温度 (°C)", + minY: 0, + maxY: 120, + lines: [_line(_leftTempHistory, Colors.deepOrange), _line(_rightTempHistory, Colors.brown)], + ); + } + + // 割刀速度图表 + Widget _buildKnifeSpeedChart() { + return _buildLineChart(title: "割刀速度 (rpm)", minY: 0, maxY: 3000, lines: [_line(_knifeHistory, Colors.indigo)]); + } + + // 芯片温度/电压面板 + Widget _buildChipTempVoltagePanel(DeviceStatusUpdated state) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + const Text("芯片温度 / 电压", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), + Switch(value: _chipGaugeMode, onChanged: (v) => setState(() => _chipGaugeMode = v)), + ], + ), + const SizedBox(height: 16), + _chipGaugeMode + ? Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [_buildGauge(state.status.chipTemp, 0, 120, "芯片温度 (°C)"), _buildGauge(double.parse(state.status.battery), 0, 100, "电量 (%)")], + ) + : _buildLineChart(title: "最近30次数据", lines: [_line(_chipTempHistory, Colors.red), _line(_voltageHistory, Colors.blue)]), + ], + ); + } + + // 航向角仪表盘 + Widget _buildHeadingGauge(DeviceStatusUpdated state) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text("航向角", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), + const SizedBox(height: 16), + Center(child: _buildGauge(state.status.yaw, 0, 360, "航向角 (°)")), + ], + ); + } + + // 构建仪表盘 + Widget _buildGauge(double value, double min, double max, String label) { + return SizedBox( + width: 150, + height: 150, + child: SfRadialGauge( + title: GaugeTitle(text: label), + axes: [ + RadialAxis( + minimum: min, + maximum: max, + pointers: [NeedlePointer(value: value)], + annotations: [ + GaugeAnnotation( + widget: Text(value.toStringAsFixed(1), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), + angle: 90, + positionFactor: 0.5, + ), + ], ), ], ), ); } + // 构建选项卡 Widget _buildTab(String title, {required bool isActive}) { return Text( title, style: TextStyle( color: isActive ? const Color(0xFF1677FF) : const Color(0xFF666666), - fontSize: 16, // 选项卡字体从18减小到16 + fontSize: 16, fontWeight: isActive ? FontWeight.bold : FontWeight.normal, decoration: isActive ? TextDecoration.underline : null, decorationColor: const Color(0xFF1677FF), @@ -202,14 +287,13 @@ class _RunningStatusPageState extends State { // 电机参数表格 Widget _buildMotorTable(DeviceStatusUpdated state) { final status = state.status; - print(state); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Padding( padding: EdgeInsets.symmetric(vertical: 12), - child: Text("电机参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), // 从20减小到18 + child: Text("电机参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), ), Table( border: TableBorder.all(color: const Color(0xFFE5E5E5)), @@ -262,7 +346,7 @@ class _RunningStatusPageState extends State { children: [ const Padding( padding: EdgeInsets.symmetric(vertical: 12), - child: Text("其他参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), // 从20减小到18 + child: Text("其他参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), ), Table( border: TableBorder.all(color: const Color(0xFFE5E5E5)), @@ -286,15 +370,16 @@ class _RunningStatusPageState extends State { ); } + // 表格单元格 Widget _buildTableCell(String text, {bool isHeader = false}) { return TableCell( child: Padding( - padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 6), // 减小表格内边距 + padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 6), child: Text( text, textAlign: TextAlign.center, style: TextStyle( - fontSize: 14, // 表格字体从16减小到14 + fontSize: 14, color: isHeader ? const Color(0xFF333333) : const Color(0xFF666666), fontWeight: isHeader ? FontWeight.bold : FontWeight.normal, ), @@ -303,6 +388,7 @@ class _RunningStatusPageState extends State { ); } + // 卡片视图 Widget _buildCardContentView(DeviceStatusState state) { if (state is DeviceStatusUpdated) { return SingleChildScrollView( @@ -318,29 +404,180 @@ class _RunningStatusPageState extends State { } } - Widget _buildChartContentView() { - return const Center( + // 图表视图 + Widget _buildChartContentView(DeviceStatusState state) { + if (state is! DeviceStatusUpdated) { + return const Center(child: CircularProgressIndicator()); + } + + return SingleChildScrollView( + padding: const EdgeInsets.all(16), child: Column( - mainAxisAlignment: MainAxisAlignment.center, children: [ - Text("图表视图", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), - SizedBox(height: 16), - Text("可接入 fl_chart / charts_flutter 实现折线图/柱状图", style: TextStyle(color: Color(0xFF999999), fontSize: 14)), + _buildSpeedMeasureChart(), + _gap(), + _buildSpeedTargetChart(), + _gap(), + _buildCurrentChart(), + _gap(), + _buildMotorTempChart(), + _gap(), + _buildKnifeSpeedChart(), + _gap(), + _buildChipTempVoltagePanel(state), + _gap(), + _buildHeadingGauge(state), ], ), ); } - void _refreshDeviceData() { + @override + Widget build(BuildContext context) { final deviceState = context.read().state; final currentDevice = deviceState.selectedDevice; - if (currentDevice != null) { - // 这里可以触发 Bloc 事件来刷新数据 - // context.read().add(RefreshDeviceStatus(deviceId: currentDevice.id)); - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("数据刷新成功!"), duration: Duration(seconds: 1))); - } else { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("暂无设备,无法刷新"), duration: Duration(seconds: 1))); + if (currentDevice == null) { + return const Scaffold(body: Center(child: Text("加载中..."))); } + + return Scaffold( + backgroundColor: const Color(0xFFF7F7F7), + body: CustomScrollView( + physics: const BouncingScrollPhysics(), + slivers: [ + SliverAppBar( + backgroundColor: const Color(0xFF1677FF), + pinned: true, + centerTitle: true, + elevation: 0, + title: const Text( + "设备数据监控", + style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold), + ), + leading: IconButton( + icon: const Icon(Icons.arrow_back, color: Colors.white), + onPressed: () => Navigator.pop(context), + ), + ), + + // 顶部状态信息 + BlocBuilder( + builder: (context, state) { + String qual = '--'; + String satelliteCnt = '--'; + String headingStatus = "--"; + + if (state is DeviceStatusUpdated) { + headingStatus = state.status.headingStatus == 0 ? '未初始化' : '已初始化'; + int qualValue = 0; + try { + qualValue = int.parse(state.status.qual.toString()); + } catch (e) { + qualValue = 0; + } + qual = _parseLocationQuality(qualValue); + satelliteCnt = state.status.satelliteCnt.toString(); + } else if (state is DeviceStatusError) { + qual = '-'; + satelliteCnt = '-'; + headingStatus = '-'; + } + + return SliverToBoxAdapter( + child: Container( + color: Colors.white, + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + flex: 3, + child: Text("航向角状态:$headingStatus", style: const TextStyle(fontSize: 12), maxLines: 1, overflow: TextOverflow.ellipsis), + ), + Expanded( + flex: 4, + child: Text( + "定位质量:$qual", + style: const TextStyle(fontSize: 12), + maxLines: 1, + overflow: TextOverflow.ellipsis, + textAlign: TextAlign.center, + ), + ), + Expanded( + flex: 2, + child: Text( + "卫星数:$satelliteCnt", + style: const TextStyle(fontSize: 12), + maxLines: 1, + overflow: TextOverflow.ellipsis, + textAlign: TextAlign.right, + ), + ), + ], + ), + ), + ); + }, + ), + + // 切换选项卡 + SliverToBoxAdapter( + child: Container( + color: Colors.white, + margin: const EdgeInsets.only(top: 8), + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Row( + children: [ + GestureDetector( + onTap: () => setState(() => _isCardView = true), + child: _buildTab("卡片", isActive: _isCardView), + ), + const SizedBox(width: 24), + GestureDetector( + onTap: () => setState(() => _isCardView = false), + child: _buildTab("图表", isActive: !_isCardView), + ), + ], + ), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: const Color(0xFF1677FF), + foregroundColor: Colors.white, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + ), + onPressed: _refreshDeviceData, + child: const Text("刷新数据", style: TextStyle(fontSize: 14)), + ), + ], + ), + ), + ), + + // 主要内容区域(修复Sliver嵌套问题) + SliverToBoxAdapter( + child: BlocBuilder( + builder: (context, state) { + // 追加图表数据 + if (state is DeviceStatusUpdated) { + _appendChartData(state); + } + + return Container( + margin: const EdgeInsets.all(16), + decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)), + child: _isCardView ? _buildCardContentView(state) : _buildChartContentView(state), + ); + }, + ), + ), + ], + ), + ); } } diff --git a/pubspec.lock b/pubspec.lock index 55096722..eeee8fb1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -348,10 +348,10 @@ packages: dependency: "direct main" description: name: fl_chart - sha256: d0f0d49112f2f4b192481c16d05b6418bd7820e021e265a3c22db98acf7ed7fb + sha256: "00b74ae680df6b1135bdbea00a7d1fc072a9180b7c3f3702e4b19a9943f5ed7d" url: "https://pub.flutter-io.cn" source: hosted - version: "0.68.0" + version: "0.66.2" flutter: dependency: "direct main" description: flutter @@ -1188,6 +1188,22 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.4.1" + syncfusion_flutter_core: + dependency: transitive + description: + name: syncfusion_flutter_core + sha256: "080c4a57f12cb03869c863f6b33fb170ce9653ac741281f11ff8e067dc96fe99" + url: "https://pub.flutter-io.cn" + source: hosted + version: "32.2.8" + syncfusion_flutter_gauges: + dependency: "direct main" + description: + name: syncfusion_flutter_gauges + sha256: "11ec00997cf37c5b02c00689b7083875f0f77561b43fbe760997eda30a321a99" + url: "https://pub.flutter-io.cn" + source: hosted + version: "32.2.8" synchronized: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 69804f80..25eca93d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -118,7 +118,8 @@ dependencies: # 高德地图需要的权限 permission_handler: ^12.0.1 # 图表 - fl_chart: ^0.68.0 + fl_chart: ^0.66.0 + syncfusion_flutter_gauges: ^32.2.8 #qr_code_scanner: ^1.0.1 # 用于扫描二维码