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
//TODO Businesslogik aufrufen!
print(_textInput);
print(slider.getSliderValue());
print(slider.sliderValue);
_moodFormKey.currentState?.reset();
}
}

View File

@ -31,7 +31,7 @@ class TimerStartStopPopupState extends State<TimerStartStopPopup> {
children: [
Padding(
padding: const EdgeInsets.only(top: 8),
child: MySlider(labelText: 'Motivation'),
child: MySlider(),
),
const SizedBox(height: 16),
MyTextFormField('Beschreibe deinen Motivation', onFormFieldSave),

View File

@ -28,7 +28,7 @@ class _SleepFormState extends State<SleepForm> {
_sleepFormKey.currentState?.save(); //call every onSave Method
//TODO Businesslogik aufrufen!
print(_textInput);
print(slider.getSliderValue());
print(slider.sliderValue);
print('Eingeschlafen um: ${sleepTimePicker.getCurrentTime}');
_sleepFormKey.currentState?.reset();
}

View File

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