diff --git a/lib/pages/sleep_page.dart b/lib/pages/sleep_page.dart index e736f44..ebb89d4 100644 --- a/lib/pages/sleep_page.dart +++ b/lib/pages/sleep_page.dart @@ -6,6 +6,6 @@ class SleepPage extends StatelessWidget { @override Widget build(BuildContext context) { - return const Center(child: SleepForm()); + return Center(child: SleepForm()); } } diff --git a/lib/widgets/sleep_form.dart b/lib/widgets/sleep_form.dart index c56b550..e11dba7 100644 --- a/lib/widgets/sleep_form.dart +++ b/lib/widgets/sleep_form.dart @@ -16,10 +16,14 @@ class _SleepFormState extends State { final GlobalKey _sleepFormKey = GlobalKey(); MySlider slider = const MySlider('Bewerte deinen Schlaf'); String _textInput = ""; - TimePicker sleepTimePicker = - const TimePicker(TimeOfDay(hour: 22, minute: 00)); - TimePicker wakeUpTimePicker = - const TimePicker(TimeOfDay(hour: 8, minute: 00)); + TimePicker sleepTimePicker = TimePicker( + const TimeOfDay(hour: 22, minute: 00), + descriptionText: 'eingeschlafen um', + ); + TimePicker wakeUpTimePicker = TimePicker( + const TimeOfDay(hour: 8, minute: 00), + descriptionText: 'aufgewacht um', + ); void submitForm() { if (_sleepFormKey.currentState!.validate()) { @@ -27,6 +31,7 @@ class _SleepFormState extends State { //TODO Businesslogik aufrufen! print(_textInput); print(slider.getSliderValue()); + print('Eingeschlafen um: ${sleepTimePicker.getCurrentTime}'); _sleepFormKey.currentState?.reset(); } } @@ -40,7 +45,6 @@ class _SleepFormState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - TimePicker(TimeOfDay(hour: 22, minute: 00)), ElevatedCard(sleepTimePicker), ElevatedCard(wakeUpTimePicker), ElevatedCard(slider), diff --git a/lib/widgets/timepicker.dart b/lib/widgets/timepicker.dart index 051ee2f..98f6a54 100644 --- a/lib/widgets/timepicker.dart +++ b/lib/widgets/timepicker.dart @@ -1,33 +1,32 @@ import 'package:flutter/material.dart'; +// ignore: must_be_immutable class TimePicker extends StatefulWidget { - final TimeOfDay _initialTime; - const TimePicker(this._initialTime, {super.key}); + TimeOfDay _initialTime; + final String descriptionText; + TimePicker(this._initialTime, {super.key, required this.descriptionText}); + + TimeOfDay get getCurrentTime => _initialTime; @override - State createState() => _TimePickerState(); + State createState() => TimePickerState(); } -class _TimePickerState extends State { - TimeOfDay time = const TimeOfDay(hour: 12, minute: 0); +class TimePickerState extends State { + TimePickerState(); @override Widget build(BuildContext context) { - time = widget._initialTime; - final hours = time.hour.toString().padLeft(2, '0'); - final minutes = time.minute.toString().padLeft(2, '0'); - return Center( child: Column(children: [ - const Text( - 'Das ist die Zeit', - style: TextStyle(fontSize: 18), + Text( + widget.descriptionText, + style: TextStyle(fontSize: 12), ), - const SizedBox(height: 16), Row(mainAxisAlignment: MainAxisAlignment.center, children: [ Text( - '${time.hour}:${time.minute}', - style: const TextStyle(fontSize: 32), + '${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}', + style: const TextStyle(fontSize: 22), ), const SizedBox(width: 16), ElevatedButton( @@ -35,7 +34,7 @@ class _TimePickerState extends State { //TODO auslagern TimeOfDay? newTime = await showTimePicker( context: context, - initialTime: time, + initialTime: widget._initialTime, builder: (context, child) { return MediaQuery( data: MediaQuery.of(context) @@ -46,8 +45,7 @@ class _TimePickerState extends State { ); if (newTime == null) return; setState(() { - time = newTime; - print('Zeit geƤndert $newTime, Zeit aktualisiert?: $time'); + widget._initialTime = newTime; }); }, child: const Text('Zeit einstellen'))