2023-02-15 14:30:40 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2023-02-15 23:02:29 +01:00
|
|
|
double _currentSliderValue = 50;
|
|
|
|
|
2023-02-15 14:30:40 +01:00
|
|
|
class MySlider extends StatefulWidget {
|
2023-02-16 12:00:02 +01:00
|
|
|
final String _title;
|
2023-02-21 13:39:06 +01:00
|
|
|
const MySlider(this._title, {Key? key}) : super(key: key);
|
2023-02-15 14:30:40 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => SliderState();
|
2023-02-15 23:02:29 +01:00
|
|
|
|
|
|
|
double getSliderValue() {
|
|
|
|
return _currentSliderValue;
|
|
|
|
}
|
2023-02-15 14:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-15 14:30:40 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-21 13:39:06 +01:00
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(widget._title),
|
|
|
|
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(
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-02-15 14:30:40 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|