2026-02-25 13:41:23 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2026-01-18 20:21:14 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
|
|
|
|
|
import '../../../../core/app/app_user_cubit.dart';
|
2026-02-25 13:41:23 +08:00
|
|
|
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-03 19:34:17 +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
|
|
|
|
2026-02-25 13:41:23 +08:00
|
|
|
class RunningStatusPage extends StatefulWidget {
|
2026-01-18 20:21:14 +08:00
|
|
|
const RunningStatusPage({super.key});
|
|
|
|
|
|
2026-02-25 13:41:23 +08:00
|
|
|
@override
|
|
|
|
|
State<RunningStatusPage> createState() => _RunningStatusPageState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _RunningStatusPageState extends State<RunningStatusPage> {
|
|
|
|
|
// 视图切换状态
|
|
|
|
|
bool _isCardView = true;
|
|
|
|
|
|
2026-01-18 20:21:14 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final deviceState = context.read<DevicesCubit>().state;
|
|
|
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
|
|
2026-02-25 13:41:23 +08:00
|
|
|
if (currentDevice == null) {
|
2026-03-03 19:34:17 +08:00
|
|
|
return const Scaffold(
|
|
|
|
|
body: Center(child: Text("加载中...")),
|
|
|
|
|
);
|
2026-02-25 13:41:23 +08:00
|
|
|
}
|
2026-01-18 20:21:14 +08:00
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
|
backgroundColor: const Color(0xFFF7F7F7),
|
|
|
|
|
body: CustomScrollView(
|
|
|
|
|
physics: const BouncingScrollPhysics(),
|
2026-02-25 13:41:23 +08:00
|
|
|
slivers: [
|
|
|
|
|
// 1. 顶部导航栏
|
|
|
|
|
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),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
2026-03-03 19:34:17 +08:00
|
|
|
// 2. 状态信息行(动态更新)- 使用 BlocBuilder 监听
|
|
|
|
|
BlocBuilder<DeviceStatusBloc, DeviceStatusState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
String yaw = '--';
|
|
|
|
|
String qual = '--';
|
|
|
|
|
String satelliteCnt = '--';
|
|
|
|
|
|
|
|
|
|
if (state is DeviceStatusUpdated) {
|
|
|
|
|
yaw = state.status.yaw.toStringAsFixed(2);
|
|
|
|
|
qual = state.status.qual.toString();
|
|
|
|
|
satelliteCnt = state.status.satelliteCnt.toString();
|
|
|
|
|
} else if (state is DeviceStatusError) {
|
|
|
|
|
yaw = '错误';
|
|
|
|
|
qual = '-';
|
|
|
|
|
satelliteCnt = '-';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SliverToBoxAdapter(
|
|
|
|
|
child: Container(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text("航向角状态:$yaw°", style: const TextStyle(fontSize: 14)),
|
|
|
|
|
Text("定位质量:$qual", style: const TextStyle(fontSize: 14)),
|
|
|
|
|
Text("卫星数:$satelliteCnt", style: const TextStyle(fontSize: 14)),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-02-25 13:41:23 +08:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// 3. 选项卡 + 刷新按钮
|
|
|
|
|
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)),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
2026-03-03 19:34:17 +08:00
|
|
|
// 4. 内容区域 - 使用 BlocBuilder 监听
|
2026-02-25 13:41:23 +08:00
|
|
|
SliverFillRemaining(
|
2026-03-03 19:34:17 +08:00
|
|
|
child: BlocBuilder<DeviceStatusBloc, DeviceStatusState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
return Container(
|
|
|
|
|
margin: const EdgeInsets.all(16),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
),
|
|
|
|
|
child: _isCardView ? _buildCardContentView(state) : _buildChartContentView(),
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-02-25 13:41:23 +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-03 19:34:17 +08:00
|
|
|
Widget _buildCardItem(String title, String value) {
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text(title, style: const TextStyle(fontSize: 16, color: Color(0xFF333333))),
|
|
|
|
|
Text(value, style: const TextStyle(fontSize: 16, color: Color(0xFF666666))),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Widget _buildCardContentView(DeviceStatusState state) {
|
|
|
|
|
// 从 Bloc 状态中提取数据
|
|
|
|
|
String voltage = '--';
|
|
|
|
|
String leftSpeed = '--';
|
|
|
|
|
String rightSpeed = '--';
|
|
|
|
|
String yaw = '--';
|
|
|
|
|
String pitch = '--';
|
|
|
|
|
String roll = '--';
|
|
|
|
|
String satelliteCnt = '--';
|
|
|
|
|
String qual = '--';
|
|
|
|
|
String latitude = '--';
|
|
|
|
|
String longitude = '--';
|
|
|
|
|
String timeStamp = '--';
|
|
|
|
|
String knifeCuttingSpeed = '--';
|
|
|
|
|
String controlMode = '--';
|
|
|
|
|
String workingArea = '--';
|
|
|
|
|
String battery = '--';
|
|
|
|
|
String obstacleFlag = '--';
|
|
|
|
|
|
|
|
|
|
if (state is DeviceStatusUpdated) {
|
|
|
|
|
final status = state.status;
|
|
|
|
|
voltage = status.voltage.toStringAsFixed(2);
|
|
|
|
|
leftSpeed = status.leftTargetSpeed.toStringAsFixed(2);
|
|
|
|
|
rightSpeed = status.rightTargetSpeed.toStringAsFixed(2);
|
|
|
|
|
yaw = status.yaw.toStringAsFixed(2);
|
|
|
|
|
pitch = status.pitch.toStringAsFixed(2);
|
|
|
|
|
roll = status.roll.toStringAsFixed(2);
|
|
|
|
|
satelliteCnt = status.satelliteCnt.toString();
|
|
|
|
|
qual = status.qual.toString();
|
|
|
|
|
latitude = status.latitude.toStringAsFixed(8);
|
|
|
|
|
longitude = status.longitude.toStringAsFixed(8);
|
|
|
|
|
timeStamp = '${status.latitude} ${status.longitude}';
|
|
|
|
|
knifeCuttingSpeed = status.knifeCuttingSpeed;
|
|
|
|
|
controlMode = status.controlMode;
|
|
|
|
|
workingArea = status.workingArea;
|
|
|
|
|
battery = status.battery;
|
|
|
|
|
obstacleFlag = status.obstacleFlag;
|
|
|
|
|
} else if (state is DeviceStatusError) {
|
|
|
|
|
voltage = '错误';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.all(20.0),child: SingleChildScrollView(
|
2026-02-25 13:41:23 +08:00
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const Text("设备实时状态", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
|
|
|
const SizedBox(height: 16),
|
2026-03-03 19:34:17 +08:00
|
|
|
_buildCardItem("电压", "$voltage V"),
|
|
|
|
|
_buildCardItem("左轮速度", "$leftSpeed rpm"),
|
|
|
|
|
_buildCardItem( "右轮速度", "$rightSpeed rpm"),
|
|
|
|
|
_buildCardItem( "航向角", "$yaw°"),
|
|
|
|
|
_buildCardItem( "俯仰角", "$pitch°"),
|
|
|
|
|
_buildCardItem( "翻滚角", "$roll°"),
|
|
|
|
|
_buildCardItem( "卫星数", satelliteCnt),
|
|
|
|
|
_buildCardItem( "定位质量", qual),
|
|
|
|
|
_buildCardItem( "纬度", latitude),
|
|
|
|
|
_buildCardItem( "经度", longitude),
|
|
|
|
|
/* //_buildCardItem( "时间戳", timeStamp),*/
|
|
|
|
|
_buildCardItem( "割刀速度", "$knifeCuttingSpeed rpm"),
|
|
|
|
|
_buildCardItem( "控制模式", controlMode),
|
|
|
|
|
_buildCardItem( "作业面积", "$workingArea m²"),
|
|
|
|
|
_buildCardItem( "电量", "$battery%"),
|
|
|
|
|
_buildCardItem( "障碍物标志", obstacleFlag),
|
2026-02-25 13:41:23 +08:00
|
|
|
],
|
|
|
|
|
),
|
2026-03-03 19:34:17 +08:00
|
|
|
|
|
|
|
|
),
|
2026-02-25 13:41:23 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildChartContentView() {
|
|
|
|
|
return const Center(
|
|
|
|
|
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))),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 19:34:17 +08:00
|
|
|
void _refreshDeviceData() {
|
|
|
|
|
final deviceState = context.read<DevicesCubit>().state;
|
|
|
|
|
final currentDevice = deviceState.selectedDevice;
|
2026-02-25 13:41:23 +08:00
|
|
|
|
2026-03-03 19:34:17 +08:00
|
|
|
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-01-18 20:21:14 +08:00
|
|
|
}
|
|
|
|
|
}
|