127 lines
3.4 KiB
Dart
127 lines
3.4 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
import 'package:cpd/database/habit.dart';
|
||
|
import 'package:cpd/database/todo_db.dart';
|
||
|
|
||
|
import '../pages/iconpage.dart';
|
||
|
|
||
|
class EditHabitDialog extends StatefulWidget {
|
||
|
final Habit todo;
|
||
|
final TodoDB todoDB;
|
||
|
final Function fetchTodos;
|
||
|
|
||
|
EditHabitDialog({
|
||
|
required this.todo,
|
||
|
required this.todoDB,
|
||
|
required this.fetchTodos,
|
||
|
});
|
||
|
|
||
|
@override
|
||
|
_EditHabitDialogState createState() => _EditHabitDialogState();
|
||
|
}
|
||
|
|
||
|
class _EditHabitDialogState extends State<EditHabitDialog> {
|
||
|
late String newTitle;
|
||
|
String? newSubtitle;
|
||
|
late IconData newIcon;
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
newTitle = widget.todo.title;
|
||
|
newSubtitle = widget.todo.subtitle;
|
||
|
newIcon = widget.todo.icon;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return AlertDialog(
|
||
|
title: const Text('Edit Habit'),
|
||
|
content: Column(
|
||
|
mainAxisSize: MainAxisSize.min,
|
||
|
children: [
|
||
|
TextFormField(
|
||
|
initialValue: newTitle,
|
||
|
onChanged: (value) {
|
||
|
newTitle = value;
|
||
|
},
|
||
|
decoration: const InputDecoration(
|
||
|
icon: Icon(Icons.title),
|
||
|
labelText: "Title",
|
||
|
hintText: "Enter task title",
|
||
|
),
|
||
|
onFieldSubmitted: (_) =>
|
||
|
_submitForm(context, widget.todo, newTitle, newSubtitle),
|
||
|
),
|
||
|
TextFormField(
|
||
|
initialValue: newSubtitle ?? '',
|
||
|
onChanged: (value) {
|
||
|
newSubtitle = value;
|
||
|
},
|
||
|
decoration: const InputDecoration(
|
||
|
icon: Icon(Icons.title),
|
||
|
labelText: "Subtitle",
|
||
|
hintText: "Enter task subtitle",
|
||
|
),
|
||
|
onFieldSubmitted: (_) =>
|
||
|
_submitForm(context, widget.todo, newTitle, newSubtitle),
|
||
|
),
|
||
|
IconButton(
|
||
|
icon: Icon(newIcon),
|
||
|
onPressed: () async {
|
||
|
IconData? selectedIcon = await Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(
|
||
|
builder: (context) => IconPage(selectedIcon: newIcon,
|
||
|
onIconSelected: (icon) {
|
||
|
setState(() {
|
||
|
newIcon = icon;
|
||
|
});
|
||
|
},)
|
||
|
),
|
||
|
);
|
||
|
if (selectedIcon != null) {
|
||
|
setState(() {
|
||
|
newIcon = selectedIcon;
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
actions: [
|
||
|
TextButton(
|
||
|
onPressed: () {
|
||
|
widget.fetchTodos();
|
||
|
Navigator.pop(context);
|
||
|
},
|
||
|
child: const Text('Cancel'),
|
||
|
),
|
||
|
TextButton(
|
||
|
onPressed: () async {
|
||
|
await widget.todoDB.update(
|
||
|
id: widget.todo.id,
|
||
|
title: newTitle,
|
||
|
subtitle: newSubtitle,
|
||
|
icon: newIcon,
|
||
|
);
|
||
|
widget.fetchTodos();
|
||
|
Navigator.of(context).pop();
|
||
|
},
|
||
|
child: const Text('Save'),
|
||
|
),
|
||
|
],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
void _submitForm(BuildContext context, Habit todo, String newTitle,
|
||
|
String? newSubtitle) async {
|
||
|
await widget.todoDB.update(
|
||
|
id: todo.id,
|
||
|
title: newTitle,
|
||
|
subtitle: newSubtitle,
|
||
|
icon: newIcon,
|
||
|
);
|
||
|
widget.fetchTodos();
|
||
|
Navigator.of(context).pop();
|
||
|
}
|
||
|
}
|