pile_nav_new/lib/pages/task/task_page.dart
2024-11-15 17:42:52 +08:00

189 lines
6.2 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:cpnav/main.dart';
import 'package:cpnav/pages/task/info.dart';
import 'package:cpnav/pages/task/model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:scence_map/controllers/controller.dart';
import 'package:scence_map/record_entity.dart';
import 'package:scence_map/scence_map.dart';
import '../pass_track/controller.dart';
import '../pass_track/view.dart';
import 'taskcontroller.dart';
import 'package:path_provider/path_provider.dart';
final ScenceMapController mapcontroller = Get.find<ScenceMapController>();
final pscontroller = Get.put(PassTrackController());
final TaskController taskcontroller = Get.find<TaskController>();
// class TaskPage extends StatelessWidget {
class TaskPage extends StatefulWidget {
const TaskPage({super.key});
@override
TaskPageState createState() => TaskPageState();
}
class TaskPageState extends State<TaskPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
// final mediaQueryData = MediaQueryData.fromView(View.of(context)); //获取当前屏幕信息
// final orientation = mediaQueryData.orientation; //获得设备方向
// bool isPortrait = Orientation.portrait == orientation ? true : false;
return ScenceMapView(
onUpdate: (Offset center, double scale, double rotation) {
log("center:$center scale:$scale rotation:$rotation");
},
forGroundPainter: appcontroller.currentIndex.value == 1
? BorderPainter(pscontroller)
: null,
onUpdatePilePoint:
(RecordEntity? selectedPilePoint, double scale, double rotation) {
log("selectedPilePoint:$selectedPilePoint scale:$scale rotation:$rotation");
},
children: const [],
);
}
}
class TaskManagePage extends GetView<TaskController> {
const TaskManagePage({super.key});
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return OrientationBuilder(builder: ((context, orientation) {
return Stack(
children: [
SizedBox(
height: size.height * 0.8,
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Obx(() {
controller.updateCount;
List<Widget> children = [];
if (controller.taskList.isEmpty) {
children.add(Container(
width: 120,
margin: EdgeInsets.symmetric(
horizontal: size.width * .4,
vertical: size.height * 0.4),
child: const FloatingActionButton(
onPressed: null, child: Text('暂无任务'))));
} else {
for (int i = 0; i < controller.taskList.length; i++) {
Widget child =
TaskItemPage(taskInfo: controller.taskList[i]);
children.add(child);
}
}
return Column(
children: children,
);
}),
),
),
Positioned(
bottom: 5,
right: 5,
child: Row(
children: [
ElevatedButton(
onPressed: () {
controller.updateCount++;
},
child: const Text("同步")),
const SizedBox(width: 10),
ElevatedButton(
onPressed: () => addDialog(context),
child: const Text("新增"),
),
],
))
],
);
}));
}
addDialog(context) {
// 点击新增按钮时显示弹窗
TaskItem addTask = TaskItem(
list: [],
name: " ",
id: 0,
utc: 0,
originLabel: "本地",
workTotal: 0,
centerPileInfo: CenterPileInfo(
x: 0,
y: 0,
));
TextEditingController taskController =
TextEditingController(text: addTask.name.toString());
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("新增任务"),
content: SizedBox(
height: 100,
child: Column(
children: [
TextField(
keyboardType: TextInputType.emailAddress,
controller: taskController,
decoration: const InputDecoration(
hintText: '任务名称',
prefixText: '任务名称:',
border: InputBorder.none,
labelStyle: TextStyle(
fontSize: 16, color: Color.fromARGB(164, 75, 73, 73)),
),
)
],
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(); // 关闭弹窗
},
child: const Text("取消"),
),
TextButton(
onPressed: () async {
addTask.name = taskController.text.trim();
addTask.utc = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final directory = await getApplicationDocumentsDirectory();
final filePath = '${directory.path}/tasks/${addTask.utc}.json';
final file = File(filePath);
final folder = Directory('${directory.path}/tasks');
await folder.create(recursive: true);
String jsonString = jsonEncode(addTask.toJson());
// 写入 JSON 字符串到文件
await file.writeAsString(jsonString);
controller.taskList.add(addTask);
controller.updateCount++;
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
},
child: const Text("确定"),
),
],
);
},
);
}
}