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/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cpd/controller/habitmanager.dart';
import 'package:cpd/model/habit_list.dart';
2024-05-09 09:46:23 +02:00
// save button + speichern durch enter
class AddHabitPopup extends StatefulWidget {
final Function? onHabitAdded;
TextEditingController titleController = TextEditingController();
TextEditingController subtitleController = TextEditingController();
AddHabitPopup({this.onHabitAdded});
2024-05-09 09:46:23 +02:00
@override
_AddHabitPopupState createState() => _AddHabitPopupState();
}
class _AddHabitPopupState extends State<AddHabitPopup> {
//habitmanager aufrufen
Habitmanager habitmanager = Habitmanager();
2024-05-09 09:46:23 +02:00
String title = '';
String subtitle = '';
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.of(context).pop();
},
),
const Expanded(child: Text(
2024-05-09 09:46:23 +02:00
'Add task',
textAlign: TextAlign.center,
)
)
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Divider(thickness: 3),
const Text(
'Tasks start each day as incomplete.\n'
'Mark a task as done to keep your progress up.\n'
'CREATE YOUR OWN:',
textAlign: TextAlign.start,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontFamily: 'Arial',
fontSize: 14,
),
),
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
controller: widget.titleController,
2024-05-09 09:46:23 +02:00
decoration: const InputDecoration(
icon: Icon(Icons.title),
labelText: "Title",
hintText: "Enter task title",
),
keyboardType: TextInputType.text,
validator: (String? value) {
return (value == null || value.trim().isEmpty)
? "Please enter a title"
: null;
},
),
TextFormField(
controller: widget.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",
),
keyboardType: TextInputType.text,
validator: (String? value) {
return (value == null || value.trim().isEmpty)
? "Please enter a title"
: null;
},
),
// auswahl an icons zur verfügung stellen
const SizedBox(height: 16.0),
2024-05-09 09:46:23 +02:00
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
String title = widget.titleController.text;
String subtitle = widget.subtitleController.text;
habitmanager.addNewHabit(false, title, subtitle, const Icon(Icons.label_important));
print('Current list after add:');
habits.forEach((habit) {
print('${habit.title}: ${habit.subtitle}');
});
2024-05-09 09:46:23 +02:00
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
2024-05-09 09:46:23 +02:00
content: Text('Form saved!'),
),
);
widget.onHabitAdded?.call();
//fester schließen
Navigator.of(context).pop();
// ui wird neu gezeichnet
2024-05-09 09:46:23 +02:00
}
},
child: const Text('Save'),
2024-05-09 09:46:23 +02:00
)
],
),
),
],
),
);
}
}