178 lines
5.6 KiB
Dart
178 lines
5.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../app_theme.dart';
|
|
import '../model/callback_models/create_task_request.dart';
|
|
import '../model/extensions/controller_context.dart';
|
|
import '../model/task.dart';
|
|
import '../service/controllers/task_controller.dart';
|
|
import '../service/tools.dart';
|
|
import '../widgets/time_selector.dart';
|
|
|
|
class TaskEditPage extends StatefulWidget {
|
|
static const routeName = '/edit';
|
|
const TaskEditPage({super.key});
|
|
|
|
@override
|
|
State<TaskEditPage> createState() => _TaskEditPageState();
|
|
}
|
|
|
|
class _TaskEditPageState extends State<TaskEditPage> {
|
|
Task? task;
|
|
bool isInitialized = false;
|
|
final titleController = TextEditingController();
|
|
final descriptionController = TextEditingController();
|
|
final categoryController = TextEditingController();
|
|
final urlController = TextEditingController();
|
|
final dueDateController = TextEditingController();
|
|
final dueTimeController = TextEditingController();
|
|
String pageTitle = 'Create Task';
|
|
final dueDateFocusNode = FocusNode();
|
|
final dueTimeFocusNode = FocusNode();
|
|
final categoryFocusNode = FocusNode();
|
|
final formKey = GlobalKey<FormState>(debugLabel: 'taskEditFormKey');
|
|
bool didFormChange = false;
|
|
bool isDueTimeEnabled = false;
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
task = ModalRoute.of(context)!.settings.arguments as Task?;
|
|
if (task != null && !isInitialized) {
|
|
titleController.text = task!.title;
|
|
descriptionController.text = task!.description;
|
|
categoryController.text = task!.category;
|
|
urlController.text = task!.url;
|
|
if (task!.due != null) {
|
|
dueDateController.text = getIsoDateString(task!.due!);
|
|
dueTimeController.text = TimeOfDay.fromDateTime(
|
|
task!.due!,
|
|
).format(context);
|
|
isDueTimeEnabled = true;
|
|
}
|
|
isInitialized = true;
|
|
pageTitle = task!.title;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(pageTitle),
|
|
actions: [
|
|
if (task != null)
|
|
IconButton(
|
|
onPressed: () {
|
|
context.controller<TaskController>().deleteTask(task!);
|
|
Navigator.of(context).pop();
|
|
},
|
|
icon: Icon(Icons.delete),
|
|
),
|
|
],
|
|
),
|
|
body: Form(
|
|
canPop: !didFormChange,
|
|
onChanged: () => didFormChange = true,
|
|
onPopInvokedWithResult: onPopInvoked,
|
|
autovalidateMode: AutovalidateMode.onUnfocus,
|
|
key: formKey,
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: MediaQuery.of(context).size.width * 0.05,
|
|
),
|
|
child: Column(
|
|
spacing: AppTheme.formColumnSpacing,
|
|
children: [
|
|
SizedBox(height: 6),
|
|
TextFormField(
|
|
autofocus: true,
|
|
controller: titleController,
|
|
decoration: InputDecoration(label: Text('Title')),
|
|
keyboardType: TextInputType.text,
|
|
textInputAction: TextInputAction.next,
|
|
),
|
|
TextFormField(
|
|
controller: descriptionController,
|
|
decoration: InputDecoration(label: Text('Description')),
|
|
keyboardType: TextInputType.multiline,
|
|
textInputAction: TextInputAction.newline,
|
|
minLines: 3,
|
|
maxLines: 10,
|
|
),
|
|
TimeSelector(
|
|
initialDueDateTime: task?.due,
|
|
nextFocusNode: categoryFocusNode,
|
|
dueDateController: dueDateController,
|
|
dueTimeController: dueTimeController,
|
|
formKey: formKey,
|
|
),
|
|
TextFormField(
|
|
focusNode: categoryFocusNode,
|
|
controller: categoryController,
|
|
decoration: InputDecoration(label: Text('Category')),
|
|
keyboardType: TextInputType.text,
|
|
textInputAction: TextInputAction.next,
|
|
),
|
|
TextFormField(
|
|
controller: urlController,
|
|
decoration: InputDecoration(label: Text('Url')),
|
|
keyboardType: TextInputType.text,
|
|
textInputAction: TextInputAction.next,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: onSavePressed,
|
|
child: Icon(Icons.save),
|
|
),
|
|
);
|
|
}
|
|
|
|
void onSavePressed() {
|
|
Navigator.of(context).pop(
|
|
CreateTaskRequest(
|
|
title: titleController.text,
|
|
description: descriptionController.text,
|
|
start: null,
|
|
due: DateTime.tryParse(
|
|
'${dueDateController.text} ${dueTimeController.text}'.trim(),
|
|
),
|
|
isCompleted: false,
|
|
category: categoryController.text,
|
|
subtasks: [],
|
|
url: urlController.text,
|
|
),
|
|
);
|
|
}
|
|
|
|
void onPopInvoked(bool didPop, Object? result) {
|
|
if (!didPop) {
|
|
showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text('Unsaved Changes'),
|
|
content: Text(
|
|
'Are you sure you want to cancel editing? Unsaved changes will be lost.',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: Text('No'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
child: Text('Yes'),
|
|
),
|
|
],
|
|
),
|
|
).then((result) {
|
|
if (result != null && result && mounted) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|