cpd/lib/widgets/addhabit_popup.dart

134 lines
3.9 KiB
Dart
Raw Normal View History

2024-05-09 09:46:23 +02:00
import 'package:flutter/material.dart';
import 'package:cpd/pages/iconpage.dart';
2024-05-09 09:46:23 +02:00
class AddHabitPopup extends StatefulWidget {
final void Function(String, String, IconData) onSubmit;
2024-05-09 09:46:23 +02:00
@override
_AddHabitPopupState createState() => _AddHabitPopupState();
const AddHabitPopup({
super.key,
required this.onSubmit,
});
}
2024-05-09 09:46:23 +02:00
class _AddHabitPopupState extends State<AddHabitPopup> {
final titleController = TextEditingController();
final subtitleController = TextEditingController();
late IconData selectedIcon;
@override
void initState() {
super.initState();
titleController.text = "";
subtitleController.text = "";
selectedIcon = Icons.favorite;
}
2024-05-09 09:46:23 +02:00
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(Icons.close, color: Colors.red),
onPressed: () => Navigator.pop(context),
2024-05-09 09:46:23 +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(
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",
),
validator: (value) =>
value != null && value.isEmpty ? "Please enter a title" : null,
onFieldSubmitted: (_) => _submitForm(),
2024-05-09 09:46:23 +02:00
),
2024-05-09 09:46:23 +02:00
TextFormField(
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",
),
validator: (value) =>
value != null && value.isEmpty ? "Please enter a subtitle" : null,
onFieldSubmitted: (_) => _submitForm(),
),
],
),
),
const SizedBox(height: 10,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: _submitForm,
child: const Text('Save'),
),
ElevatedButton(
onPressed: () async {
IconData? newSelectedIcon = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => IconPage(selectedIcon: selectedIcon,
onIconSelected: (icon) {
setState(() {
selectedIcon = icon;
});
},
)
),
);
if (newSelectedIcon != null) {
selectedIcon = newSelectedIcon;
}
},
child: const Text('Select Icon'),
),
]
),
2024-05-09 09:46:23 +02:00
],
),
);
}
void _submitForm() {
if (_formKey.currentState!.validate()) {
widget.onSubmit(titleController.text, subtitleController.text,
selectedIcon);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${titleController.text} saved!'),
),
);
}
}
}