Files
flutterApp/lib/features/home/presentation/pages/running_status_page.dart

1131 lines
42 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2026-03-04 11:21:12 +08:00
import 'dart:math' as math;
2026-03-04 11:08:50 +08:00
import 'package:fl_chart/fl_chart.dart';
2026-01-18 20:21:14 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2026-03-04 11:08:50 +08:00
import 'package:syncfusion_flutter_gauges/gauges.dart';
2026-03-04 13:51:27 +08:00
import 'package:flutter/animation.dart';
2026-01-18 20:21:14 +08:00
import '../../../../core/app/app_user_cubit.dart';
import '../../../../core/di/injection.dart';
import '../../../../core/network/net_message_dispatcher.dart';
import '../../../../core/network/protocol_decoder.dart';
2026-01-18 20:21:14 +08:00
import '../../../devices/presentation/bloc/devices_cubit.dart';
2026-03-04 10:09:58 +08:00
import '../../../devices/presentation/bloc/device_status_bloc.dart';
import '../../../devices/presentation/bloc/device_status_state.dart';
2026-01-18 20:21:14 +08:00
class RunningStatusPage extends StatefulWidget {
2026-01-18 20:21:14 +08:00
const RunningStatusPage({super.key});
@override
State<RunningStatusPage> createState() => _RunningStatusPageState();
}
class _RunningStatusPageState extends State<RunningStatusPage> {
bool _isCardView = true;
2026-03-04 11:08:50 +08:00
2026-03-04 13:25:20 +08:00
// 仪表盘/折线图切换状态
bool _voltageGaugeMode = true;
bool _chipTempGaugeMode = true;
bool _knifeSpeedGaugeMode = false;
// 历史数据
2026-03-04 11:08:50 +08:00
final List<FlSpot> _leftMeasureHistory = [];
final List<FlSpot> _rightMeasureHistory = [];
final List<FlSpot> _leftTargetHistory = [];
final List<FlSpot> _rightTargetHistory = [];
final List<FlSpot> _leftCurrentHistory = [];
final List<FlSpot> _rightCurrentHistory = [];
final List<FlSpot> _leftTempHistory = [];
final List<FlSpot> _rightTempHistory = [];
final List<FlSpot> _knifeHistory = [];
final List<FlSpot> _chipTempHistory = [];
final List<FlSpot> _voltageHistory = [];
double _timeIndex = 0;
2026-03-04 11:21:12 +08:00
// 兼容低版本的 takeLast 方法
List<T> _takeLast<T>(List<T> list, int count) {
if (list.length <= count) {
return List.from(list);
}
return list.sublist(list.length - count);
}
2026-03-04 11:08:50 +08:00
void _limit(List<FlSpot> list, {int max = 30}) {
if (list.length > max) list.removeAt(0);
}
void _refreshDeviceData() {
final deviceState = context.read<DevicesCubit>().state;
final currentDevice = deviceState.selectedDevice;
if (currentDevice != null) {
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)));
}
}
2026-03-04 10:09:58 +08:00
String _parseLocationQuality(int locationValue) {
switch (locationValue) {
case 0:
return '无效';
case 1:
return 'GPS 单点定位';
case 2:
return 'DGPS 伪距差分或 SBAS';
case 4:
return 'RTK 固定解';
case 5:
return 'RTK 浮点解';
default:
2026-03-04 11:08:50 +08:00
return '未知($locationValue)';
2026-03-04 10:09:58 +08:00
}
}
2026-03-04 11:08:50 +08:00
void _appendChartData(DeviceStatusUpdated state) {
final status = state.status;
2026-01-18 20:21:14 +08:00
2026-03-04 11:08:50 +08:00
_leftTargetHistory.add(FlSpot(_timeIndex, status.leftTargetSpeed));
_rightTargetHistory.add(FlSpot(_timeIndex, status.rightTargetSpeed));
_leftMeasureHistory.add(FlSpot(_timeIndex, status.leftMeasureSpeed));
_rightMeasureHistory.add(FlSpot(_timeIndex, status.rightMeasureSpeed));
_leftCurrentHistory.add(FlSpot(_timeIndex, status.leftCurrent));
_rightCurrentHistory.add(FlSpot(_timeIndex, status.rightCurrent));
_leftTempHistory.add(FlSpot(_timeIndex, status.leftMotorTemp));
_rightTempHistory.add(FlSpot(_timeIndex, status.rightMotorTemp));
_knifeHistory.add(FlSpot(_timeIndex, double.tryParse(status.knifeCuttingSpeed) ?? 0));
_chipTempHistory.add(FlSpot(_timeIndex, status.chipTemp));
2026-03-04 13:25:20 +08:00
_voltageHistory.add(FlSpot(_timeIndex, status.voltage));
2026-03-04 11:08:50 +08:00
_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);
}
2026-03-04 11:21:12 +08:00
// ====================== 图表卡片(统一样式) ======================
Widget _chartCard({required String title, required Widget child}) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)),
),
const SizedBox(height: 16),
child,
],
),
);
}
2026-03-04 13:51:27 +08:00
// ====================== 带动画的圆形仪表盘(核心修改) ======================
2026-03-04 13:26:56 +08:00
Widget _circularGauge({
2026-03-04 11:21:12 +08:00
required double value,
required double maxValue,
required String unit,
required String label,
2026-03-04 13:26:56 +08:00
List<double> majorTicks = const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
2026-03-04 11:21:12 +08:00
}) {
2026-03-04 13:51:27 +08:00
return AnimatedCircularGauge(value: value, maxValue: maxValue, unit: unit, majorTicks: majorTicks);
2026-03-04 11:21:12 +08:00
}
// ====================== 罗盘式航向角仪表盘 ======================
Widget _compassGauge({required double yaw}) {
return SizedBox(
height: 250,
child: Stack(
alignment: Alignment.center,
children: [
CustomPaint(
painter: CompassGaugePainter(yaw: yaw),
size: const Size(280, 280),
),
Text(
"${yaw.toStringAsFixed(0)}°",
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black),
),
],
),
);
}
// ====================== 折线图(统一样式) ======================
Widget _styledLineChart({
required String title,
required List<LineChartBarData> lines,
required double yAxisMax,
double yAxisMin = 0,
List<String> bottomLabels = const ["t", "t+1", "t+2", "t+3", "t+4", "t+5"],
2026-03-04 13:25:20 +08:00
List<double> leftTicks = const [0, 1000, 2000, 3000],
2026-03-04 11:21:12 +08:00
}) {
2026-03-04 13:25:20 +08:00
// 获取实际显示的点位数量
final int showSpotCount = lines.isNotEmpty ? lines.first.spots.length : 0;
// 计算底部标签的间隔,避免标签过多溢出
final double bottomInterval = showSpotCount > 6 ? showSpotCount / 6 : 1;
2026-03-04 11:08:50 +08:00
return Column(
children: [
2026-03-04 11:21:12 +08:00
if (lines.length > 1)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_legendItem(color: const Color(0xFF3F51B5), label: "左轮"),
const SizedBox(width: 24),
_legendItem(color: const Color(0xFF8BC34A), label: "右轮"),
],
),
),
2026-03-04 13:25:20 +08:00
// 用Expanded适配宽度,避免溢出
Expanded(
child: SizedBox(
height: 200,
child: LineChart(
LineChartData(
minY: yAxisMin,
maxY: yAxisMax,
gridData: FlGridData(
show: true,
horizontalInterval: (yAxisMax - yAxisMin) / leftTicks.length,
getDrawingHorizontalLine: (value) => const FlLine(color: Color(0xFFE5E5E5), strokeWidth: 1),
drawVerticalLine: false,
2026-03-04 11:21:12 +08:00
),
2026-03-04 13:25:20 +08:00
titlesData: FlTitlesData(
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
interval: (yAxisMax - yAxisMin) / leftTicks.length,
getTitlesWidget: (value, meta) {
if (leftTicks.contains(value)) {
return Text(value.toStringAsFixed(0), style: const TextStyle(fontSize: 12, color: Colors.grey));
}
return const SizedBox.shrink();
},
// 限制左侧标签宽度
reservedSize: 40,
),
2026-03-04 11:21:12 +08:00
),
2026-03-04 13:25:20 +08:00
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
interval: bottomInterval,
// 限制底部标签高度和宽度
reservedSize: 30,
getTitlesWidget: (value, meta) {
int idx = value.toInt();
if (idx >= 0 && idx < bottomLabels.length) {
return Container(
width: 40,
alignment: Alignment.center,
child: Text(
bottomLabels[idx],
style: const TextStyle(fontSize: 12, color: Colors.grey),
overflow: TextOverflow.ellipsis,
),
);
}
return const SizedBox.shrink();
},
),
),
rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
2026-03-04 11:21:12 +08:00
),
2026-03-04 13:25:20 +08:00
borderData: FlBorderData(show: false),
lineBarsData: lines,
// 设置图表边距,避免内容贴边
minX: 0,
maxX: bottomLabels.length - 1.toDouble(),
extraLinesData: ExtraLinesData(horizontalLines: []),
2026-03-04 11:21:12 +08:00
),
),
),
2026-03-04 11:08:50 +08:00
),
],
);
}
2026-03-04 11:21:12 +08:00
Widget _legendItem({required Color color, required String label}) {
return Row(
children: [
Container(
width: 16,
height: 16,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
border: Border.all(color: color, width: 2),
),
),
const SizedBox(width: 4),
Text(label, style: const TextStyle(fontSize: 14, color: Colors.black87)),
],
);
}
LineChartBarData _lineData(List<FlSpot> data, Color color) {
2026-03-04 11:08:50 +08:00
return LineChartBarData(
2026-03-04 13:25:20 +08:00
spots: _takeLast(data, 6),
2026-03-04 11:21:12 +08:00
isCurved: false,
2026-03-04 11:08:50 +08:00
color: color,
barWidth: 2,
2026-03-04 11:21:12 +08:00
isStrokeCapRound: true,
2026-03-04 13:25:20 +08:00
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) => FlDotCirclePainter(radius: 4, color: color, strokeWidth: 2, strokeColor: Colors.white),
),
2026-03-04 11:21:12 +08:00
belowBarData: BarAreaData(show: false),
2026-03-04 11:08:50 +08:00
);
}
2026-03-04 11:21:12 +08:00
// ====================== 图表视图 ======================
Widget _buildChartContentView(DeviceStatusState state) {
if (state is! DeviceStatusUpdated) {
return const Center(child: CircularProgressIndicator());
}
2026-03-04 11:08:50 +08:00
2026-03-04 11:21:12 +08:00
final status = state.status;
2026-03-04 11:08:50 +08:00
2026-03-04 11:21:12 +08:00
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
2026-03-04 13:25:20 +08:00
// 电压仪表盘/折线图
2026-03-04 11:21:12 +08:00
_chartCard(
title: "电压 (V)",
2026-03-04 13:25:20 +08:00
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () => setState(() => _voltageGaugeMode = true),
child: Text(
"仪表盘",
style: TextStyle(
color: _voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: _voltageGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
const SizedBox(width: 16),
GestureDetector(
onTap: () => setState(() => _voltageGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_voltageGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
const SizedBox(height: 16),
_voltageGaugeMode
2026-03-04 13:26:56 +08:00
? _circularGauge(value: status.voltage, maxValue: 250, unit: "V", label: "电压", majorTicks: const [0, 50, 100, 150, 200, 250])
2026-03-04 13:25:20 +08:00
: SizedBox(
height: 200,
child: _styledLineChart(
title: "电压",
lines: [_lineData(_voltageHistory, const Color(0xFF4CAF50))],
yAxisMax: 250,
leftTicks: const [0, 50, 100, 150, 200, 250],
),
),
],
2026-03-04 11:21:12 +08:00
),
),
_gap(),
2026-03-04 11:08:50 +08:00
2026-03-04 13:25:20 +08:00
// 芯片温度仪表盘/折线图
2026-03-04 11:21:12 +08:00
_chartCard(
title: "芯片温度 (°C)",
2026-03-04 13:25:20 +08:00
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () => setState(() => _chipTempGaugeMode = true),
child: Text(
"仪表盘",
style: TextStyle(
color: _chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: _chipTempGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
const SizedBox(width: 16),
GestureDetector(
onTap: () => setState(() => _chipTempGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_chipTempGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
const SizedBox(height: 16),
_chipTempGaugeMode
2026-03-04 13:26:56 +08:00
? _circularGauge(
2026-03-04 13:25:20 +08:00
value: status.chipTemp,
maxValue: 100,
unit: "°C",
label: "芯片温度",
majorTicks: const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
)
: SizedBox(
height: 200,
child: _styledLineChart(
title: "芯片温度",
lines: [_lineData(_chipTempHistory, const Color(0xFF4CAF50))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
),
],
2026-03-04 11:21:12 +08:00
),
),
_gap(),
2026-03-04 11:08:50 +08:00
2026-03-04 13:25:20 +08:00
// 割刀速度仪表盘/折线图
2026-03-04 11:21:12 +08:00
_chartCard(
title: "割刀速度 (rpm)",
2026-03-04 13:25:20 +08:00
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () => setState(() => _knifeSpeedGaugeMode = true),
child: Text(
"仪表盘",
style: TextStyle(
color: _knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: _knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
const SizedBox(width: 16),
GestureDetector(
onTap: () => setState(() => _knifeSpeedGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
const SizedBox(height: 16),
_knifeSpeedGaugeMode
2026-03-04 13:26:56 +08:00
? _circularGauge(
2026-03-04 13:25:20 +08:00
value: double.tryParse(status.knifeCuttingSpeed) ?? 0,
maxValue: 3000,
unit: "rpm",
label: "割刀速度",
majorTicks: const [0, 500, 1000, 1500, 2000, 2500, 3000],
)
: SizedBox(
height: 200,
child: _styledLineChart(
title: "割刀速度",
lines: [_lineData(_knifeHistory, const Color(0xFF4CAF50))],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
),
],
),
2026-03-04 11:21:12 +08:00
),
_gap(),
2026-03-04 13:25:20 +08:00
// 测量速度对比(左轮 + 右轮在一张图)
2026-03-04 11:21:12 +08:00
_chartCard(
2026-03-04 13:25:20 +08:00
title: "左右轮测量速度对比 (rpm)",
child: SizedBox(
height: 200,
child: _styledLineChart(
title: "左右轮测量速度对比",
lines: [
_lineData(_leftMeasureHistory, const Color(0xFF3F51B5)), // 左轮
_lineData(_rightMeasureHistory, const Color(0xFF8BC34A)), // 右轮
],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
2026-03-04 11:21:12 +08:00
),
),
2026-03-04 11:08:50 +08:00
2026-03-04 13:25:20 +08:00
_gap(),
// 目标速度对比(左轮 + 右轮在一张图)
2026-03-04 11:21:12 +08:00
_chartCard(
2026-03-04 13:25:20 +08:00
title: "左右轮目标速度对比 (rpm)",
child: SizedBox(
height: 200,
child: _styledLineChart(
title: "左右轮目标速度对比",
lines: [
_lineData(_leftTargetHistory, const Color(0xFF3F51B5)), // 左轮
_lineData(_rightTargetHistory, const Color(0xFF8BC34A)), // 右轮
],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
2026-03-04 11:21:12 +08:00
),
),
2026-03-04 13:25:20 +08:00
2026-03-04 11:21:12 +08:00
_gap(),
2026-03-04 13:25:20 +08:00
// 电流对比
2026-03-04 11:21:12 +08:00
_chartCard(
2026-03-04 13:25:20 +08:00
title: "电流对比 (A)",
child: SizedBox(
height: 200,
child: _styledLineChart(
title: "电流对比",
lines: [_lineData(_leftCurrentHistory, const Color(0xFF3F51B5)), _lineData(_rightCurrentHistory, const Color(0xFF8BC34A))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
2026-03-04 11:21:12 +08:00
),
),
_gap(),
2026-03-04 13:25:20 +08:00
// 电机温度对比
2026-03-04 11:21:12 +08:00
_chartCard(
2026-03-04 13:25:20 +08:00
title: "电机温度对比 (°C)",
child: SizedBox(
height: 200,
child: _styledLineChart(
title: "电机温度对比",
lines: [_lineData(_leftTempHistory, const Color(0xFF3F51B5)), _lineData(_rightTempHistory, const Color(0xFF8BC34A))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
2026-03-04 11:21:12 +08:00
),
),
2026-03-04 13:25:20 +08:00
// 航向角仪表盘
_chartCard(
title: "航向角 (°)",
child: _compassGauge(yaw: status.yaw),
),
_gap(),
],
),
);
}
2026-03-04 11:21:12 +08:00
// ====================== 卡片视图 ======================
Widget _buildCardContentView(DeviceStatusState state) {
if (state is DeviceStatusUpdated) {
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(children: [_buildMotorTable(state), const SizedBox(height: 20), _buildOtherParamsTable(state)]),
);
} else if (state is DeviceStatusError) {
return const Center(
child: Text("数据加载失败", style: TextStyle(color: Colors.red, fontSize: 16)),
);
} else {
return const Center(child: CircularProgressIndicator());
}
}
2026-03-04 10:09:58 +08:00
Widget _buildMotorTable(DeviceStatusUpdated state) {
final status = state.status;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 12),
2026-03-04 11:08:50 +08:00
child: Text("电机参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
2026-03-04 10:09:58 +08:00
),
Table(
border: TableBorder.all(color: const Color(0xFFE5E5E5)),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: [
TableRow(
decoration: const BoxDecoration(color: Color(0xFFF5F5F5)),
children: [const SizedBox(), _buildTableCell("左轮", isHeader: true), _buildTableCell("右轮", isHeader: true)],
),
TableRow(
children: [
_buildTableCell("目标速度\n(rpm)"),
_buildTableCell(status.leftTargetSpeed.toStringAsFixed(2)),
_buildTableCell(status.rightTargetSpeed.toStringAsFixed(2)),
],
),
TableRow(
children: [
_buildTableCell("测量速度\n(rpm)"),
2026-03-04 10:42:56 +08:00
_buildTableCell(status.leftMeasureSpeed.toStringAsFixed(2)),
_buildTableCell(status.rightMeasureSpeed.toStringAsFixed(2)),
2026-03-04 10:09:58 +08:00
],
),
TableRow(
children: [
_buildTableCell("电流(A)"),
_buildTableCell(status.leftCurrent.toStringAsFixed(2)),
_buildTableCell(status.rightCurrent.toStringAsFixed(2)),
],
),
TableRow(
children: [
_buildTableCell("电机温度(°C)"),
_buildTableCell(status.leftMotorTemp.toStringAsFixed(2)),
_buildTableCell(status.rightMotorTemp.toStringAsFixed(2)),
],
),
],
),
],
);
}
2026-03-04 10:09:58 +08:00
Widget _buildOtherParamsTable(DeviceStatusUpdated state) {
final status = state.status;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 12),
2026-03-04 11:08:50 +08:00
child: Text("其他参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
2026-03-04 10:09:58 +08:00
),
Table(
border: TableBorder.all(color: const Color(0xFFE5E5E5)),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: [
TableRow(
decoration: const BoxDecoration(color: Color(0xFFF5F5F5)),
children: [_buildTableCell("名称", isHeader: true), _buildTableCell("数值", isHeader: true)],
),
TableRow(children: [_buildTableCell("俯仰角(°)"), _buildTableCell(status.pitch.toStringAsFixed(2))]),
TableRow(children: [_buildTableCell("翻滚角(°)"), _buildTableCell(status.roll.toStringAsFixed(2))]),
TableRow(children: [_buildTableCell("航向角(°)"), _buildTableCell(status.yaw.toStringAsFixed(2))]),
TableRow(children: [_buildTableCell("电量(%)"), _buildTableCell(status.battery)]),
TableRow(children: [_buildTableCell("芯片温度(°C)"), _buildTableCell(status.chipTemp.toStringAsFixed(2))]),
TableRow(children: [_buildTableCell("割刀速度(rpm)"), _buildTableCell(status.knifeCuttingSpeed)]),
2026-03-04 10:19:46 +08:00
TableRow(children: [_buildTableCell("经度(°)"), _buildTableCell(status.longitude.toStringAsFixed(6))]),
2026-03-04 10:09:58 +08:00
TableRow(children: [_buildTableCell("纬度(°)"), _buildTableCell(status.latitude.toStringAsFixed(6))]),
],
),
],
);
}
2026-03-04 10:09:58 +08:00
Widget _buildTableCell(String text, {bool isHeader = false}) {
return TableCell(
child: Padding(
2026-03-04 11:08:50 +08:00
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 6),
2026-03-04 10:09:58 +08:00
child: Text(
text,
textAlign: TextAlign.center,
style: TextStyle(
2026-03-04 11:08:50 +08:00
fontSize: 14,
2026-03-04 10:09:58 +08:00
color: isHeader ? const Color(0xFF333333) : const Color(0xFF666666),
fontWeight: isHeader ? FontWeight.bold : FontWeight.normal,
),
),
),
);
}
2026-03-04 11:21:12 +08:00
Widget _buildTab(String title, {required bool isActive}) {
return Text(
title,
style: TextStyle(
color: isActive ? const Color(0xFF1677FF) : const Color(0xFF666666),
fontSize: 16,
fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
decoration: isActive ? TextDecoration.underline : null,
decorationColor: const Color(0xFF1677FF),
decorationThickness: 2,
),
);
}
2026-03-04 11:08:50 +08:00
@override
Widget build(BuildContext context) {
final deviceState = context.read<DevicesCubit>().state;
final currentDevice = deviceState.selectedDevice;
2026-03-04 11:08:50 +08:00
if (currentDevice == null) {
return const Scaffold(body: Center(child: Text("加载中...")));
}
2026-03-04 11:08:50 +08:00
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<DeviceStatusBloc, DeviceStatusState>(
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)),
),
],
),
),
),
SliverToBoxAdapter(
child: BlocBuilder<DeviceStatusBloc, DeviceStatusState>(
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),
);
},
),
),
],
),
);
2026-01-18 20:21:14 +08:00
}
}
2026-03-04 11:21:12 +08:00
2026-03-04 13:51:27 +08:00
// ====================== 带动画的圆形仪表盘 Widget ======================
class AnimatedCircularGauge extends StatefulWidget {
2026-03-04 11:21:12 +08:00
final double value;
final double maxValue;
2026-03-04 13:51:27 +08:00
final String unit;
2026-03-04 11:21:12 +08:00
final List<double> majorTicks;
2026-03-04 13:51:27 +08:00
const AnimatedCircularGauge({super.key, required this.value, required this.maxValue, required this.unit, required this.majorTicks});
@override
State<AnimatedCircularGauge> createState() => _AnimatedCircularGaugeState();
}
class _AnimatedCircularGaugeState extends State<AnimatedCircularGauge> with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation<double> _valueAnimation;
double _previousValue = 0.0;
@override
void initState() {
super.initState();
// 初始化动画控制器,时长800ms(可调整)
_animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 800));
// 初始值动画
_previousValue = widget.value;
_valueAnimation =
Tween<double>(begin: 0, end: widget.value).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.easeOutCubic, // 缓出曲线,动画更自然
),
)..addListener(() {
setState(() {}); // 动画值变化时刷新UI
});
_animationController.forward();
}
@override
void didUpdateWidget(covariant AnimatedCircularGauge oldWidget) {
super.didUpdateWidget(oldWidget);
// 数值变化时触发新的动画
if (widget.value != oldWidget.value) {
_previousValue = _valueAnimation.value; // 从当前值开始动画
_valueAnimation =
Tween<double>(begin: _previousValue, end: widget.value).animate(CurvedAnimation(parent: _animationController, curve: Curves.easeOutCubic))
..addListener(() {
setState(() {});
});
_animationController.reset(); // 重置动画
_animationController.forward(); // 执行动画
}
}
@override
void dispose() {
_animationController.dispose(); // 释放动画控制器
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 220,
child: Stack(
alignment: Alignment.center,
children: [
CustomPaint(
painter: CircularGaugePainter(
value: _valueAnimation.value, // 使用动画插值后的数值
maxValue: widget.maxValue,
majorTicks: widget.majorTicks,
),
size: const Size(280, 280),
),
Positioned(
bottom: 20,
child: Text(
"${_valueAnimation.value.toStringAsFixed(2)} ${widget.unit}",
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w500, color: Colors.grey),
),
),
],
),
);
}
}
// ====================== 圆形仪表盘绘制器(带动画支持) ======================
// ====================== 圆形仪表盘绘制器(修复widget关键字错误) ======================
class CircularGaugePainter extends CustomPainter {
final double value;
final double maxValue;
final List<double> majorTicks; // 直接通过成员变量获取,而非widget
2026-03-04 13:26:56 +08:00
CircularGaugePainter({required this.value, required this.maxValue, required this.majorTicks});
2026-03-04 11:21:12 +08:00
@override
void paint(Canvas canvas, Size size) {
2026-03-04 13:26:56 +08:00
// 画布中心和半径(留出边距)
final center = Offset(size.width / 2, size.height / 2);
2026-03-04 11:21:12 +08:00
final radius = size.width / 2 - 20;
2026-03-04 13:26:56 +08:00
// 1. 绘制背景圆弧(浅蓝底色,270°范围:从135°到405°,对应左下到右下的半圆)
final bgArcPaint = Paint()
2026-03-04 11:21:12 +08:00
..color = const Color(0xFFE3F2FD)
..style = PaintingStyle.stroke
2026-03-04 13:26:56 +08:00
..strokeWidth = 8
2026-03-04 11:21:12 +08:00
..strokeCap = StrokeCap.round;
2026-03-04 13:26:56 +08:00
final bgRect = Rect.fromCircle(center: center, radius: radius);
canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180, false, bgArcPaint);
2026-03-04 11:21:12 +08:00
2026-03-04 13:51:27 +08:00
// 2. 绘制进度圆弧(浅蓝色,根据动画后的数值比例绘制)
2026-03-04 13:26:56 +08:00
final progressRatio = math.min(value / maxValue, 1.0); // 限制最大为1
2026-03-04 11:21:12 +08:00
final progressPaint = Paint()
..color = const Color(0xFFBBDEFB)
..style = PaintingStyle.stroke
2026-03-04 13:26:56 +08:00
..strokeWidth = 8
2026-03-04 11:21:12 +08:00
..strokeCap = StrokeCap.round;
2026-03-04 13:26:56 +08:00
canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180 * progressRatio, false, progressPaint);
2026-03-04 11:21:12 +08:00
2026-03-04 13:26:56 +08:00
// 3. 绘制刻度(主刻度+副刻度)
final tickPaint = Paint()..color = Colors.grey.shade400;
final majorTickPaint = Paint()
..color = Colors.grey.shade600
2026-03-04 11:21:12 +08:00
..strokeWidth = 2;
2026-03-04 13:26:56 +08:00
// 总刻度数(每1°一个副刻度,每10°一个主刻度)
const totalTicks = 270;
for (int i = 0; i < totalTicks; i++) {
// 计算刻度角度(从135°开始,到405°结束)
final angle = 135 * math.pi / 180 + (i * math.pi * 270 / 180) / totalTicks;
// 刻度起始和结束位置
final tickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 4);
final tickEnd = center + Offset(math.cos(angle), math.sin(angle)) * radius;
// 副刻度(短刻度)
tickPaint.strokeWidth = 1;
canvas.drawLine(tickStart, tickEnd, tickPaint);
// 主刻度(长刻度,匹配majorTicks的数值)
final tickValue = (i / totalTicks) * maxValue;
2026-03-04 13:51:27 +08:00
// 修复:直接使用this.majorTicks(去掉widget.)
2026-03-04 13:26:56 +08:00
if (majorTicks.any((tick) => (tickValue - tick).abs() < 0.1)) {
final majorTickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 8);
canvas.drawLine(majorTickStart, tickEnd, majorTickPaint);
// 绘制主刻度数值
final textValue = tickValue.toStringAsFixed(0);
2026-03-04 11:21:12 +08:00
final textPainter = TextPainter(
text: TextSpan(
text: textValue,
2026-03-04 13:26:56 +08:00
style: const TextStyle(fontSize: 14, color: Colors.grey, fontWeight: FontWeight.w500),
2026-03-04 11:21:12 +08:00
),
textDirection: TextDirection.ltr,
)..layout();
2026-03-04 13:26:56 +08:00
// 数值位置(在主刻度外侧)
final textOffset = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 20);
final textX = textOffset.dx - textPainter.width / 2;
final textY = textOffset.dy - textPainter.height / 2;
2026-03-04 11:21:12 +08:00
textPainter.paint(canvas, Offset(textX, textY));
}
}
2026-03-04 13:51:27 +08:00
// 4. 计算指针角度(使用动画后的数值)
2026-03-04 13:26:56 +08:00
final needleAngle = 135 * math.pi / 180 + 270 * math.pi / 180 * progressRatio;
2026-03-04 13:42:39 +08:00
// 5. 绘制三角形指针(细尖角指向刻度,宽底边在中心)
2026-03-04 11:21:12 +08:00
final needlePaint = Paint()
2026-03-04 13:42:39 +08:00
..color = Colors.grey.shade600
2026-03-04 11:21:12 +08:00
..style = PaintingStyle.fill;
2026-03-04 13:42:39 +08:00
// 调整三角形顶点和底边位置:细尖角指向刻度,宽底边在中心
2026-03-04 11:21:12 +08:00
final needlePath = Path()
2026-03-04 13:42:39 +08:00
// 1. 移动画笔到指针的细尖角(指向刻度的一端)
2026-03-04 13:51:27 +08:00
..moveTo(center.dx + (radius - 10) * math.cos(needleAngle), center.dy + (radius - 10) * math.sin(needleAngle))
2026-03-04 13:42:39 +08:00
// 2. 绘制第一条边:从尖角到中心左侧的底边端点
..lineTo(
center.dx + 8 * math.cos(needleAngle - math.pi / 2), // 中心左侧8px
center.dy + 8 * math.sin(needleAngle - math.pi / 2),
)
// 3. 绘制第二条边:从中心左侧端点到中心右侧的底边端点
..lineTo(
center.dx + 8 * math.cos(needleAngle + math.pi / 2), // 中心右侧8px
center.dy + 8 * math.sin(needleAngle + math.pi / 2),
)
// 4. 闭合路径:回到尖角,形成三角形
2026-03-04 11:21:12 +08:00
..close();
2026-03-04 13:42:39 +08:00
2026-03-04 11:21:12 +08:00
canvas.drawPath(needlePath, needlePaint);
2026-03-04 13:26:56 +08:00
2026-03-04 13:51:27 +08:00
// 6. 绘制指针中心圆点(增强视觉效果)
final centerDotPaint = Paint()
2026-03-04 13:42:39 +08:00
..color = Colors.white
2026-03-04 13:26:56 +08:00
..style = PaintingStyle.fill;
2026-03-04 13:51:27 +08:00
canvas.drawCircle(center, 4, centerDotPaint);
2026-03-04 11:21:12 +08:00
}
@override
2026-03-04 13:26:56 +08:00
bool shouldRepaint(covariant CircularGaugePainter oldDelegate) {
2026-03-04 13:51:27 +08:00
return oldDelegate.value != value || oldDelegate.maxValue != maxValue || oldDelegate.majorTicks != majorTicks;
2026-03-04 13:26:56 +08:00
}
2026-03-04 11:21:12 +08:00
}
// ====================== 罗盘式航向角仪表盘绘制器 ======================
class CompassGaugePainter extends CustomPainter {
final double yaw;
CompassGaugePainter({required this.yaw});
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2 - 20;
// 外圈
final outerPaint = Paint()
..color = const Color(0xFF1677FF)
..style = PaintingStyle.stroke
..strokeWidth = 12;
canvas.drawCircle(center, radius, outerPaint);
// 刻度
final tickPaint = Paint()
..color = Colors.grey
..strokeWidth = 2;
for (int i = 0; i < 36; i++) {
final angle = (i * 10 - 90) * math.pi / 180;
final x1 = center.dx + (radius - 10) * math.cos(angle);
final y1 = center.dy + (radius - 10) * math.sin(angle);
final x2 = center.dx + radius * math.cos(angle);
final y2 = center.dy + radius * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), tickPaint);
// 主刻度(45°间隔)
if (i % 9 == 0) {
final x3 = center.dx + (radius - 20) * math.cos(angle);
final y3 = center.dy + (radius - 20) * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x3, y3), tickPaint..strokeWidth = 4);
// 方向文字
final direction = i == 0
? "N"
: i == 9
? "E"
: i == 18
? "S"
: i == 27
? "W"
: "";
if (direction.isNotEmpty) {
final textPainter = TextPainter(
text: TextSpan(
text: direction,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black),
),
textDirection: TextDirection.ltr,
)..layout();
final textX = center.dx + (radius - 40) * math.cos(angle) - textPainter.width / 2;
final textY = center.dy + (radius - 40) * math.sin(angle) - textPainter.height / 2;
textPainter.paint(canvas, Offset(textX, textY));
}
// 角度值
final angleValue = (i * 10).toString();
final textPainter = TextPainter(
text: TextSpan(
text: angleValue,
style: const TextStyle(fontSize: 14, color: Colors.black54),
),
textDirection: TextDirection.ltr,
)..layout();
final textX = center.dx + (radius - 60) * math.cos(angle) - textPainter.width / 2;
final textY = center.dy + (radius - 60) * math.sin(angle) - textPainter.height / 2;
textPainter.paint(canvas, Offset(textX, textY));
}
}
// 指针
final needleAngle = (yaw - 90) * math.pi / 180;
final needlePaint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
final needlePath = Path()
..moveTo(center.dx, center.dy)
..lineTo(center.dx + radius * 0.7 * math.cos(needleAngle - 0.03), center.dy + radius * 0.7 * math.sin(needleAngle - 0.03))
..lineTo(center.dx + radius * 0.7 * math.cos(needleAngle + 0.03), center.dy + radius * 0.7 * math.sin(needleAngle + 0.03))
..close();
canvas.drawPath(needlePath, needlePaint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}