added task edit/create form
This commit is contained in:
@@ -1,18 +1,170 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../model/callback_models/create_task_request.dart';
|
||||||
import '../model/task.dart';
|
import '../model/task.dart';
|
||||||
|
import '../service/tools.dart';
|
||||||
|
import '../service/validators.dart';
|
||||||
|
|
||||||
class TaskEditPage extends StatelessWidget {
|
class TaskEditPage extends StatefulWidget {
|
||||||
static const routeName = '/edit';
|
static const routeName = '/edit';
|
||||||
const TaskEditPage({super.key});
|
const TaskEditPage({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
State<TaskEditPage> createState() => _TaskEditPageState();
|
||||||
final task = ModalRoute.of(context)!.settings.arguments as Task?;
|
}
|
||||||
|
|
||||||
|
class _TaskEditPageState extends State<TaskEditPage> {
|
||||||
|
final titleController = TextEditingController();
|
||||||
|
final descriptionController = TextEditingController();
|
||||||
|
final categoryController = TextEditingController();
|
||||||
|
final urlController = TextEditingController();
|
||||||
|
final dueDateController = TextEditingController();
|
||||||
|
final dueTimeController = TextEditingController();
|
||||||
|
late String pageTitle;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
final task = ModalRoute.of(context)!.settings.arguments as Task?;
|
||||||
|
if (task != null) {
|
||||||
|
titleController.text = task.title;
|
||||||
|
descriptionController.text = task.description;
|
||||||
|
categoryController.text = task.category;
|
||||||
|
urlController.text = task.url;
|
||||||
|
dueDateController.text = task.due != null
|
||||||
|
? getIsoDateString(task.due!)
|
||||||
|
: '';
|
||||||
|
dueTimeController.text = task.due != null
|
||||||
|
? TimeOfDay.fromDateTime(task.due!).format(context)
|
||||||
|
: '';
|
||||||
|
}
|
||||||
|
pageTitle = task?.title ?? 'CreateTask';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text(task?.title ?? 'Create Task')),
|
appBar: AppBar(title: Text(pageTitle)),
|
||||||
body: Form(child: Column()),
|
body: Form(
|
||||||
|
autovalidateMode: AutovalidateMode.onUnfocus,
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
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.text,
|
||||||
|
textInputAction: TextInputAction.next,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
controller: dueDateController,
|
||||||
|
onChanged: (value) {
|
||||||
|
if (dateTimeValidator(value) == null) {}
|
||||||
|
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
|
decoration: InputDecoration(
|
||||||
|
label: Text('Due Date'),
|
||||||
|
suffix: IconButton(
|
||||||
|
onPressed: () async {
|
||||||
|
final result = await onOpenCalendarPickerPressed();
|
||||||
|
if (result != null) {
|
||||||
|
dueDateController.text = getIsoDateString(result);
|
||||||
|
}
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.calendar_month),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
validator: dateTimeValidator,
|
||||||
|
keyboardType: TextInputType.text,
|
||||||
|
textInputAction: TextInputAction.next,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
controller: dueTimeController,
|
||||||
|
enabled: DateTime.tryParse(dueDateController.text) != null
|
||||||
|
? true
|
||||||
|
: false,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
label: Text('Due Time'),
|
||||||
|
suffix: IconButton(
|
||||||
|
onPressed: () async {
|
||||||
|
final result = await onOpenTimePickerPressed();
|
||||||
|
if (result != null) {
|
||||||
|
setState(() {
|
||||||
|
dueTimeController.text = result.format(context);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.schedule),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
validator: timeValidator,
|
||||||
|
keyboardType: TextInputType.text,
|
||||||
|
textInputAction: TextInputAction.next,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
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),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<DateTime?> onOpenCalendarPickerPressed() {
|
||||||
|
return showDialog<DateTime?>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => DatePickerDialog(
|
||||||
|
firstDate: DateTime(DateTime.now().year - 100),
|
||||||
|
lastDate: DateTime(DateTime.now().year + 100),
|
||||||
|
initialDate:
|
||||||
|
DateTime.tryParse(dueDateController.text) ?? DateTime.now(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<TimeOfDay?> onOpenTimePickerPressed() {
|
||||||
|
return showDialog<TimeOfDay?>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) =>
|
||||||
|
TimePickerDialog(initialTime: TimeOfDay(hour: 0, minute: 0)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSavePressed() {
|
||||||
|
Navigator.of(context).pop(
|
||||||
|
CreateTaskRequest(
|
||||||
|
title: titleController.text,
|
||||||
|
description: descriptionController.text,
|
||||||
|
start: null,
|
||||||
|
due: DateTime.tryParse(dueDateController.text),
|
||||||
|
isCompleted: false,
|
||||||
|
category: categoryController.text,
|
||||||
|
subtasks: [],
|
||||||
|
alarms: [],
|
||||||
|
location: null,
|
||||||
|
url: urlController.text,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user