From 0e1c027ab53d7b6716e6747177c98de5363b8b64 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Mon, 2 Mar 2026 10:51:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E8=A1=8C=E8=B7=9D=E5=92=8C?= =?UTF-8?q?=E8=88=AA=E7=BA=BF=E6=96=B9=E5=90=91=E5=AE=9E=E6=97=B6=E6=94=B9?= =?UTF-8?q?=E5=8F=98=20=20=E7=94=9F=E6=88=90=E8=B7=AF=E5=BE=84=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../widgets/BottomDirectionLine.dart | 81 +++---- .../widgets/map/testmap_pages.dart | 200 ++++++++++-------- 2 files changed, 131 insertions(+), 150 deletions(-) diff --git a/lib/features/home/presentation/widgets/BottomDirectionLine.dart b/lib/features/home/presentation/widgets/BottomDirectionLine.dart index a70c33d7..5721522e 100644 --- a/lib/features/home/presentation/widgets/BottomDirectionLine.dart +++ b/lib/features/home/presentation/widgets/BottomDirectionLine.dart @@ -1,19 +1,19 @@ import 'package:flutter/material.dart'; class RouteDirectionPanel extends StatefulWidget { - // 重命名为Panel更符合组件语义 // 接收外部传入的初始值 final bool initialOptimalHeading; final double initialDirection; - // 新增:回调函数 - 关闭面板/返回设置结果 - final ValueChanged>? onConfirm; + // 🔥 核心修改:新增实时回调(值变更时立即触发) + final ValueChanged>? 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 { 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 { 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 { 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 { 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 { 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('确认'), - ), - ), ], ), ), diff --git a/lib/features/home/presentation/widgets/map/testmap_pages.dart b/lib/features/home/presentation/widgets/map/testmap_pages.dart index e5e5a264..a9eb1910 100644 --- a/lib/features/home/presentation/widgets/map/testmap_pages.dart +++ b/lib/features/home/presentation/widgets/map/testmap_pages.dart @@ -168,7 +168,7 @@ class _MapPageEnterpriseState extends State { bounds, options: FitBoundsOptions( padding: const EdgeInsets.all(50), // 边缘留白(避免内容贴边) - maxZoom: 18, // 最大缩放级别(防止过度放大) + //maxZoom: 18, // 最大缩放级别(防止过度放大) ), ); @@ -1236,6 +1236,100 @@ class _MapPageEnterpriseState extends State { }).toList(); } + // ========== 抽象:生成路径的核心函数 ========== + Future _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 = []; + final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 弓字模式0,自定义2 + + // 3. 调用 Cubit 的 generatePath 方法生成路径 + await context.read().generatePath(reference: referencePoint, heading: heading, outer: outerBoundary, holes: holes, workType: workType); + + // 4. 获取生成结果并处理 + final cubitState = context.read().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().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 { '操作完成回调:当前机器人模式=$_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 = []; - - final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 注意:这里弓字模式是0,自定义是2(确认和后端一致) - - // 3. 调用 Cubit 的 generatePath 方法生成路径 - await context.read().generatePath( - reference: referencePoint, - heading: heading, - outer: outerBoundary, - holes: holes, - workType: workType, - ); - - // 4. 获取生成结果并处理 - final cubitState = context.read().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 转换为 List - final pathList = cubitState.generatedPath as List; - typedPathList = pathList.whereType().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 { 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 { 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);