65 lines
1.4 KiB
Dart
65 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:lamiter/Class/Form/sleep_well_index_form.dart';
|
|
import 'package:lamiter/Class/Question/scq.dart';
|
|
|
|
class SleepWellIndexResult {
|
|
int score;
|
|
|
|
SleepWellIndexResult({
|
|
required this.score,
|
|
});
|
|
|
|
SleepWellIndexResult.fromJson(Map<String, dynamic> json)
|
|
: score = json["score"] as int;
|
|
|
|
Map<String, dynamic> toJson() => {'score': score};
|
|
|
|
SleepWellIndexResult.fromForm(SleepWellIndexForm form)
|
|
: score = _calculateScore(form);
|
|
|
|
static int _calculateScore(SleepWellIndexForm form) {
|
|
int score = 0;
|
|
form.scores.forEach((String qid, SCQAnswer answer) {
|
|
score += answer.score;
|
|
});
|
|
return score;
|
|
}
|
|
|
|
final int max_score = 28;
|
|
final int status_length = 4;
|
|
final List<int> status_thresh = <int>[7, 7, 7, 7];
|
|
final List<Color> status_colors = [
|
|
Colors.green,
|
|
Colors.amber,
|
|
Colors.orange,
|
|
Colors.red,
|
|
];
|
|
final List<String> status_labels = [
|
|
'無睡眠障礙',
|
|
'輕度失眠',
|
|
'中度失眠',
|
|
'嚴重失眠',
|
|
];
|
|
|
|
int _status() {
|
|
int temp = score;
|
|
for (int i = 0; i < status_length; i++) {
|
|
if (temp <= status_thresh[i]) return i;
|
|
temp -= status_thresh[i];
|
|
}
|
|
return status_length - 1;
|
|
}
|
|
|
|
double factor() {
|
|
return score.toDouble() / max_score.toDouble();
|
|
}
|
|
|
|
Color color() {
|
|
return status_colors[_status()];
|
|
}
|
|
|
|
String label() {
|
|
return status_labels[_status()];
|
|
}
|
|
}
|