Added DropDown for Relapse

main
Kai Mannweiler 2023-02-27 02:28:26 +01:00
parent a2ed17fd5c
commit 63fdda8195
4 changed files with 55 additions and 4 deletions

View File

@ -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<String, dynamic> 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

View File

@ -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));

View File

@ -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<InputProvider>();
var settings = context.watch<SettingsProvider>();
return DropdownButtonFormField<String>(
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<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}

View File

@ -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'),
)
],