Parricc35 2023-02-20 23:59:40 +01:00
parent c9e57b06d6
commit f5ef084997
5 changed files with 144 additions and 62 deletions

View File

@ -6,6 +6,6 @@ class SleepPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(child: SleepForm());
return const Center(child: SleepForm());
}
}

View File

@ -1,17 +1,37 @@
import 'package:flutter/material.dart';
class ElevatedCard extends StatelessWidget {
final Widget? _child;
const ElevatedCard(this._child, {super.key});
final Widget child;
final String title;
const ElevatedCard({
Key? key,
required this.child,
required this.title,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Card(
child: SizedBox(
width: 300,
height: 100,
child: _child,
return Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8.0),
child,
],
),
),
);

View File

@ -6,7 +6,7 @@ import 'package:smoke_cess_app/widgets/text_formfield.dart';
import 'package:smoke_cess_app/widgets/timepicker.dart';
class SleepForm extends StatefulWidget {
const SleepForm({super.key});
const SleepForm({Key? key}) : super(key: key);
@override
State<SleepForm> createState() => _SleepFormState();
@ -14,21 +14,18 @@ class SleepForm extends StatefulWidget {
class _SleepFormState extends State<SleepForm> {
final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>();
MySlider slider = const MySlider('Bewerte deinen Schlaf');
MySlider slider = const MySlider('');
String _textInput = "";
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()) {
_sleepFormKey.currentState?.save(); //call every onSave Method
//TODO Businesslogik aufrufen!
_sleepFormKey.currentState?.save();
print(_textInput);
print(slider.getSliderValue());
print('Eingeschlafen um: ${sleepTimePicker.getCurrentTime}');
@ -41,17 +38,43 @@ class _SleepFormState extends State<SleepForm> {
@override
Widget build(BuildContext context) {
return Form(
key: _sleepFormKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedCard(sleepTimePicker),
ElevatedCard(wakeUpTimePicker),
ElevatedCard(slider),
ElevatedCard(
MyTextFormField('Beschreibe deinen Schlaf', onFormFieldSave)),
SubmitFormButton(submitForm)
],
));
key: _sleepFormKey,
child: Stack(
children: [
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedCard(
child: sleepTimePicker,
title: 'Einschlafzeit',
),
const SizedBox(height: 16),
ElevatedCard(
child: wakeUpTimePicker,
title: 'Aufwachzeit',
),
const SizedBox(height: 16),
ElevatedCard(
child: slider,
title: 'Schlafbewertung',
),
const SizedBox(height: 16),
ElevatedCard(
child: MyTextFormField(
'Beschreibe deinen Schlaf', onFormFieldSave),
title: 'Schlafbeschreibung',
),
const SizedBox(
height: 32,
),
],
),
),
SubmitFormButton(submitForm),
],
),
);
}
}

View File

@ -15,6 +15,15 @@ class MySlider extends StatefulWidget {
}
class SliderState extends State<MySlider> {
TextEditingController _textFieldController = TextEditingController();
String? _errorText;
@override
void initState() {
super.initState();
_textFieldController.text = _currentSliderValue.toString();
}
@override
Widget build(BuildContext context) {
return Column(
@ -25,13 +34,50 @@ class SliderState extends State<MySlider> {
children: [
Slider(
value: _currentSliderValue,
min: 1,
max: 100,
divisions: 99,
label: _currentSliderValue.round().toString(),
onChanged: (double value) =>
{setState((() => _currentSliderValue = value))}),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
_textFieldController.text = _currentSliderValue.toString();
_errorText = null;
});
}),
Text(_currentSliderValue.round().toString())
],
)
),
SizedBox(height: 16),
TextFormField(
controller: _textFieldController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter value',
errorText: _errorText,
),
onChanged: (text) {
if (text.isEmpty) {
_currentSliderValue = 0;
setState(() {
_errorText = null;
});
return;
}
final value = double.tryParse(text);
if (value == null || value < 1 || value > 100) {
setState(() {
_textFieldController.clear();
_errorText = 'Please enter a value between 1 and 100.';
});
return;
}
setState(() {
_currentSliderValue = value;
_errorText = null;
});
},
),
],
);
}

View File

@ -3,8 +3,7 @@ import 'package:flutter/material.dart';
// ignore: must_be_immutable
class TimePicker extends StatefulWidget {
TimeOfDay _initialTime;
final String descriptionText;
TimePicker(this._initialTime, {super.key, required this.descriptionText});
TimePicker(this._initialTime, {Key? key});
TimeOfDay get getCurrentTime => _initialTime;
@ -18,38 +17,32 @@ class TimePickerState extends State<TimePicker> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(children: [
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
widget.descriptionText,
style: TextStyle(fontSize: 12),
'${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 22),
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 22),
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () async {
//TODO auslagern
TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: widget._initialTime,
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime == null) return;
setState(() {
widget._initialTime = newTime;
});
},
child: const Text('Zeit einstellen'))
])
const SizedBox(width: 16),
ElevatedButton(
onPressed: () async {
//TODO auslagern
TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: widget._initialTime,
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime == null) return;
setState(() {
widget._initialTime = newTime;
});
},
child: const Text('Zeit einstellen'))
]),
);
}