路径规划作业列表 其余界面优化
This commit is contained in:
@@ -2,12 +2,18 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:maibu_satabot_v2/components/confrim_dialog.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/net_message_dispatcher.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/protocol_decoder.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_work_record_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart';
|
||||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/BottomDirectionLine.dart';
|
||||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/enum.dart';
|
||||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/tracepoint.dart';
|
||||
@@ -20,12 +26,19 @@ import '../path_list_pages.dart';
|
||||
// 定义轨迹点类型(经纬度)
|
||||
typedef PlotPoint = LatLng;
|
||||
|
||||
// 保持 PlotData 类不变
|
||||
class PlotData {
|
||||
final String id; // 唯一ID,用于删除
|
||||
final String plotName; // 地块名称
|
||||
final String id;
|
||||
final String plotName;
|
||||
final String imageUrl; // 图片URL(本地/assets/网络都可)
|
||||
final String? jsonData; // 原始数据的JSON字符串(可选,便于调试或后续使用)
|
||||
|
||||
PlotData({required this.id, required this.plotName, required this.imageUrl});
|
||||
PlotData({
|
||||
required this.id,
|
||||
required this.plotName,
|
||||
required this.imageUrl,
|
||||
this.jsonData,
|
||||
});
|
||||
}
|
||||
|
||||
class MapPageEnterprise extends StatefulWidget {
|
||||
@@ -70,22 +83,8 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
double _workDistance = 0.0; // 作业行距(单位:米)
|
||||
double _angle = 0.0; // 航线方向角(单位:度)
|
||||
// =================================
|
||||
//测试数据
|
||||
final List<PlotData> _plotList = [
|
||||
PlotData(
|
||||
id: '1',
|
||||
plotName: '北地块(一号田)',
|
||||
imageUrl: '', // 本地图片
|
||||
// 网络图片示例:imageUrl: 'https://xxx.com/plot1.jpg',
|
||||
),
|
||||
PlotData(id: '2', plotName: '东地块(二号田)', imageUrl: ''),
|
||||
PlotData(id: '3', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
PlotData(id: '4', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
PlotData(id: '5', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
PlotData(id: '6', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
PlotData(id: '7', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
PlotData(id: '8', plotName: '西地块(三号田)', imageUrl: ''),
|
||||
];
|
||||
// 🔥 改动1:删除硬编码的 _plotList,改为从Bloc读取
|
||||
// final List<PlotData> _plotList = [...];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -233,16 +232,13 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
// 3. 保存地块数据的核心方法(这里写你的实际保存逻辑)
|
||||
void _savePlotData(String plotName) {
|
||||
// 示例:打印保存的信息(实际开发中替换为存数据库/本地存储/接口调用)
|
||||
|
||||
debugPrint('===== 保存地块数据 =====');
|
||||
debugPrint('地块名称:$plotName');
|
||||
debugPrint('打点数量:${_markedPoints.length}');
|
||||
debugPrint('打点坐标:$_markedPoints');
|
||||
debugPrint('作业模式:$_currentWorkMode');
|
||||
|
||||
// 这里可以添加实际的保存逻辑:
|
||||
// 1. 存到本地数据库(如Hive/SQLite)
|
||||
// 2. 调用API上传到服务器
|
||||
// 3. 保存到SharedPreferences(简单数据)
|
||||
}
|
||||
|
||||
List<LatLng> _getPolygonPoints() {
|
||||
@@ -389,11 +385,13 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
_traceManager.upsert(newPoint);
|
||||
}
|
||||
|
||||
// 在State类中添加列表项构建方法
|
||||
Widget _buildPlotListItem(PlotData plot) {
|
||||
// 🔥 改动2:修改 _buildPlotListItem 方法,增加删除时调用Bloc的逻辑
|
||||
Widget _buildPlotListItem(PlotData plot, Function(PlotData) onDelete) {
|
||||
return GestureDetector(
|
||||
// 核心:点击列表项触发选中逻辑
|
||||
onTap: () {
|
||||
onTap: () async {
|
||||
await context.read<DevicesCubit>().deleteWorkRecord(plot.plotName);
|
||||
|
||||
setState(() {
|
||||
// 1. 选中当前地块
|
||||
_selectedPlot = plot;
|
||||
@@ -402,18 +400,10 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
// 3. 打开底部作业面板
|
||||
_isWorkPanelOpen = true;
|
||||
});
|
||||
|
||||
//// 可选:提示选中成功
|
||||
//ScaffoldMessenger.of(context).showSnackBar(
|
||||
// SnackBar(
|
||||
// content: Text('已选中地块:${plot.plotName}'),
|
||||
// duration: const Duration(seconds: 1),
|
||||
// ),
|
||||
//);
|
||||
},
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
padding: const EdgeInsets.all(12),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
@@ -429,15 +419,15 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
// 1. 地块图片(原有代码)
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Image.asset(
|
||||
child: Image.network(
|
||||
plot.imageUrl,
|
||||
width: 60,
|
||||
height: 60,
|
||||
width: 30,
|
||||
height: 30,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Container(
|
||||
width: 60,
|
||||
height: 60,
|
||||
width: 30,
|
||||
height: 30,
|
||||
color: Colors.grey[200],
|
||||
child: const Icon(Icons.image_outlined, color: Colors.grey),
|
||||
);
|
||||
@@ -445,7 +435,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 12),
|
||||
const SizedBox(width: 10),
|
||||
|
||||
// 2. 地块名称(原有代码)
|
||||
Expanded(
|
||||
@@ -457,11 +447,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
),
|
||||
),
|
||||
|
||||
// 3. 删除按钮(原有代码)
|
||||
// 3. 删除按钮(修改:调用外部传入的删除方法)
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
_showDeleteConfirmDialog(plot);
|
||||
},
|
||||
onPressed: () => _showDeleteConfirmDialog(plot, onDelete),
|
||||
icon: const Icon(
|
||||
Icons.delete_outline,
|
||||
color: Colors.redAccent,
|
||||
@@ -476,43 +464,20 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
);
|
||||
}
|
||||
|
||||
// 实现删除确认弹窗
|
||||
void _showDeleteConfirmDialog(PlotData plot) {
|
||||
showDialog(
|
||||
void _showDeleteConfirmDialog(PlotData plot, Function(PlotData) onDelete) {
|
||||
ConfirmDialog.show(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: const Text('确认删除'),
|
||||
content: Text('是否删除地块「${plot.plotName}」?删除后不可恢复。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text('取消'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// 执行删除逻辑
|
||||
setState(() {
|
||||
_plotList.removeWhere((item) => item.id == plot.id);
|
||||
});
|
||||
|
||||
// 关闭弹窗
|
||||
Navigator.of(context).pop();
|
||||
|
||||
// 提示删除成功
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('已删除地块「${plot.plotName}」'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('删除', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
);
|
||||
title: '确认删除',
|
||||
content: '是否删除地块「${plot.plotName}」?删除后不可恢复。',
|
||||
confirmText: '删除',
|
||||
confirmTextColor: Colors.red,
|
||||
onConfirm: () {
|
||||
onDelete(plot);
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('已删除地块「${plot.plotName}」')));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -551,7 +516,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
// 地块图片
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Image.asset(
|
||||
child: Image.network(
|
||||
_selectedPlot!.imageUrl,
|
||||
width: 80,
|
||||
height: 80,
|
||||
@@ -650,11 +615,28 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
);
|
||||
}
|
||||
|
||||
// 🔥 改动4:新增方法 - 将Bloc的workRecords转换为PlotData列表
|
||||
List<PlotData> _convertWorkRecordsToPlotData(
|
||||
List<Map<String, dynamic>> records,
|
||||
) {
|
||||
return records.map((record) {
|
||||
return PlotData(
|
||||
id:
|
||||
record['id']?.toString() ??
|
||||
DateTime.now().microsecondsSinceEpoch.toString(), // 唯一ID
|
||||
plotName: record['workName'] ?? '未命名地块', // 地块名称(从接口字段取)
|
||||
imageUrl: record['imgUrl'] ?? '', // 图片URL(从接口字段取,无则为空)
|
||||
jsonData: record['jsonData'], // 原始数据的JSON字符串(可选,便于调试或后续使用)
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final screenHeight = MediaQuery.of(context).size.height;
|
||||
final menuHeight = 16 * 6; // 假设 VerticalFloatMenu 有 6 个选项,每个高度为 56
|
||||
final maxTop = screenHeight - menuHeight;
|
||||
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Stack(
|
||||
@@ -885,6 +867,12 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
});
|
||||
},
|
||||
onListBox: (bool isOpen) {
|
||||
// 🔥 改动5:加载作业记录(原有逻辑保留)
|
||||
final userId =
|
||||
context.read<AppUserCubit>().state.user?.userId ?? "";
|
||||
print('加载作业记录,当前用户ID:$userId');
|
||||
context.read<DevicesCubit>().loadWorkRecords(userId);
|
||||
|
||||
setState(() {
|
||||
_isListBoxOpen = isOpen;
|
||||
});
|
||||
@@ -972,114 +960,148 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
),
|
||||
),
|
||||
|
||||
// 列表面板
|
||||
// 🔥 改动6:列表面板 - 使用BlocBuilder监听DevicesCubit状态
|
||||
if (_isListBoxOpen)
|
||||
Positioned.fill(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Expanded(
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_isListBoxOpen = false;
|
||||
});
|
||||
},
|
||||
child: Container(color: Colors.black.withOpacity(0.3)),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.vertical(
|
||||
top: Radius.circular(16),
|
||||
BlocBuilder<DevicesCubit, DevicesState>(
|
||||
builder: (context, state) {
|
||||
final plotList = _convertWorkRecordsToPlotData(
|
||||
state.workRecords ?? [],
|
||||
);
|
||||
|
||||
return Positioned.fill(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Expanded(
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_isListBoxOpen = false;
|
||||
});
|
||||
},
|
||||
child: Container(
|
||||
color: Colors.black.withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black12,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, -2),
|
||||
),
|
||||
],
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
maxHeight: 600,
|
||||
minHeight: 200,
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 12,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'地块列表(共${_plotList.length}条)',
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_isListBoxOpen = false;
|
||||
});
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.close,
|
||||
color: Colors.grey,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
],
|
||||
Container(
|
||||
width: double.infinity,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.vertical(
|
||||
top: Radius.circular(16),
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black12,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, -2),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Divider(height: 1, color: Colors.grey),
|
||||
Expanded(
|
||||
child: _plotList.isEmpty
|
||||
? const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.inbox_outlined,
|
||||
color: Colors.grey,
|
||||
size: 48,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无地块数据',
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
),
|
||||
itemCount: _plotList.length,
|
||||
itemBuilder: (context, index) {
|
||||
final plot = _plotList[index];
|
||||
return _buildPlotListItem(plot);
|
||||
},
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
maxHeight: 600,
|
||||
minHeight: 200,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 12,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'地块列表(共${plotList.length}条)',
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_isListBoxOpen = false;
|
||||
});
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.close,
|
||||
color: Colors.grey,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(height: 1, color: Colors.grey),
|
||||
Expanded(
|
||||
child: plotList.isEmpty
|
||||
? const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.inbox_outlined,
|
||||
color: Colors.grey,
|
||||
size: 48,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无地块数据',
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
),
|
||||
itemCount: plotList.length,
|
||||
itemBuilder: (context, index) {
|
||||
final plot = plotList[index];
|
||||
// 传入删除回调(调用Bloc的删除方法)
|
||||
return _buildPlotListItem(plot, (
|
||||
deletedPlot,
|
||||
) async {
|
||||
await context
|
||||
.read<DevicesCubit>()
|
||||
.deleteWorkRecord(
|
||||
deletedPlot.plotName,
|
||||
);
|
||||
final userId =
|
||||
context
|
||||
.read<AppUserCubit>()
|
||||
.state
|
||||
.user
|
||||
?.userId ??
|
||||
"";
|
||||
await context
|
||||
.read<DevicesCubit>()
|
||||
.loadWorkRecords(userId);
|
||||
|
||||
setState(() {
|
||||
// 因为Bloc数据更新后会自动重建,这里可省略
|
||||
});
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
if (_isWorkPanelOpen) _buildWorkPanel(),
|
||||
|
||||
Reference in New Issue
Block a user