Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 11aa6f8812 | |||
| 5fae2dcf4f | |||
| d44070a8e9 | |||
| 05b9d3e920 |
@@ -1,18 +1,170 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/callback_models/create_task_request.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';
|
||||
const TaskEditPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final task = ModalRoute.of(context)!.settings.arguments as Task?;
|
||||
State<TaskEditPage> createState() => _TaskEditPageState();
|
||||
}
|
||||
|
||||
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(
|
||||
appBar: AppBar(title: Text(task?.title ?? 'Create Task')),
|
||||
body: Form(child: Column()),
|
||||
appBar: AppBar(title: Text(pageTitle)),
|
||||
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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../example_data.dart';
|
||||
import '../model/callback_models/create_task_request.dart';
|
||||
import '../model/task.dart';
|
||||
import '../service/tools.dart';
|
||||
import 'task_edit_page.dart';
|
||||
|
||||
class TaskOverviewPage extends StatefulWidget {
|
||||
@@ -38,15 +40,22 @@ class _TaskOverviewPageState extends State<TaskOverviewPage> {
|
||||
key: Key(task.id),
|
||||
title: Text(task.title),
|
||||
subtitle: Text(task.description),
|
||||
onTap: () => onTaskTapped(task),
|
||||
onTap: () async {
|
||||
final result = await onTaskTapped(task);
|
||||
if (result != null) {
|
||||
tasks.remove(task);
|
||||
tasks.add(result.toTask(id: task.id, position: task.position));
|
||||
setState(() {});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<Task?> onTaskTapped(Task task) async {
|
||||
Future<CreateTaskRequest?> onTaskTapped(Task task) async {
|
||||
final result = await Navigator.of(
|
||||
context,
|
||||
).pushNamed(TaskEditPage.routeName, arguments: task);
|
||||
return result as Task?;
|
||||
return result as CreateTaskRequest?;
|
||||
}
|
||||
|
||||
List<Task> reorderList(List<Task> tasks, oldIndex, newIndex) {
|
||||
@@ -65,10 +74,12 @@ class _TaskOverviewPageState extends State<TaskOverviewPage> {
|
||||
void onCreateTaskTapped() async {
|
||||
//TODO: example data call
|
||||
final result =
|
||||
await Navigator.of(context).pushNamed(TaskEditPage.routeName) as Task?;
|
||||
await Navigator.of(context).pushNamed(TaskEditPage.routeName)
|
||||
as CreateTaskRequest?;
|
||||
|
||||
if (result != null) {
|
||||
tasks.add(result);
|
||||
tasks.add(result.toTask(id: generateId(), position: tasks.length));
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
String generateId() => DateTime.now().millisecondsSinceEpoch.toString();
|
||||
|
||||
String getIsoDateString(DateTime dateTime) {
|
||||
return dateTime.toIso8601String().substring(0, 10);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
String? timeValidator(String? value) {
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
if (RegExp(r'^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$').hasMatch(value)) {
|
||||
return null;
|
||||
}
|
||||
return 'Not a valid time format';
|
||||
}
|
||||
|
||||
String? dateTimeValidator(String? value) {
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
return DateTime.tryParse(value) != null ? null : 'Not a date format';
|
||||
}
|
||||
Reference in New Issue
Block a user