-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathquestion_step.dart
More file actions
197 lines (181 loc) · 6.07 KB
/
Copy pathquestion_step.dart
File metadata and controls
197 lines (181 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
part of '../../ui.dart';
/// The UI representation of the [RPQuestionStep].
///
/// This widget is the container, the concrete content depends on the input
/// step's [RPAnswerFormat].
///
/// As soon as the participant has finished with the question the [RPStepResult]
/// is being added to the [RPTaskResult]'s result list.
class RPUIQuestionStep extends StatefulWidget {
final RPQuestionStep step;
const RPUIQuestionStep(this.step, {super.key});
@override
RPUIQuestionStepState createState() => RPUIQuestionStepState();
}
class RPUIQuestionStepState extends State<RPUIQuestionStep> with CanSaveResult {
// dynamic because we don't know what value the RPChoice will have
dynamic _currentQuestionBodyResult;
RPStepResult? result;
RPTaskProgress? recentTaskProgress;
int timeInSeconds = 0;
Timer? timer;
set currentQuestionBodyResult(dynamic currentQuestionBodyResult) {
_currentQuestionBodyResult = currentQuestionBodyResult;
createAndSendResult();
if (_currentQuestionBodyResult != null) {
blocQuestion.sendReadyToProceed(true);
} else {
blocQuestion.sendReadyToProceed(false);
}
}
void skipQuestion() {
timer?.cancel();
FocusManager.instance.primaryFocus?.unfocus();
blocTask.sendStatus(RPStepStatus.Skipped);
currentQuestionBodyResult = null;
}
@override
void initState() {
super.initState();
if (timeInSeconds <= 0) {
timeInSeconds = widget.step.timeout.inSeconds;
const oneSec = Duration(seconds: 1);
timer = Timer.periodic(oneSec, (t) {
if (mounted) {
setState(() {
timeInSeconds--;
});
}
if (widget.step.autoSkip) {
t.cancel();
skipQuestion();
}
});
}
// Create the result object here to record the start time
result = RPStepResult(
identifier: widget.step.identifier,
questionTitle: widget.step.title,
answerFormat: widget.step.answerFormat);
blocQuestion.sendReadyToProceed(false);
recentTaskProgress = blocTask.lastProgressValue;
}
@override
void deactivate() {
timer?.cancel();
super.deactivate();
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
// Returning the according step body widget based on the answerFormat of the step
Widget stepBody(RPAnswerFormat answerFormat) {
switch (answerFormat.runtimeType) {
case const (RPIntegerAnswerFormat):
return RPUIIntegerQuestionBody((answerFormat as RPIntegerAnswerFormat),
(result) {
currentQuestionBodyResult = result;
});
case const (RPDoubleAnswerFormat):
return RPUIDoubleQuestionBody((answerFormat as RPDoubleAnswerFormat),
(result) {
currentQuestionBodyResult = result;
});
case const (RPChoiceAnswerFormat):
return RPUIChoiceQuestionBody((answerFormat as RPChoiceAnswerFormat),
(result) {
currentQuestionBodyResult = result;
});
case const (RPSliderAnswerFormat):
return RPUISliderQuestionBody((answerFormat as RPSliderAnswerFormat),
(result) {
currentQuestionBodyResult = result;
});
case const (RPImageChoiceAnswerFormat):
return RPUIImageChoiceQuestionBody(
(answerFormat as RPImageChoiceAnswerFormat), (result) {
currentQuestionBodyResult = result;
});
case const (RPStarChoiceAnswerFormat):
return RPUIStarChoiceQuestionBody(
(answerFormat as RPStarChoiceAnswerFormat), (result) {
currentQuestionBodyResult = result;
});
case const (RPDateTimeAnswerFormat):
return RPUIDateTimeQuestionBody(
(answerFormat as RPDateTimeAnswerFormat), (result) {
currentQuestionBodyResult = result;
});
case const (RPTextAnswerFormat):
return RPUITextInputQuestionBody((answerFormat as RPTextAnswerFormat),
(result) {
currentQuestionBodyResult = result;
});
default:
return Container();
}
}
@override
Widget build(BuildContext context) {
RPLocalizations? locale = RPLocalizations.of(context);
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
Expanded(
child: ListView(
padding: const EdgeInsets.all(8),
children: [
// Title
Padding(
padding: const EdgeInsets.only(
bottom: 24, left: 8, right: 8, top: 0),
child: Text(
locale?.translate(widget.step.title) ?? widget.step.title,
textAlign: TextAlign.left,
style: Theme.of(context).textTheme.titleLarge,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: stepBody(widget.step.answerFormat),
),
widget.step.optional
? TextButton(
onPressed: () => skipQuestion(),
child: Text(locale?.translate("Skip this question") ??
"Skip this question"),
)
: Container(),
],
),
),
if (widget.step.footnote != null)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
locale?.translate(widget.step.footnote!) ??
widget.step.footnote!,
style: Theme.of(context).textTheme.bodySmall,
textAlign: TextAlign.center,
softWrap: true,
),
),
],
),
),
);
}
@override
void createAndSendResult() {
timer?.cancel();
if (result != null) {
result?.questionTitle = widget.step.title;
result?.setResult(_currentQuestionBodyResult);
blocTask.sendStepResult(result!);
}
}
}