ModernMemoires/lib/utils/widgets/QuestionSliderWidget.dart

150 lines
5.3 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';
2023-12-17 23:00:31 +01:00
class QuestionSliderWidget extends StatefulWidget {
final DateTime date;
final String questionText;
final double initialSliderValue;
final bool isSliderEnabled;
2024-01-01 18:31:10 +01:00
final ValueChanged<SliderChangeData> onSliderChanged;
final ValueChanged<double> onSliderPositionChanged;
2024-01-01 19:52:42 +01:00
final Color sliderColor; // parameter for the color
2023-12-17 23:00:31 +01:00
QuestionSliderWidget({
Key? key,
required this.date,
required this.questionText,
required this.initialSliderValue,
required this.isSliderEnabled,
required this.onSliderChanged,
2024-01-01 18:31:10 +01:00
required this.onSliderPositionChanged,
2024-01-01 19:52:42 +01:00
required this.sliderColor, // Initialize the color
2023-12-17 23:00:31 +01:00
}) : super(key: key);
@override
_QuestionSliderWidgetState createState() => _QuestionSliderWidgetState();
}
class _QuestionSliderWidgetState extends State<QuestionSliderWidget> {
2024-01-01 18:31:10 +01:00
bool _showDragText = true;
SliderChangeData _sliderData = SliderChangeData(0, 0);
2023-12-25 14:10:31 +01:00
QuestionGenerator generator = QuestionGenerator();
2023-12-17 23:00:31 +01:00
@override
void initState() {
super.initState();
2024-01-01 18:31:10 +01:00
_sliderData.value = widget.initialSliderValue;
2023-12-17 23:00:31 +01:00
}
@override
Widget build(BuildContext context) {
2024-01-01 18:31:10 +01:00
return Padding(
padding: const EdgeInsets.fromLTRB(0, 150, 20, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// Date
2023-12-25 14:10:31 +01:00
Padding(
2024-01-01 18:31:10 +01:00
padding: const EdgeInsets.fromLTRB(30, 0, 0, 30),
2023-12-25 14:10:31 +01:00
child: Column(
2024-01-01 18:31:10 +01:00
mainAxisAlignment: MainAxisAlignment.start,
2023-12-25 14:10:31 +01:00
crossAxisAlignment: CrossAxisAlignment.start,
2024-01-01 18:31:10 +01:00
children: [
2023-12-25 14:10:31 +01:00
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
),
2024-01-01 18:31:10 +01:00
],
),
),
// Slider and label
Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(15, 0, 0, 15),
child: SliderTheme(
2023-12-25 14:10:31 +01:00
data: SliderTheme.of(context).copyWith(
2024-01-01 18:31:10 +01:00
disabledThumbColor: Colors.transparent,
2024-01-01 19:52:42 +01:00
activeTrackColor: widget.sliderColor,
2023-12-25 14:10:31 +01:00
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(
2024-01-01 18:31:10 +01:00
enabledThumbRadius: 10.0,
2023-12-25 14:10:31 +01:00
elevation: 0,
disabledThumbRadius: 0),
rangeThumbShape: RoundRangeSliderThumbShape(
disabledThumbRadius: 0,
enabledThumbRadius: 0,
elevation: 0,
),
),
2024-01-01 18:31:10 +01:00
child: Slider(
min: 0.0,
max: 100.0,
value: _sliderData.value,
onChanged: widget.isSliderEnabled
? (value) {
setState(() {
_sliderData.position =
_calculateSliderPosition(context, value);
_sliderData.value = value;
_showDragText = false;
});
widget.onSliderChanged(SliderChangeData(
_sliderData.value, _sliderData.position));
}
: null,
2023-12-17 23:00:31 +01:00
),
),
2024-01-01 18:31:10 +01:00
),
Positioned(
left: _calculateSliderPosition(context, _sliderData.value),
child: IgnorePointer(
child: _showDragText
? Text(
"Drag Me -->",
2024-01-01 19:52:42 +01:00
style: TextStyle(
fontSize: 16, color: widget.sliderColor),
2024-01-01 18:31:10 +01:00
)
: Text(
"${_sliderData.value.toStringAsFixed(0)}%",
2024-01-01 19:52:42 +01:00
style: TextStyle(
fontSize: 20, color: widget.sliderColor),
2024-01-01 18:31:10 +01:00
),
),
),
],
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;
}
}
2024-01-01 18:31:10 +01:00
class SliderChangeData {
double value;
double position; // Additional data you might want to include
SliderChangeData(this.value, this.position);
}