add iconbuttons to change slider value

main
Julian Gegner 2023-02-26 11:30:24 +01:00
parent 449230e8d9
commit f89fb41397
4 changed files with 26 additions and 58 deletions

View File

@ -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

@ -31,7 +31,7 @@ class TimerStartStopPopupState extends State<TimerStartStopPopup> {
children: [ children: [
Padding( Padding(
padding: const 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

@ -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';
// ignore: must_be_immutable
class MySlider extends StatefulWidget { class MySlider extends StatefulWidget {
double _currentSliderValue = 50; double _currentSliderValue = 50;
final String _labelText; MySlider({Key? key}) : super(key: key);
MySlider({Key? key, String labelText = 'Stimmung'})
: _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 = widget._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: widget._currentSliderValue, value: widget._currentSliderValue,
min: 1, min: 0,
max: 100, max: 100,
divisions: 99, divisions: 100,
label: widget._currentSliderValue.round().toString(), label: widget._currentSliderValue.round().toString(),
onChanged: (double value) { onChanged: (double value) {
setState(() { setState(() {
widget._currentSliderValue = value; widget._currentSliderValue = value;
_textFieldController.text =
widget._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(() {
widget._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(() {
widget._currentSliderValue = value;
_errorText = null;
});
},
),
), ),
], ],
), ),