import 'package:flutter/material.dart'; double _currentSliderValue = 50; class MySlider extends StatefulWidget { final String _labelText; const MySlider({Key? key, String labelText = 'Stimmung'}) : _labelText = labelText, super(key: key); @override State createState() => SliderState(); double getSliderValue() { return _currentSliderValue; } } class SliderState extends State { TextEditingController _textFieldController = TextEditingController(); String? _errorText; @override void initState() { super.initState(); _textFieldController.text = _currentSliderValue.toStringAsFixed(0); } @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox(height: 16), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Expanded( child: Slider( value: _currentSliderValue, min: 1, max: 100, divisions: 99, label: _currentSliderValue.round().toString(), onChanged: (double value) { setState(() { _currentSliderValue = value; _textFieldController.text = _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(() { _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; }); }, ), ), ], ), ], ), ); } }