Начальный коммит
This commit is contained in:
211
lib/Pages/views/task_screen.dart
Normal file
211
lib/Pages/views/task_screen.dart
Normal file
@@ -0,0 +1,211 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:Stocky/Pages/controllers/MainController.dart';
|
||||
import 'package:Stocky/classes/styles.dart';
|
||||
import 'package:Stocky/models/task_adapter.dart';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class TaskFormScreen extends StatefulWidget {
|
||||
final Task? task;
|
||||
|
||||
const TaskFormScreen({super.key, this.task});
|
||||
|
||||
@override
|
||||
State<TaskFormScreen> createState() => _TaskFormScreenState();
|
||||
}
|
||||
|
||||
class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _titleController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _itemsController = TextEditingController();
|
||||
|
||||
DateTime? _selectedDate;
|
||||
|
||||
List<Subtask> _items = [];
|
||||
String _newItem = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.task != null) {
|
||||
_titleController.text = widget.task!.title;
|
||||
_descriptionController.text = widget.task!.description;
|
||||
_selectedDate = widget.task!.startTime;
|
||||
_items = List.from(widget.task!.items);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
widget.task != null ? 'Редактировать задачу' : 'Новая задача',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _saveTask,
|
||||
child: const Text('Сохранить', style: TextStyle(fontSize: 16)),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
children: [
|
||||
// Дата создания
|
||||
ListTile(
|
||||
title: Text(
|
||||
_selectedDate != null
|
||||
? 'Дата: ${_selectedDate!.day}.${_selectedDate!.month}.${_selectedDate!.year}'
|
||||
: 'Выберите дату',
|
||||
),
|
||||
trailing: const Icon(Icons.calendar_today),
|
||||
onTap: _selectDate,
|
||||
),
|
||||
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Название задачи',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Введите название задачи';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Описание (опционально)
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Описание',
|
||||
border: OutlineInputBorder(),
|
||||
alignLabelWithHint: true,
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Пункты задачи (чеклист)
|
||||
Text('Пункты задачи', style: AppStyles.titleSmall),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _itemsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Добавить пункт',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) {
|
||||
_newItem = value;
|
||||
},
|
||||
),
|
||||
),
|
||||
IconButton(icon: const Icon(Icons.add), onPressed: _addItem),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Список пунктов
|
||||
if (_items.isNotEmpty)
|
||||
..._items.map(
|
||||
(item) => Card(
|
||||
child: ListTile(
|
||||
title: Text(item.text),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.delete, color: Colors.red),
|
||||
onPressed: () => _removeItem(item),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _selectDate() async {
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: _selectedDate ?? DateTime.now(),
|
||||
firstDate: DateTime(2020),
|
||||
lastDate: DateTime(2101),
|
||||
);
|
||||
if (picked != null) {
|
||||
setState(() {
|
||||
_selectedDate = picked;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _addItem() {
|
||||
if (_newItem.trim().isNotEmpty) {
|
||||
setState(() {
|
||||
_items.add(
|
||||
Subtask(
|
||||
id: DateTime.now().microsecondsSinceEpoch.toString(),
|
||||
text: _newItem.trim(),
|
||||
isDone: false,
|
||||
),
|
||||
);
|
||||
|
||||
_itemsController.clear();
|
||||
_newItem = '';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _removeItem(Subtask item) {
|
||||
setState(() {
|
||||
_items.remove(item);
|
||||
});
|
||||
}
|
||||
|
||||
void _saveTask() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final mainController = Get.find<MainController>();
|
||||
|
||||
String generateId() {
|
||||
return DateTime.now().microsecondsSinceEpoch.toString();
|
||||
}
|
||||
|
||||
final task = Task(
|
||||
id: widget.task?.id ?? generateId(),
|
||||
title: _titleController.text,
|
||||
description: _descriptionController.text.trim(),
|
||||
startTime: _selectedDate ?? DateTime.now(),
|
||||
items: List.from(_items),
|
||||
author: mainController.username.value,
|
||||
executor: mainController.username.value,
|
||||
isSynced: false,
|
||||
isDeletedLocally: false,
|
||||
);
|
||||
|
||||
try {
|
||||
if (widget.task == null) {
|
||||
mainController.addTask(task);
|
||||
} else {
|
||||
mainController.updateTask(task);
|
||||
}
|
||||
Get.back();
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Ошибка сохранения: $e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user