ModernMemoires/lib/utils/widgets/QuestionSliderWidget.dart

131 lines
4.5 KiB
Dart
Raw Normal View History

2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
2023-12-25 14:10:31 +01:00
import 'package:moody/utils/logic/QuestionGenerator.dart';
import '../CirclePainter.dart';
2023-12-17 23:00:31 +01:00
class QuestionSliderWidget extends StatefulWidget {
final DateTime date;
final String questionText;
final double initialSliderValue;
final bool isSliderEnabled;
final ValueChanged<double> onSliderChanged;
QuestionSliderWidget({
Key? key,
required this.date,
required this.questionText,
required this.initialSliderValue,
required this.isSliderEnabled,
required this.onSliderChanged,
}) : super(key: key);
@override
_QuestionSliderWidgetState createState() => _QuestionSliderWidgetState();
}
class _QuestionSliderWidgetState extends State<QuestionSliderWidget> {
late double _sliderValue;
bool _sliderChanged = false;
2023-12-25 14:10:31 +01:00
QuestionGenerator generator = QuestionGenerator();
2023-12-17 23:00:31 +01:00
@override
void initState() {
super.initState();
_sliderValue = widget.initialSliderValue;
}
@override
Widget build(BuildContext context) {
2023-12-25 14:10:31 +01:00
return Expanded(
child: Stack(
children: [
Positioned.fill(
top: 20,
left: _calculateSliderPosition(context, _sliderValue) - 60,
child: CustomPaint(
painter: CirclePainter(_sliderValue),
2023-12-17 23:00:31 +01:00
),
2023-12-25 14:10:31 +01:00
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 150, 20, 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// Date
Text(
DateFormat('dd MM yyyy').format(widget.date),
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
Text(
generator.getQuestionByDate(widget.date),
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
2023-12-17 23:00:31 +01:00
),
2023-12-25 14:10:31 +01:00
// Slider and label
SliderTheme(
data: SliderTheme.of(context).copyWith(
disabledThumbColor: Colors.amber,
activeTrackColor: Colors.black,
trackShape: RectangularSliderTrackShape(),
inactiveTickMarkColor: Colors.red,
inactiveTrackColor: Colors.transparent,
valueIndicatorColor: Colors.purple,
overlayColor: Colors.transparent,
activeTickMarkColor: Colors.red,
showValueIndicator: ShowValueIndicator.always,
trackHeight: 2,
thumbColor: Colors.transparent,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 30.0,
elevation: 0,
disabledThumbRadius: 0),
rangeThumbShape: RoundRangeSliderThumbShape(
disabledThumbRadius: 0,
enabledThumbRadius: 0,
elevation: 0,
),
),
child: Stack(
alignment: Alignment.centerLeft,
children: [
Slider(
min: 0.0,
max: 100.0,
value: _sliderValue,
onChanged: widget.isSliderEnabled
? (value) {
setState(() {
_sliderValue = value;
_sliderChanged = true;
});
widget.onSliderChanged(value);
}
: null,
),
Positioned(
left: _calculateSliderPosition(context, _sliderValue),
child: Text(
"${_sliderValue.toStringAsFixed(0)}%",
style: TextStyle(fontSize: 20), // Size increased by 2
),
),
],
2023-12-17 23:00:31 +01:00
),
),
],
),
),
],
),
);
}
double _calculateSliderPosition(BuildContext context, double value) {
double sliderWidth =
2023-12-25 14:10:31 +01:00
MediaQuery.of(context).size.width - 40; // Width minus padding
2023-12-17 23:00:31 +01:00
double thumbWidth = 48; // Estimate of the thumb width
return (sliderWidth - thumbWidth) * value / 100.0 + thumbWidth / 2;
}
}