cpd_2022_zi/lib/widgets/mood_form.dart

56 lines
1.7 KiB
Dart
Raw Normal View History

2023-02-15 23:02:29 +01:00
import 'package:flutter/material.dart';
2023-02-22 01:28:01 +01:00
import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/service/database_service.dart';
2023-02-15 23:02:29 +01:00
import 'package:smoke_cess_app/widgets/slider.dart';
2023-02-15 23:28:19 +01:00
import 'package:smoke_cess_app/widgets/submit_form_button.dart';
import 'package:smoke_cess_app/widgets/text_formfield.dart';
2023-02-15 23:02:29 +01:00
2023-02-21 20:56:10 +01:00
import 'elevated_card.dart';
2023-02-15 23:02:29 +01:00
class MoodForm extends StatefulWidget {
const MoodForm({super.key});
@override
State<MoodForm> createState() => _MoodFormState();
}
class _MoodFormState extends State<MoodForm> {
final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>();
2023-02-21 22:33:03 +01:00
MySlider slider = const MySlider();
2023-02-15 23:02:29 +01:00
String _textInput = "";
2023-02-15 23:28:19 +01:00
void submitForm() {
if (_moodFormKey.currentState!.validate()) {
_moodFormKey.currentState?.save(); //call every onSave Method
Mood mood =
Mood(slider.getSliderValue().toInt(), _textInput, DateTime.now());
2023-02-22 01:28:01 +01:00
DatabaseService.instance.addMood(mood);
2023-02-15 23:28:19 +01:00
_moodFormKey.currentState?.reset();
}
}
void onFormFieldSave(String? newValue) => _textInput = newValue!;
2023-02-15 23:02:29 +01:00
@override
Widget build(BuildContext context) {
return Form(
key: _moodFormKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-02-21 20:56:10 +01:00
ElevatedCard(
title: 'Stimmungsbewertung',
child: slider,
),
const SizedBox(height: 16),
ElevatedCard(
title: 'Beschreibe deine Stimmung',
child:
MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave),
),
2023-02-15 23:28:19 +01:00
SubmitFormButton(submitForm)
2023-02-15 23:02:29 +01:00
],
));
}
}