Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesbrandenburger committed Feb 20, 2024
2 parents f12e51b + c3aef8e commit 79240cb
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 75 deletions.
48 changes: 33 additions & 15 deletions frontend/lib/components/elements/feedback/slider_feedback.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class SliderFeedback extends StatefulWidget {
final int initialFeedback;
final String rangeLow;
final String rangeHigh;
final ValueChanged<int> onFeedbackChanged;

const SliderFeedback(
{super.key, this.initialFeedback = 5, required this.onFeedbackChanged});
{super.key,
this.initialFeedback = 5,
this.rangeLow = "0",
this.rangeHigh = "10",
required this.onFeedbackChanged});

@override
State<SliderFeedback> createState() => _SliderFeedbackState();
Expand Down Expand Up @@ -37,20 +44,31 @@ class _SliderFeedbackState extends State<SliderFeedback> {
}
}

return Slider(
value: _feedback,
min: 0,
max: 10,
divisions: 10,
onChanged: (newFeedback) {
setState(() {
_feedback = newFeedback;
_hasChanged = true;
});
widget.onFeedbackChanged(newFeedback.toInt());
},
activeColor: activeColor,
label: _feedback.toInt().toString(),
return Column(
children: [
Slider(
value: _feedback,
min: 0,
max: 10,
divisions: 10,
onChanged: (newFeedback) {
setState(() {
_feedback = newFeedback;
_hasChanged = true;
});
widget.onFeedbackChanged(newFeedback.toInt());
},
activeColor: activeColor,
label: _feedback.toInt().toString(),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(widget.rangeLow),
Text(widget.rangeHigh),
],
),
],
);
}
}
129 changes: 72 additions & 57 deletions frontend/lib/components/elements/feedback/slider_feedback_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import 'package:flutter/material.dart';

class SliderFeedbackResult extends StatefulWidget {
final List<int> results;
final String rangeLow;
final String rangeHigh;
final double average;
final int min;
final int max;

const SliderFeedbackResult({
super.key,
required this.results,
this.rangeLow = "0",
this.rangeHigh = "10",
required this.average,
required this.min,
required this.max,
Expand Down Expand Up @@ -71,64 +75,75 @@ class _SliderFeedbackResultState extends State<SliderFeedbackResult> {
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;

return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double width = constraints.maxWidth;
double height = width / 4;
double innerWidth = constraints.maxWidth - height;

return SizedBox(
width: width,
height: height,
child: Stack(
children: List.generate(_itemCount + 2, (index) {
// Horizontal line
if (index == 0) {
return Positioned(
left: height / 2,
top: height / 2 - 1,
child: Container(
width: innerWidth,
height: 2,
decoration: BoxDecoration(
color: colors.tertiary.withAlpha(48),
),
),
);
}
// Average line
if (index == _itemCount + 1) {
return Positioned(
left: height / 2 +
(widget.average - _min) * innerWidth / (_max - _min),
top: 0,
child: Container(
width: 2,
height: height,
decoration: BoxDecoration(
color: colors.secondary,
return Column(
children: [
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double width = constraints.maxWidth;
double height = width / 4;
double innerWidth = constraints.maxWidth - height;

return SizedBox(
width: width,
height: height,
child: Stack(
children: List.generate(_itemCount + 2, (index) {
// Horizontal line
if (index == 0) {
return Positioned(
left: height / 2,
top: height / 2 - 1,
child: Container(
width: innerWidth,
height: 2,
decoration: BoxDecoration(
color: colors.tertiary.withAlpha(48),
),
),
);
}
// Average line
if (index == _itemCount + 1) {
return Positioned(
left: height / 2 +
(widget.average - _min) * innerWidth / (_max - _min),
top: 0,
child: Container(
width: 2,
height: height,
decoration: BoxDecoration(
color: colors.secondary,
),
),
);
}
index--;
var size = 10 + _normCounts[index] * (height - 10);
return Positioned(
left:
height / 2 + (index) * innerWidth / (_max - _min) - size / 2,
top: height / 2 - size / 2,
child: Container(
width: size,
height: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors.primary.withOpacity(0.4),
),
),
),
);
}
index--;
var size = 10 + _normCounts[index] * (height - 10);
return Positioned(
left:
height / 2 + (index) * innerWidth / (_max - _min) - size / 2,
top: height / 2 - size / 2,
child: Container(
width: size,
height: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors.primary.withOpacity(0.4),
),
),
);
}),
);
}),
),
);
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(widget.rangeLow),
Text(widget.rangeHigh),
],
),
);
});
],
);
}
}
9 changes: 8 additions & 1 deletion frontend/lib/models/feedback/feedback_question.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import 'package:frontend/models/question.dart';

