cpd_2022_zi/lib/widgets/slider.dart

96 lines
2.9 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 {
2023-02-21 22:33:03 +01:00
final String _labelText;
const MySlider({Key? key, String labelText = 'Stimmung'})
: _labelText = labelText,
super(key: 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();
2023-02-21 13:39:06 +01:00
_textFieldController.text = _currentSliderValue.toStringAsFixed(0);
2023-02-20 23:59:40 +01:00
}
@override
Widget build(BuildContext context) {
2023-02-21 13:39:06 +01:00
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Slider(
2023-02-21 00:20:43 +01:00
value: _currentSliderValue,
min: 1,
max: 100,
divisions: 99,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
2023-02-21 13:39:06 +01:00
_textFieldController.text = _currentSliderValue.toStringAsFixed(0);
2023-02-21 00:20:43 +01:00
_errorText = null;
});
2023-02-21 13:39:06 +01:00
},
2023-02-21 00:20:43 +01:00
),
2023-02-21 13:39:06 +01:00
),
SizedBox(width: 16),
SizedBox(
width: 100,
child: TextFormField(
controller: _textFieldController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
2023-02-21 22:33:03 +01:00
labelText: widget._labelText,
2023-02-21 13:39:06 +01:00
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;
}
2023-02-21 00:20:43 +01:00
setState(() {
2023-02-21 13:39:06 +01:00
_currentSliderValue = value;
2023-02-21 00:20:43 +01:00
_errorText = null;
});
2023-02-21 13:39:06 +01:00
},
),
2023-02-21 00:20:43 +01:00
),
2023-02-21 13:39:06 +01:00
],
),
],
),
);
}
}