98 lines
3.0 KiB
Dart
98 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:smoke_cess_app/models/sleep.dart';
|
|
import 'package:smoke_cess_app/services/database_service.dart';
|
|
import 'package:smoke_cess_app/widgets/elevated_card.dart';
|
|
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';
|
|
import 'package:smoke_cess_app/widgets/timepicker.dart';
|
|
|
|
import '../providers/input_provider.dart';
|
|
|
|
class SleepForm extends StatefulWidget {
|
|
const SleepForm({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SleepForm> createState() => _SleepFormState();
|
|
}
|
|
|
|
class _SleepFormState extends State<SleepForm> {
|
|
final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>();
|
|
MySlider slider = MySlider();
|
|
String _textInput = "";
|
|
TimePicker sleepTimePicker = TimePicker(
|
|
const TimeOfDay(hour: 22, minute: 00),
|
|
);
|
|
TimePicker wakeUpTimePicker = TimePicker(
|
|
const TimeOfDay(hour: 8, minute: 00),
|
|
);
|
|
|
|
void submitForm(BuildContext context) {
|
|
var inputModel = context.watch<InputProvider>();
|
|
if (_sleepFormKey.currentState!.validate()) {
|
|
_sleepFormKey.currentState?.save(); //call every onSave Method
|
|
Sleep sleep = Sleep(
|
|
inputModel.sliderValue.toInt(),
|
|
_textInput,
|
|
DateTime.now(),
|
|
sleepTimePicker.getCurrentTime,
|
|
wakeUpTimePicker.getCurrentTime);
|
|
DatabaseService.instance.addSleep(sleep);
|
|
_sleepFormKey.currentState?.reset();
|
|
}
|
|
}
|
|
|
|
void onFormFieldSave(String? newValue) => _textInput = newValue!;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Form(
|
|
key: _sleepFormKey,
|
|
child: Stack(
|
|
children: [
|
|
SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
ElevatedCard(
|
|
title: 'Einschlafzeit',
|
|
child: sleepTimePicker,
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedCard(
|
|
title: 'Aufwachzeit',
|
|
child: wakeUpTimePicker,
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedCard(
|
|
title: 'Schlafbewertung',
|
|
child: slider,
|
|
),
|
|
const SizedBox(height: 16),
|
|
const ElevatedCard(
|
|
title: 'Schlafbeschreibung',
|
|
child: MyTextFormField('Beschreibe deinen Schlaf'),
|
|
),
|
|
const SizedBox(
|
|
height: 80,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 0,
|
|
right: 0,
|
|
child: SizedBox(
|
|
width: 140,
|
|
height: 80,
|
|
child: SubmitFormButton(() => submitForm(context)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|