ModernMemoires/lib/utils/widgets/question_slider_widget.dart

160 lines
5.8 KiB
Dart
Raw Normal View History

2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
2024-01-09 12:34:44 +01:00
import 'package:moody/utils/logic/question_generator.dart';
2023-12-25 14:10:31 +01:00
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
2024-01-09 12:34:44 +01:00
const QuestionSliderWidget({
2023-12-17 23:00:31 +01:00
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
2024-01-09 12:34:44 +01:00
State<QuestionSliderWidget> createState() => _QuestionSliderWidget();
2023-12-17 23:00:31 +01:00
}
2024-01-09 12:34:44 +01:00
class _QuestionSliderWidget extends State<QuestionSliderWidget> {
2024-01-01 18:31:10 +01:00
bool _showDragText = true;
2024-01-08 19:34:48 +01:00
SliderChangeData? _sliderData;
2023-12-25 14:10:31 +01:00
QuestionGenerator generator = QuestionGenerator();
2024-01-08 19:34:48 +01:00
final double changedInitValue = 0.0;
bool initValSet = false;
2023-12-17 23:00:31 +01:00
@override
void initState() {
super.initState();
2024-01-08 19:34:48 +01:00
//_sliderData?.value = widget.initialSliderValue;
_sliderData = SliderChangeData(widget.initialSliderValue, 0);
if (!widget.isSliderEnabled) {
_showDragText = false;
}
2023-12-17 23:00:31 +01:00
}
@override
Widget build(BuildContext context) {
2024-01-08 19:34:48 +01:00
if (!widget.isSliderEnabled) {
_sliderData = SliderChangeData(widget.initialSliderValue, 0);
} else {
if (!initValSet && changedInitValue != widget.initialSliderValue) {
initValSet = true;
changedInitValue != widget.initialSliderValue;
_sliderData?.value = widget.initialSliderValue;
}
}
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),
2024-01-09 12:34:44 +01:00
style: const TextStyle(fontSize: 18),
2023-12-25 14:10:31 +01:00
),
2024-01-09 12:34:44 +01:00
const SizedBox(height: 10),
2023-12-25 14:10:31 +01:00
Text(
2024-01-08 19:34:48 +01:00
widget.questionText == "" ? generator.getQuestionByDate(widget.date) : widget.questionText,
2024-01-09 12:34:44 +01:00
style: const 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,
2024-01-09 12:34:44 +01:00
trackShape: const RectangularSliderTrackShape(),
2024-01-08 19:34:48 +01:00
inactiveTickMarkColor: Colors.transparent,
2023-12-25 14:10:31 +01:00
inactiveTrackColor: Colors.transparent,
2024-01-08 19:34:48 +01:00
valueIndicatorColor: Colors.transparent,
2023-12-25 14:10:31 +01:00
overlayColor: Colors.transparent,
2024-01-08 19:34:48 +01:00
activeTickMarkColor: Colors.transparent,
2023-12-25 14:10:31 +01:00
showValueIndicator: ShowValueIndicator.always,
2024-01-08 19:34:48 +01:00
disabledInactiveTrackColor: Colors.transparent,
2023-12-25 14:10:31 +01:00
trackHeight: 2,
thumbColor: Colors.transparent,
2024-01-08 19:34:48 +01:00
thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 10.0, elevation: 0, disabledThumbRadius: 0),
rangeThumbShape: const RoundRangeSliderThumbShape(
2023-12-25 14:10:31 +01:00
disabledThumbRadius: 0,
enabledThumbRadius: 0,
elevation: 0,
),
),
2024-01-01 18:31:10 +01:00
child: Slider(
min: 0.0,
max: 100.0,
2024-01-08 19:34:48 +01:00
value: _sliderData!.value,
2024-01-01 18:31:10 +01:00
onChanged: widget.isSliderEnabled
? (value) {
setState(() {
2024-01-08 19:34:48 +01:00
_sliderData?.position = _calculateSliderPosition(context, value);
_sliderData?.value = value;
2024-01-01 18:31:10 +01:00
_showDragText = false;
});
2024-01-08 19:34:48 +01:00
widget.onSliderChanged(SliderChangeData(_sliderData!.value, _sliderData!.position));
2024-01-01 18:31:10 +01:00
}
: null,
2023-12-17 23:00:31 +01:00
),
),
2024-01-01 18:31:10 +01:00
),
Positioned(
2024-01-08 19:34:48 +01:00
left: _calculateSliderPosition(context, _sliderData!.value) + 10,
top: 12,
2024-01-01 18:31:10 +01:00
child: IgnorePointer(
2024-01-08 19:34:48 +01:00
child: (_showDragText && _sliderData!.value == 0)
2024-01-01 18:31:10 +01:00
? Text(
2024-01-08 19:34:48 +01:00
"drag me -->",
style: TextStyle(fontSize: 16, color: widget.sliderColor),
2024-01-01 18:31:10 +01:00
)
: Text(
2024-01-08 19:34:48 +01:00
"${_sliderData!.value.toStringAsFixed(0)}%",
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) {
2024-01-08 19:34:48 +01:00
double sliderWidth = MediaQuery.of(context).size.width - 60; // Width minus padding
2023-12-17 23:00:31 +01:00
double thumbWidth = 48; // Estimate of the thumb width
2024-01-08 19:34:48 +01:00
return ((sliderWidth - thumbWidth) * value / 100.0 + thumbWidth / 2) + 20;
2023-12-17 23:00:31 +01:00
}
}
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);
}