cpd_2022_zi/lib/widgets/slider.dart

85 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2023-02-15 23:02:29 +01:00
double _currentSliderValue = 50;
class MySlider extends StatefulWidget {
final String _title;
const MySlider(this._title, {super.key});
@override
State<StatefulWidget> createState() => SliderState();
2023-02-15 23:02:29 +01:00
double getSliderValue() {
return _currentSliderValue;
}
}
class SliderState extends State<MySlider> {
2023-02-20 23:59:40 +01:00
TextEditingController _textFieldController = TextEditingController();
String? _errorText;
@override
void initState() {
super.initState();
_textFieldController.text = _currentSliderValue.toString();
}
@override
Widget build(BuildContext context) {
2023-02-15 23:02:29 +01:00
return Column(
children: [
Text(widget._title),
2023-02-15 23:02:29 +01:00
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Slider(
value: _currentSliderValue,
2023-02-20 23:59:40 +01:00
min: 1,
2023-02-15 23:02:29 +01:00
max: 100,
2023-02-20 23:59:40 +01:00
divisions: 99,
2023-02-15 23:02:29 +01:00
label: _currentSliderValue.round().toString(),
2023-02-20 23:59:40 +01:00
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
_textFieldController.text = _currentSliderValue.toString();
_errorText = null;
});
}),
2023-02-15 23:02:29 +01:00
Text(_currentSliderValue.round().toString())
],
2023-02-20 23:59:40 +01:00
),
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;
});
},
),
],
);
}
}