55 lines
1.3 KiB
Dart
55 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:lamiter/Class/Form/health_index_form.dart';
|
|
import 'package:lamiter/Class/Question/scq.dart';
|
|
|
|
class HealthIndexResult {
|
|
int score;
|
|
|
|
HealthIndexResult({
|
|
required this.score,
|
|
});
|
|
|
|
HealthIndexResult.fromJson(Map<String, dynamic> json)
|
|
: score = json["score"] as int;
|
|
|
|
Map<String, dynamic> toJson() => {'score': score};
|
|
|
|
HealthIndexResult.fromForm(HealthIndexForm form)
|
|
: score = _calculateScore(form);
|
|
|
|
static int _calculateScore(HealthIndexForm form) {
|
|
int score = 0;
|
|
form.scores.forEach((String qid, SCQAnswer answer) {
|
|
score += answer.score;
|
|
});
|
|
return score;
|
|
}
|
|
|
|
final int max_score = 14;
|
|
final int status_length = 3;
|
|
final List<int> status_thresh = <int>[3, 5, 6];
|
|
final List<Color> status_colors = [Colors.red, Colors.amber, Colors.green];
|
|
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()];
|
|
}
|
|
}
|