cpd_2022_zi/lib/widgets/slider.dart

95 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
double _currentSliderValue = 50;
class MySlider extends StatefulWidget {
final String _title;
const MySlider(this._title, {Key? key}) : super(key: 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.toStringAsFixed(0);
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(widget._title),
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: 'Stimmung',
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;
});
},
),
),
],
),
],
),
);
}
}