91 lines
2.5 KiB
Dart
91 lines
2.5 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../database/habit.dart';
|
|
|
|
// speichern durch enter
|
|
|
|
class CreateTodoWidget extends StatefulWidget {
|
|
final Habit? todo;
|
|
final void Function(String, String) onSubmit;
|
|
|
|
|
|
const CreateTodoWidget({
|
|
super.key,
|
|
this.todo,
|
|
required this.onSubmit,
|
|
});
|
|
|
|
@override
|
|
State<CreateTodoWidget> createState() => _CreateTodoWidgetState();
|
|
}
|
|
|
|
class _CreateTodoWidgetState extends State<CreateTodoWidget> {
|
|
final titleController = TextEditingController();
|
|
final subtitleController = TextEditingController();
|
|
|
|
final formKey = GlobalKey<FormState>();
|
|
|
|
void init() {
|
|
super.initState();
|
|
|
|
titleController.text = widget.todo?.title ?? '';
|
|
subtitleController.text = widget.todo?.subtitle ?? '';
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isEditing = widget.todo != null;
|
|
return AlertDialog(
|
|
title: Text(isEditing ? 'Edit Todo' : 'Add Todo'),
|
|
content: Form(
|
|
key: formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextFormField(
|
|
autofocus: true,
|
|
controller: titleController,
|
|
decoration: const InputDecoration(
|
|
icon: Icon(Icons.title),
|
|
labelText: "Title",
|
|
hintText: "Enter task title",
|
|
),
|
|
//keyboardType: TextInputType.text,
|
|
validator: (value) =>
|
|
value != null && value.isEmpty ? "Please enter a title" : null
|
|
),
|
|
TextFormField(
|
|
autofocus: true,
|
|
controller: subtitleController,
|
|
decoration: const InputDecoration(
|
|
icon: Icon(Icons.title),
|
|
labelText: "Title",
|
|
hintText: "Enter task title",
|
|
),
|
|
//keyboardType: TextInputType.text,
|
|
validator: (value) =>
|
|
value != null && value.isEmpty ? "Please enter a title" : null
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
if (formKey.currentState!.validate()) {
|
|
widget.onSubmit(titleController.text, subtitleController.text);
|
|
}
|
|
},
|
|
child: const Text('OK'),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
}
|