fix slider bug

main
Julian Gegner 2023-02-26 10:44:16 +01:00
parent b38213a1a4
commit 449230e8d9
4 changed files with 15 additions and 15 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() {

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,8 +29,8 @@ 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(labelText: 'Motivation'),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),

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),

View File

@ -1,10 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
double _currentSliderValue = 50;
class MySlider extends StatefulWidget { class MySlider extends StatefulWidget {
double _currentSliderValue = 50;
final String _labelText; final String _labelText;
const MySlider({Key? key, String labelText = 'Stimmung'}) MySlider({Key? key, String labelText = 'Stimmung'})
: _labelText = labelText, : _labelText = labelText,
super(key: key); super(key: key);
@ -23,7 +22,7 @@ class SliderState extends State<MySlider> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_textFieldController.text = _currentSliderValue.toStringAsFixed(0); _textFieldController.text = widget._currentSliderValue.toStringAsFixed(0);
} }
@override @override
@ -38,15 +37,16 @@ class SliderState extends State<MySlider> {
children: [ children: [
Expanded( Expanded(
child: Slider( child: Slider(
value: _currentSliderValue, value: widget._currentSliderValue,
min: 1, min: 1,
max: 100, max: 100,
divisions: 99, divisions: 99,
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); _textFieldController.text =
widget._currentSliderValue.toStringAsFixed(0);
_errorText = null; _errorText = null;
}); });
}, },
@ -65,7 +65,7 @@ class SliderState extends State<MySlider> {
onChanged: (text) { onChanged: (text) {
if (text.isEmpty) { if (text.isEmpty) {
setState(() { setState(() {
_currentSliderValue = 1; widget._currentSliderValue = 1;
_textFieldController.clear(); _textFieldController.clear();
_errorText = null; _errorText = null;
}); });
@ -80,7 +80,7 @@ class SliderState extends State<MySlider> {
return; return;
} }
setState(() { setState(() {
_currentSliderValue = value; widget._currentSliderValue = value;
_errorText = null; _errorText = null;
}); });
}, },