2024-05-09 09:46:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-19 22:08:04 +02:00
|
|
|
import 'package:cpd/pages/iconpage.dart';
|
2024-05-09 09:46:23 +02:00
|
|
|
|
|
|
|
class AddHabitPopup extends StatefulWidget {
|
2024-05-19 22:08:04 +02:00
|
|
|
final void Function(String, String, IconData) onSubmit;
|
2024-05-09 17:37:41 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
@override
|
2024-06-08 14:22:18 +02:00
|
|
|
AddHabitPopupState createState() => AddHabitPopupState();
|
2024-05-12 00:08:54 +02:00
|
|
|
|
|
|
|
const AddHabitPopup({
|
2024-05-12 22:55:54 +02:00
|
|
|
super.key,
|
2024-06-16 17:12:28 +02:00
|
|
|
required this.onSubmit,
|
2024-05-12 22:55:54 +02:00
|
|
|
});
|
2024-06-16 17:12:28 +02:00
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
|
2024-06-08 14:22:18 +02:00
|
|
|
class AddHabitPopupState extends State<AddHabitPopup> {
|
2024-05-12 00:08:54 +02:00
|
|
|
final titleController = TextEditingController();
|
|
|
|
final subtitleController = TextEditingController();
|
2024-05-19 22:08:04 +02:00
|
|
|
late IconData selectedIcon;
|
2024-05-09 17:37:41 +02:00
|
|
|
|
2024-05-12 00:08:54 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-05-09 17:37:41 +02:00
|
|
|
|
2024-05-12 22:55:54 +02:00
|
|
|
titleController.text = "";
|
|
|
|
subtitleController.text = "";
|
2024-05-19 22:08:04 +02:00
|
|
|
selectedIcon = Icons.favorite;
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
2024-05-09 17:37:41 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-24 00:01:08 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
return AlertDialog(
|
|
|
|
title: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.close, color: Colors.red),
|
2024-05-12 00:08:54 +02:00
|
|
|
onPressed: () => Navigator.pop(context),
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
2024-05-12 17:56:36 +02:00
|
|
|
const Expanded(
|
|
|
|
child: Text(
|
|
|
|
'Add task',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-05-09 09:46:23 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
|
|
|
const Divider(thickness: 3),
|
|
|
|
|
|
|
|
Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
TextFormField(
|
2024-05-12 00:08:54 +02:00
|
|
|
autofocus: true,
|
|
|
|
controller: titleController,
|
2024-05-09 09:46:23 +02:00
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.title),
|
|
|
|
labelText: "Title",
|
|
|
|
hintText: "Enter task title",
|
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
validator: (value) =>
|
2024-05-12 17:56:36 +02:00
|
|
|
value != null && value.isEmpty ? "Please enter a title" : null,
|
2024-05-13 20:08:39 +02:00
|
|
|
onFieldSubmitted: (_) => _submitForm(),
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
TextFormField(
|
2024-05-12 00:08:54 +02:00
|
|
|
controller: subtitleController,
|
2024-05-09 09:46:23 +02:00
|
|
|
decoration: const InputDecoration(
|
|
|
|
icon: Icon(Icons.description),
|
|
|
|
labelText: "Description",
|
|
|
|
hintText: "Describe how you plan to achieve your goal",
|
2024-05-12 17:56:36 +02:00
|
|
|
),
|
|
|
|
validator: (value) =>
|
|
|
|
value != null && value.isEmpty ? "Please enter a subtitle" : null,
|
2024-05-13 20:08:39 +02:00
|
|
|
onFieldSubmitted: (_) => _submitForm(),
|
2024-05-12 17:56:36 +02:00
|
|
|
),
|
|
|
|
],
|
2024-05-12 00:08:54 +02:00
|
|
|
),
|
|
|
|
),
|
2024-05-12 22:55:54 +02:00
|
|
|
const SizedBox(height: 10,),
|
2024-06-16 17:12:28 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: _submitForm,
|
|
|
|
key: const Key('Save'),
|
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
|
|
|
selectedIcon= await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => IconPage(selectedIcon: selectedIcon,
|
|
|
|
onIconSelected: (icon) {
|
|
|
|
setState(() {
|
|
|
|
selectedIcon = icon;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Select Icon'),
|
|
|
|
),
|
|
|
|
]
|
2024-05-12 22:55:54 +02:00
|
|
|
),
|
2024-05-09 09:46:23 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-13 20:08:39 +02:00
|
|
|
void _submitForm() {
|
|
|
|
if (_formKey.currentState!.validate()) {
|
2024-05-19 22:08:04 +02:00
|
|
|
widget.onSubmit(titleController.text, subtitleController.text,
|
2024-06-16 17:12:28 +02:00
|
|
|
selectedIcon);
|
2024-05-13 20:08:39 +02:00
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text('${titleController.text} saved!'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-05-19 22:08:04 +02:00
|
|
|
}
|