作业行距和航线方向实时改变 生成路径完成
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class RouteDirectionPanel extends StatefulWidget {
|
||||
// 重命名为Panel更符合组件语义
|
||||
// 接收外部传入的初始值
|
||||
final bool initialOptimalHeading;
|
||||
final double initialDirection;
|
||||
// 新增:回调函数 - 关闭面板/返回设置结果
|
||||
final ValueChanged<Map<String, dynamic>>? onConfirm;
|
||||
// 🔥 核心修改:新增实时回调(值变更时立即触发)
|
||||
final ValueChanged<Map<String, dynamic>>? onValueChanged;
|
||||
// 取消/关闭面板回调(保留)
|
||||
final VoidCallback? onCancel;
|
||||
|
||||
const RouteDirectionPanel({
|
||||
super.key,
|
||||
this.initialOptimalHeading = true, // 默认开启最优航向
|
||||
this.initialDirection = 0.0, // 默认方向0度
|
||||
this.onConfirm,
|
||||
this.onValueChanged, // 实时值变更回调
|
||||
this.onCancel,
|
||||
});
|
||||
|
||||
@@ -30,11 +30,18 @@ class _RouteDirectionPanelState extends State<RouteDirectionPanel> {
|
||||
super.initState();
|
||||
_optimalHeading = widget.initialOptimalHeading;
|
||||
_direction = widget.initialDirection.clamp(0, 360); // 限制在0-360度
|
||||
|
||||
// 初始化时立即传递一次初始值(可选,保证外部能拿到初始状态)
|
||||
_notifyValueChanged();
|
||||
}
|
||||
|
||||
// 🔥 新增:封装值变更通知逻辑
|
||||
void _notifyValueChanged() {
|
||||
widget.onValueChanged?.call({'optimalHeading': _optimalHeading, 'direction': _direction});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// 纯面板结构:无Scaffold,仅Container包裹核心UI
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
@@ -42,7 +49,7 @@ class _RouteDirectionPanelState extends State<RouteDirectionPanel> {
|
||||
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 8)],
|
||||
),
|
||||
padding: const EdgeInsets.all(16),
|
||||
constraints: const BoxConstraints(minHeight: 300), // 最小高度保证UI完整
|
||||
constraints: const BoxConstraints(minHeight: 240), // 最小高度保证UI完整
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -56,35 +63,22 @@ class _RouteDirectionPanelState extends State<RouteDirectionPanel> {
|
||||
onTap: () {
|
||||
widget.onCancel?.call(); // 触发取消回调
|
||||
},
|
||||
child: const Icon(
|
||||
Icons.arrow_back_ios,
|
||||
color: Colors.black87,
|
||||
size: 20,
|
||||
),
|
||||
child: const Icon(Icons.arrow_back_ios, color: Colors.black87, size: 20),
|
||||
),
|
||||
const Text(
|
||||
'航线方向',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.black87,
|
||||
),
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Colors.black87),
|
||||
),
|
||||
const SizedBox(width: 24), // 占位保持标题居中
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
//const Divider(height: 1, color: Colors.grey),
|
||||
//const SizedBox(height: 20),
|
||||
|
||||
// 最优航向开关
|
||||
// 最优航向开关(修改:值变更时触发回调)
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'最优航向',
|
||||
style: TextStyle(fontSize: 16, color: Colors.black87),
|
||||
),
|
||||
const Text('最优航向', style: TextStyle(fontSize: 16, color: Colors.black87)),
|
||||
Switch(
|
||||
value: _optimalHeading,
|
||||
activeColor: const Color(0xFF00C853),
|
||||
@@ -92,30 +86,25 @@ class _RouteDirectionPanelState extends State<RouteDirectionPanel> {
|
||||
setState(() {
|
||||
_optimalHeading = value;
|
||||
});
|
||||
// 🔥 实时传递最新值
|
||||
_notifyValueChanged();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// 航线方向滑块
|
||||
// 航线方向滑块(修改:值变更时触发回调)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'航线方向',
|
||||
style: TextStyle(fontSize: 16, color: Colors.black87),
|
||||
),
|
||||
const Text('航线方向', style: TextStyle(fontSize: 16, color: Colors.black87)),
|
||||
Text(
|
||||
'${_direction.toStringAsFixed(0)}°',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: _optimalHeading ? Colors.grey : Colors.black87,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
style: TextStyle(fontSize: 16, color: _optimalHeading ? Colors.grey : Colors.black87, fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -134,34 +123,12 @@ class _RouteDirectionPanelState extends State<RouteDirectionPanel> {
|
||||
setState(() {
|
||||
_direction = value;
|
||||
});
|
||||
// 🔥 实时传递最新值
|
||||
_notifyValueChanged();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// 确认按钮
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
// 触发确认回调,传递设置结果
|
||||
widget.onConfirm?.call({
|
||||
'optimalHeading': _optimalHeading,
|
||||
'direction': _direction,
|
||||
});
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF00C853),
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
child: const Text('确认'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -168,7 +168,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
bounds,
|
||||
options: FitBoundsOptions(
|
||||
padding: const EdgeInsets.all(50), // 边缘留白(避免内容贴边)
|
||||
maxZoom: 18, // 最大缩放级别(防止过度放大)
|
||||
//maxZoom: 18, // 最大缩放级别(防止过度放大)
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1236,6 +1236,100 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
// ========== 抽象:生成路径的核心函数 ==========
|
||||
Future<void> _generatePath({bool showTips = true}) async {
|
||||
// 1. 校验必要条件(至少需要3个打点才能形成闭合区域)
|
||||
if (_markedPoints.isEmpty) {
|
||||
if (showTips) {
|
||||
// 仅在手动点击完成时显示提示,实时调用时静默
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先添加作业区域打点!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_markedPoints.length < 3) {
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('作业区域至少需要3个打点才能生成路径!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_currentWorkMode == null) {
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先选择作业模式!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 转换页面数据为 generatePath 所需的参数格式
|
||||
// 2.1 参考点(取第一个打点,转换为 WGS84)
|
||||
final firstPoint = _markedPoints.first;
|
||||
final wgs84FirstPoint = gcj02ToWgs84(firstPoint.latitude, firstPoint.longitude);
|
||||
final referencePoint = work_area_model.ReferencePoint(lat: wgs84FirstPoint.latitude, lon: wgs84FirstPoint.longitude);
|
||||
|
||||
// 2.2 航向角(转换为整型,-1表示最优航向则取-1)
|
||||
debugPrint('当前航向角: $_angle');
|
||||
final heading = _angle == -1 ? -1 : _angle.toInt();
|
||||
|
||||
// 2.3 外边界(转换为 OuterBoundary 格式,所有点转 WGS84)
|
||||
final outerBoundary = work_area_model.OuterBoundary(
|
||||
// 将 LatLng 打点转换为 Position 列表(先转 WGS84)
|
||||
position: _markedPoints.map((latLng) {
|
||||
// 关键:每个点都转换为 WGS84 坐标系
|
||||
final wgs84Point = gcj02ToWgs84(latLng.latitude, latLng.longitude);
|
||||
return work_area_model.Position(lat: wgs84Point.latitude, lon: wgs84Point.longitude);
|
||||
}).toList(),
|
||||
sideWidth: _workDistance,
|
||||
);
|
||||
|
||||
final holes = <work_area_model.HoleBoundary>[];
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 弓字模式0,自定义2
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法生成路径
|
||||
await context.read<DevicesCubit>().generatePath(reference: referencePoint, heading: heading, outer: outerBoundary, holes: holes, workType: workType);
|
||||
|
||||
// 4. 获取生成结果并处理
|
||||
final cubitState = context.read<DevicesCubit>().state;
|
||||
print('${cubitState.generatedPath},路径生成结果');
|
||||
|
||||
if (cubitState.errorMessage != null) {
|
||||
// 生成失败(仅手动调用时显示提示)
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成失败:${cubitState.errorMessage}'), backgroundColor: Colors.red));
|
||||
}
|
||||
} else if (cubitState.generatedPath != null) {
|
||||
// 生成成功(手动调用显示提示,实时调用静默)
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('路径生成成功!'), backgroundColor: Colors.green));
|
||||
}
|
||||
debugPrint('生成的路径数据:${cubitState.generatedPath}');
|
||||
|
||||
// 转换路径坐标(WGS84 → GCJ02)并更新UI
|
||||
if (cubitState.generatedPath is List) {
|
||||
setState(() {
|
||||
final pathList = cubitState.generatedPath as List;
|
||||
typedPathList = pathList.whereType<work_area_model.DeviceAddPathPointModel>().toList();
|
||||
|
||||
gcjPathPoints = typedPathList.map((point) {
|
||||
double wgs84Lat = point.latitude;
|
||||
double wgs84Lon = point.longitude;
|
||||
final gcjPoint = wgs84ToGcj02(wgs84Lat, wgs84Lon);
|
||||
return LatLng(gcjPoint.latitude, gcjPoint.longitude);
|
||||
}).toList();
|
||||
|
||||
gcjOuterPoints = List.from(_markedPoints);
|
||||
});
|
||||
moveMapToPointsCenter(gcjPathPoints);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 异常处理(仅手动调用时显示提示)
|
||||
debugPrint('路径生成异常:$e');
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成异常:${e.toString().substring(0, 50)}'), backgroundColor: Colors.red));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final screenHeight = MediaQuery.of(context).size.height;
|
||||
@@ -1515,98 +1609,11 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
'操作完成回调:当前机器人模式=$_currentRobotMode,当前区域模式=$_currentAreaMode,当前作业模式:$_currentWorkMode,作业区域点:$_markedPoints,作业行距=$_workDistance,航线方向角=$_angle',
|
||||
);
|
||||
|
||||
// 1. 校验必要条件(至少需要3个打点才能形成闭合区域)
|
||||
if (_markedPoints.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先添加作业区域打点!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
if (_markedPoints.length < 3) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('作业区域至少需要3个打点才能生成路径!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
if (_currentWorkMode == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先选择作业模式!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 转换页面数据为 generatePath 所需的参数格式
|
||||
// 2.1 参考点(取第一个打点,转换为 WGS84)
|
||||
final firstPoint = _markedPoints.first;
|
||||
final wgs84FirstPoint = gcj02ToWgs84(firstPoint.latitude, firstPoint.longitude);
|
||||
final referencePoint = work_area_model.ReferencePoint(lat: wgs84FirstPoint.latitude, lon: wgs84FirstPoint.longitude);
|
||||
|
||||
// 2.2 航向角(转换为整型,-1表示最优航向则取-1)
|
||||
final heading = _angle == -1 ? -1 : _angle.toInt();
|
||||
|
||||
// 2.3 外边界(转换为 OuterBoundary 格式,所有点转 WGS84)
|
||||
final outerBoundary = work_area_model.OuterBoundary(
|
||||
// 将 LatLng 打点转换为 Position 列表(先转 WGS84)
|
||||
position: _markedPoints.map((latLng) {
|
||||
// 关键:每个点都转换为 WGS84 坐标系
|
||||
final wgs84Point = gcj02ToWgs84(latLng.latitude, latLng.longitude);
|
||||
return work_area_model.Position(lat: wgs84Point.latitude, lon: wgs84Point.longitude);
|
||||
}).toList(),
|
||||
sideWidth: _workDistance,
|
||||
);
|
||||
|
||||
final holes = <work_area_model.HoleBoundary>[];
|
||||
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 注意:这里弓字模式是0,自定义是2(确认和后端一致)
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法生成路径
|
||||
await context.read<DevicesCubit>().generatePath(
|
||||
reference: referencePoint,
|
||||
heading: heading,
|
||||
outer: outerBoundary,
|
||||
holes: holes,
|
||||
workType: workType,
|
||||
);
|
||||
|
||||
// 4. 获取生成结果并处理
|
||||
final cubitState = context.read<DevicesCubit>().state;
|
||||
print('${cubitState.generatedPath},路径生成结果');
|
||||
if (cubitState.errorMessage != null) {
|
||||
// 生成失败
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成失败:${cubitState.errorMessage}'), backgroundColor: Colors.red));
|
||||
} else if (cubitState.generatedPath != null) {
|
||||
// 生成成功
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('路径生成成功!'), backgroundColor: Colors.green));
|
||||
debugPrint('生成的路径数据:${cubitState.generatedPath}');
|
||||
|
||||
// 可选:将生成的路径转换为地图可显示的坐标(WGS84 → GCJ02)
|
||||
if (cubitState.generatedPath is List) {
|
||||
setState(() {
|
||||
// 先将 List<dynamic> 转换为 List<DeviceAddPathPointModel>
|
||||
final pathList = cubitState.generatedPath as List;
|
||||
typedPathList = pathList.whereType<work_area_model.DeviceAddPathPointModel>().toList();
|
||||
|
||||
// 然后使用 .lat 和 .lon 属性访问
|
||||
gcjPathPoints = typedPathList.map((point) {
|
||||
double wgs84Lat = point.latitude;
|
||||
double wgs84Lon = point.longitude;
|
||||
final gcjPoint = wgs84ToGcj02(wgs84Lat, wgs84Lon);
|
||||
return LatLng(gcjPoint.latitude, gcjPoint.longitude);
|
||||
}).toList();
|
||||
|
||||
gcjOuterPoints = List.from(_markedPoints); // 生成新列表,复制所有元素 // ========== 核心修改:清空临时打点列表 ==========
|
||||
//_markedPoints.clear(); // 清空后,临时划线会自动消失
|
||||
});
|
||||
moveMapToPointsCenter(gcjPathPoints);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 异常处理
|
||||
debugPrint('路径生成异常:$e');
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('路径生成异常:${e.toString().substring(0, 50)}'), backgroundColor: Colors.red));
|
||||
}
|
||||
|
||||
// 调用抽象后的生成路径函数(showTips=true:手动点击时显示提示)
|
||||
await _generatePath(showTips: true);
|
||||
// 5. 关闭操作面板
|
||||
setState(() {
|
||||
_isPanelOpen = false; // 关闭操作面板
|
||||
_currentWorkMode = null; // 清空作业模式,清除弓字模式的绿色半透明区域
|
||||
//_isPanelOpen = false; // 关闭操作面板
|
||||
});
|
||||
},
|
||||
onUndoTap: _undoLastPoint,
|
||||
@@ -1626,6 +1633,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
onDistanceTap: (distance) {
|
||||
_workDistance = distance;
|
||||
debugPrint('外部处理作业行距距离设置,当前距离:${distance.toStringAsFixed(1)}米');
|
||||
if (_markedPoints.length >= 3 && _currentWorkMode != null) {
|
||||
_generatePath(showTips: false);
|
||||
}
|
||||
},
|
||||
onSettingTap: () {
|
||||
setState(() {
|
||||
@@ -1651,11 +1661,15 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
child: RouteDirectionPanel(
|
||||
initialOptimalHeading: true,
|
||||
initialDirection: 0.0,
|
||||
onConfirm: (result) {
|
||||
onValueChanged: (result) {
|
||||
debugPrint('最优航向:${result['optimalHeading']},角度:${result['direction']}');
|
||||
_angle = result['optimalHeading'] == true ? -1 : result['direction'];
|
||||
|
||||
debugPrint('外部处理航线方向设置,当前角度:${_angle}');
|
||||
setState(() => _directionBoxOpen = false);
|
||||
if (_markedPoints.length >= 3 && _currentWorkMode != null) {
|
||||
debugPrint('外部处理航线方向设置11222:${_angle}');
|
||||
_generatePath(showTips: false);
|
||||
}
|
||||
},
|
||||
onCancel: () {
|
||||
setState(() => _directionBoxOpen = false);
|
||||
|
||||
Reference in New Issue
Block a user