class FeedbackQuestion extends Question {

final String rangeLow;
final String rangeHigh;

FeedbackQuestion(
{required super.id,
required super.name,
required super.description,
required super.type,
required super.options});
required super.options,
required this.rangeLow,
required this.rangeHigh});

factory FeedbackQuestion.fromJson(Map<String, dynamic> json) {
return FeedbackQuestion(
Expand All @@ -16,6 +21,8 @@ class FeedbackQuestion extends Question {
description: json['description'],
type: json['type'],
options: json['options'].cast<String>(),
rangeLow: json['rangeLow'] ?? "0",
rangeHigh: json['rangeHigh'] ?? "10",
);
}
}
5 changes: 4 additions & 1 deletion frontend/lib/pages/feedback/attend_feedback_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:frontend/components/elements/feedback/slider_feedback.dart';
import 'package:frontend/components/elements/feedback/star_feedback.dart';
import 'package:frontend/global.dart';
import 'package:frontend/models/feedback/feedback_form.dart';
import 'package:frontend/models/feedback/feedback_question.dart';
import 'package:frontend/theme/assets.dart';
import 'package:frontend/utils.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
Expand Down Expand Up @@ -207,7 +208,7 @@ class _AttendFeedbackPageState extends State<AttendFeedbackPage> {
physics: const NeverScrollableScrollPhysics(),
itemCount: _form.questions.length,
itemBuilder: (context, index) {
var element = _form.questions[index];
final element = _form.questions[index] as FeedbackQuestion;
return Padding(
padding: const EdgeInsets.all(32.0),
child: Column(
Expand All @@ -230,6 +231,8 @@ class _AttendFeedbackPageState extends State<AttendFeedbackPage> {
else if (element.type == 'SLIDER')
SliderFeedback(
initialFeedback: _feedbackValues[element.id],
rangeLow: element.rangeLow,
rangeHigh: element.rangeHigh,
onFeedbackChanged: (newFeedback) {
setState(() {
_feedbackValues[element.id] = newFeedback;
Expand Down
5 changes: 4 additions & 1 deletion frontend/lib/pages/feedback/feedback_result_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:frontend/components/elements/feedback/slider_feedback_result.dar
import 'package:frontend/components/elements/feedback/star_feedback_result.dart';
import 'package:frontend/global.dart';
import 'package:frontend/models/feedback/feedback_form.dart';
import 'package:frontend/models/feedback/feedback_question.dart';
import 'package:frontend/utils.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -222,7 +223,7 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
physics: const NeverScrollableScrollPhysics(),
itemCount: _form.questions.length,
itemBuilder: (context, index) {
final element = _form.questions[index];
final element = _form.questions[index] as FeedbackQuestion;
final double average = _results[index]["average"];
final roundAverage = (average * 100).round() / 100;
final values = _results[index]["values"];
Expand All @@ -241,6 +242,8 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
else if (element.type == 'SLIDER')
SliderFeedbackResult(
results: values,
rangeLow: element.rangeLow,
rangeHigh: element.rangeHigh,
average: average,
min: 0,
max: 10,
Expand Down

0 comments on commit 79240cb

Please sign in to comment.