From 63fdda819544f0cdf5f03f8514d5f1ae1dcee6f4 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 02:28:26 +0100 Subject: [PATCH] Added DropDown for Relapse --- lib/models/relapse.dart | 7 ++++--- lib/providers/input_provider.dart | 15 +++++++++++++++ lib/widgets/drop_down.dart | 30 ++++++++++++++++++++++++++++++ lib/widgets/relapse_form.dart | 7 ++++++- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 lib/widgets/drop_down.dart diff --git a/lib/models/relapse.dart b/lib/models/relapse.dart index 78eeb12..2e65f51 100644 --- a/lib/models/relapse.dart +++ b/lib/models/relapse.dart @@ -1,20 +1,21 @@ import 'package:smoke_cess_app/interface/db_record.dart'; class Relapse implements DatabaseRecord { + final String _category; final String _comment; final DateTime _date; - Relapse(this._comment, this._date); + Relapse(this._category, this._comment, this._date); @override factory Relapse.fromDatabase(Map map) { DateTime date = DateTime.parse(map['date']); - return Relapse(map['comment'], date); + return Relapse(map['_category'], map['comment'], date); } @override String toCSV() { - return "${_date.toIso8601String()}, {_wokeUpAt.minute}, $_comment"; + return "${_date.toIso8601String()}, $_category, $_comment"; } @override diff --git a/lib/providers/input_provider.dart b/lib/providers/input_provider.dart index 881ce2c..5fec37c 100644 --- a/lib/providers/input_provider.dart +++ b/lib/providers/input_provider.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/models/mood.dart'; +import 'package:smoke_cess_app/models/relapse.dart'; import 'package:smoke_cess_app/models/sleep.dart'; import '../globals.dart' as globals; @@ -10,15 +11,22 @@ class InputProvider extends ChangeNotifier { 'wokeUpAt': const TimeOfDay(hour: 8, minute: 0), 'sleptAt': const TimeOfDay(hour: 22, minute: 0), }; + String _relapseCategory = ''; double get sliderValue => _sliderValue; TextEditingController get textController => _textController; + String get relapseCategory => _relapseCategory; set sliderValue(double newValue) { _sliderValue = newValue; notifyListeners(); } + set relapseCategory(String newValue) { + _relapseCategory = newValue; + notifyListeners(); + } + TimeOfDay getTimeEntry(String key) { return _times[key] ?? const TimeOfDay(hour: 12, minute: 0); } @@ -43,6 +51,13 @@ class InputProvider extends ChangeNotifier { _resetFields(); } + void saveRelapse() { + Relapse relapse = + Relapse(_relapseCategory, _textController.text, DateTime.now()); + globals.databaseService.addRelapse(relapse); + _resetFields(); + } + void saveSleep(String wokeUpKey, String sleptKey) { Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text, DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey)); diff --git a/lib/widgets/drop_down.dart b/lib/widgets/drop_down.dart new file mode 100644 index 0000000..2df8b07 --- /dev/null +++ b/lib/widgets/drop_down.dart @@ -0,0 +1,30 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import '../providers/input_provider.dart'; +import '../providers/settings_provider.dart'; + +class DropDown extends StatelessWidget { + const DropDown({super.key}); + + @override + Widget build(BuildContext context) { + var inputModel = context.watch(); + var settings = context.watch(); + return DropdownButtonFormField( + value: settings.settings?.relapseCategories?[0] ?? '', + icon: const Icon(Icons.arrow_downward), + elevation: 16, + style: const TextStyle(color: Colors.deepPurple), + onChanged: (String? value) { + inputModel.relapseCategory = value ?? ''; + }, + items: settings.settings?.relapseCategories + ?.map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + ); + } +} diff --git a/lib/widgets/relapse_form.dart b/lib/widgets/relapse_form.dart index 825e303..b80fcba 100644 --- a/lib/widgets/relapse_form.dart +++ b/lib/widgets/relapse_form.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/widgets/drop_down.dart'; import 'package:smoke_cess_app/widgets/text_formfield.dart'; import '../providers/input_provider.dart'; @@ -14,6 +15,10 @@ class RelapseForm extends StatelessWidget { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ + const ElevatedCard( + title: 'Rückfallkategorie', + child: DropDown(), + ), const ElevatedCard( title: 'Beschreibe deinen Rückfall', child: MyTextFormField('Beschreibe deinen Rückfall'), @@ -22,7 +27,7 @@ class RelapseForm extends StatelessWidget { height: 80, ), ElevatedButton( - onPressed: () {}, + onPressed: () => inputModel.saveRelapse(), child: const Text('Speichern'), ) ],