Merge branch 'prettier-slider' into 'main'

Prettier slider

Closes #28

See merge request Crondung/hsma_cpd!12
main
Kai Mannweiler 2023-02-26 11:48:10 +00:00
commit 60d8c6653a
4 changed files with 35 additions and 67 deletions

View File

@ -14,7 +14,7 @@ class MoodForm extends StatefulWidget {
class _MoodFormState extends State<MoodForm> { class _MoodFormState extends State<MoodForm> {
final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>(); final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>();
MySlider slider = const MySlider(); MySlider slider = MySlider();
String _textInput = ""; String _textInput = "";
void submitForm() { void submitForm() {
@ -22,7 +22,7 @@ class _MoodFormState extends State<MoodForm> {
_moodFormKey.currentState?.save(); //call every onSave Method _moodFormKey.currentState?.save(); //call every onSave Method
//TODO Businesslogik aufrufen! //TODO Businesslogik aufrufen!
print(_textInput); print(_textInput);
print(slider.getSliderValue()); print(slider.sliderValue);
_moodFormKey.currentState?.reset(); _moodFormKey.currentState?.reset();
} }
} }

View File

@ -13,7 +13,7 @@ class TimerStartStopPopup extends StatefulWidget {
} }
class TimerStartStopPopupState extends State<TimerStartStopPopup> { class TimerStartStopPopupState extends State<TimerStartStopPopup> {
final MySlider slider = const MySlider(); final MySlider slider = MySlider();
void submitForm(BuildContext context) { void submitForm(BuildContext context) {
Navigator.of(context).pop(); Navigator.of(context).pop();
@ -29,9 +29,9 @@ class TimerStartStopPopupState extends State<TimerStartStopPopup> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const Padding( Padding(
padding: EdgeInsets.only(top: 8), padding: const EdgeInsets.only(top: 8),
child: MySlider(labelText: 'Motivation'), child: MySlider(),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
MyTextFormField('Beschreibe deinen Motivation', onFormFieldSave), MyTextFormField('Beschreibe deinen Motivation', onFormFieldSave),

View File

@ -14,7 +14,7 @@ class SleepForm extends StatefulWidget {
class _SleepFormState extends State<SleepForm> { class _SleepFormState extends State<SleepForm> {
final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>(); final GlobalKey<FormState> _sleepFormKey = GlobalKey<FormState>();
MySlider slider = const MySlider(); MySlider slider = MySlider();
String _textInput = ""; String _textInput = "";
TimePicker sleepTimePicker = TimePicker( TimePicker sleepTimePicker = TimePicker(
const TimeOfDay(hour: 22, minute: 00), const TimeOfDay(hour: 22, minute: 00),
@ -28,7 +28,7 @@ class _SleepFormState extends State<SleepForm> {
_sleepFormKey.currentState?.save(); //call every onSave Method _sleepFormKey.currentState?.save(); //call every onSave Method
//TODO Businesslogik aufrufen! //TODO Businesslogik aufrufen!
print(_textInput); print(_textInput);
print(slider.getSliderValue()); print(slider.sliderValue);
print('Eingeschlafen um: ${sleepTimePicker.getCurrentTime}'); print('Eingeschlafen um: ${sleepTimePicker.getCurrentTime}');
_sleepFormKey.currentState?.reset(); _sleepFormKey.currentState?.reset();
} }

View File

@ -1,90 +1,58 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
double _currentSliderValue = 50; // ignore: must_be_immutable
class MySlider extends StatefulWidget { class MySlider extends StatefulWidget {
final String _labelText; double _currentSliderValue = 50;
const MySlider({Key? key, String labelText = 'Stimmung'}) MySlider({Key? key}) : super(key: key);
: _labelText = labelText,
super(key: key);
@override @override
State<StatefulWidget> createState() => SliderState(); State<StatefulWidget> createState() => SliderState();
double getSliderValue() { double get sliderValue => _currentSliderValue;
return _currentSliderValue;
}
} }
class SliderState extends State<MySlider> { class SliderState extends State<MySlider> {
TextEditingController _textFieldController = TextEditingController();
String? _errorText;
@override
void initState() {
super.initState();
_textFieldController.text = _currentSliderValue.toStringAsFixed(0);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
SizedBox(height: 16), const SizedBox(height: 16),
Text('${widget._currentSliderValue.toInt()}',
style: const TextStyle(fontSize: 22)),
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
IconButton(
icon: const Icon(Icons.remove_outlined),
onPressed: () {
setState(() {
widget._currentSliderValue -= 1;
});
},
),
Expanded( Expanded(
child: Slider( child: Slider(
value: _currentSliderValue, value: widget._currentSliderValue,
min: 1, min: 0,
max: 100, max: 100,
divisions: 99, divisions: 100,
label: _currentSliderValue.round().toString(), label: widget._currentSliderValue.round().toString(),
onChanged: (double value) { onChanged: (double value) {
setState(() { setState(() {
_currentSliderValue = value; widget._currentSliderValue = value;
_textFieldController.text = _currentSliderValue.toStringAsFixed(0);
_errorText = null;
}); });
}, },
), ),
), ),
SizedBox(width: 16), IconButton(
SizedBox( icon: const Icon(Icons.add_outlined),
width: 100, onPressed: () {
child: TextFormField( setState(() {
controller: _textFieldController, widget._currentSliderValue += 1;
keyboardType: TextInputType.number, });
decoration: InputDecoration( },
labelText: widget._labelText,
errorText: _errorText,
),
onChanged: (text) {
if (text.isEmpty) {
setState(() {
_currentSliderValue = 1;
_textFieldController.clear();
_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;
});
},
),
), ),
], ],
), ),