85 lines
2.3 KiB
Dart
85 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
double _currentSliderValue = 50;
|
|
|
|
class MySlider extends StatefulWidget {
|
|
final String _title;
|
|
const MySlider(this._title, {super.key});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => SliderState();
|
|
|
|
double getSliderValue() {
|
|
return _currentSliderValue;
|
|
}
|
|
}
|
|
|
|
class SliderState extends State<MySlider> {
|
|
TextEditingController _textFieldController = TextEditingController();
|
|
String? _errorText;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_textFieldController.text = _currentSliderValue.toString();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
Text(widget._title),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
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;
|
|
});
|
|
}),
|
|
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;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|