Parricc35 2023-02-21 00:20:43 +01:00
parent 79c5b8fa4b
commit f0e2ce16f2
1 changed files with 50 additions and 40 deletions

View File

@ -32,52 +32,62 @@ class SliderState extends State<MySlider> {
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Slider(
value: _currentSliderValue,
min: 1,
max: 100,
divisions: 99,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
SliderTheme(
data: SliderThemeData(
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 8.0),
trackHeight: 2.0,
),
child: Slider(
value: _currentSliderValue,
min: 1,
max: 100,
divisions: 99,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
_textFieldController.text =
_currentSliderValue.toString();
_errorText = null;
});
}),
),
SizedBox(width: 16.0),
SizedBox(
width: 50,
child: TextFormField(
controller: _textFieldController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter value',
errorText: _errorText,
),
onChanged: (text) {
if (text.isEmpty) {
_currentSliderValue = 0;
setState(() {
_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;
_textFieldController.text = _currentSliderValue.toString();
_errorText = null;
});
}),
Text(_currentSliderValue.round().toString())
},
),
),
],
),
SizedBox(height: 16),
TextFormField(
controller: _textFieldController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter value',
errorText: _errorText,
),
onChanged: (text) {
if (text.isEmpty) {
_currentSliderValue = 0;
setState(() {
_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;
});
},
),
],
);
}