2023-02-16 12:00:02 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-26 14:59:37 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2023-02-25 18:54:54 +01:00
|
|
|
import 'package:smoke_cess_app/models/sleep.dart';
|
2023-02-26 14:10:06 +01:00
|
|
|
import 'package:smoke_cess_app/services/database_service.dart';
|
2023-02-17 11:35:05 +01:00
|
|
|
import 'package:smoke_cess_app/widgets/elevated_card.dart';
|
2023-02-16 12:00:02 +01:00
|
|
|
import 'package:smoke_cess_app/widgets/slider.dart';
|
|
|
|
import 'package:smoke_cess_app/widgets/submit_form_button.dart';
|
|
|
|
import 'package:smoke_cess_app/widgets/text_formfield.dart';
|
2023-02-17 11:35:05 +01:00
|
|
|
import 'package:smoke_cess_app/widgets/timepicker.dart';
|
2023-02-16 12:00:02 +01:00
|
|
|
|
2023-02-26 14:59:37 +01:00
|
|
|
import '../providers/input_provider.dart';
|
|
|
|
|
2023-02-16 12:00:02 +01:00
|
|
|
class SleepForm extends StatefulWidget {
|
2023-02-20 23:59:40 +01:00
|
|
|
const SleepForm({Key? key}) : super(key: key);
|
2023-02-16 12:00:02 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<SleepForm> createState() => _SleepFormState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SleepFormState extends State<SleepForm> {
|
|
|
|
final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>();
|
2023-02-26 10:44:16 +01:00
|
|
|
MySlider slider = MySlider();
|
2023-02-16 12:00:02 +01:00
|
|
|
String _textInput = "";
|
2023-02-20 23:23:37 +01:00
|
|
|
TimePicker sleepTimePicker = TimePicker(
|
|
|
|
const TimeOfDay(hour: 22, minute: 00),
|
|
|
|
);
|
|
|
|
TimePicker wakeUpTimePicker = TimePicker(
|
|
|
|
const TimeOfDay(hour: 8, minute: 00),
|
|
|
|
);
|
2023-02-16 12:00:02 +01:00
|
|
|
|
2023-02-26 14:59:37 +01:00
|
|
|
void submitForm(BuildContext context) {
|
|
|
|
var inputModel = context.watch<InputProvider>();
|
2023-02-16 12:00:02 +01:00
|
|
|
if (_sleepFormKey.currentState!.validate()) {
|
|
|
|
_sleepFormKey.currentState?.save(); //call every onSave Method
|
2023-02-25 18:54:54 +01:00
|
|
|
Sleep sleep = Sleep(
|
2023-02-26 14:59:37 +01:00
|
|
|
inputModel.sliderValue.toInt(),
|
2023-02-25 18:54:54 +01:00
|
|
|
_textInput,
|
|
|
|
DateTime.now(),
|
|
|
|
sleepTimePicker.getCurrentTime,
|
|
|
|
wakeUpTimePicker.getCurrentTime);
|
|
|
|
DatabaseService.instance.addSleep(sleep);
|
2023-02-16 12:00:02 +01:00
|
|
|
_sleepFormKey.currentState?.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onFormFieldSave(String? newValue) => _textInput = newValue!;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Form(
|
2023-02-20 23:59:40 +01:00
|
|
|
key: _sleepFormKey,
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
ElevatedCard(
|
|
|
|
title: 'Einschlafzeit',
|
2023-02-21 13:39:06 +01:00
|
|
|
child: sleepTimePicker,
|
2023-02-20 23:59:40 +01:00
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
ElevatedCard(
|
|
|
|
title: 'Aufwachzeit',
|
2023-02-21 13:39:06 +01:00
|
|
|
child: wakeUpTimePicker,
|
2023-02-20 23:59:40 +01:00
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
ElevatedCard(
|
|
|
|
title: 'Schlafbewertung',
|
2023-02-21 13:39:06 +01:00
|
|
|
child: slider,
|
2023-02-20 23:59:40 +01:00
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2023-02-26 16:17:57 +01:00
|
|
|
const ElevatedCard(
|
2023-02-21 13:39:06 +01:00
|
|
|
title: 'Schlafbeschreibung',
|
2023-02-26 16:17:57 +01:00
|
|
|
child: MyTextFormField('Beschreibe deinen Schlaf'),
|
2023-02-20 23:59:40 +01:00
|
|
|
),
|
|
|
|
const SizedBox(
|
2023-02-21 13:39:06 +01:00
|
|
|
height: 80,
|
2023-02-20 23:59:40 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2023-02-21 13:39:06 +01:00
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
child: SizedBox(
|
|
|
|
width: 140,
|
|
|
|
height: 80,
|
2023-02-26 14:59:37 +01:00
|
|
|
child: SubmitFormButton(() => submitForm(context)),
|
2023-02-21 13:39:06 +01:00
|
|
|
),
|
|
|
|
),
|
2023-02-20 23:59:40 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2023-02-16 12:00:02 +01:00
|
|
|
}
|
|
|
|
}
|