85 lines
2.3 KiB
Dart
85 lines
2.3 KiB
Dart
import 'dart:math';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:lamiter/Class/Form/physical_index_form.dart';
|
|
|
|
class PhysicalIndexResult {
|
|
num BMI;
|
|
num BMR;
|
|
|
|
PhysicalIndexResult({
|
|
required this.BMI,
|
|
required this.BMR,
|
|
});
|
|
|
|
PhysicalIndexResult.fromJson(Map<String, dynamic> json)
|
|
: BMI = json["bmi"] as num,
|
|
BMR = json["bmr"] as num;
|
|
|
|
Map<String, dynamic> toJson() => {'bmi': BMI, 'bmr': BMR};
|
|
|
|
PhysicalIndexResult.fromForm(PhysicalIndexForm form)
|
|
: BMI = _calculateBMI(
|
|
form.weight,
|
|
form.height,
|
|
),
|
|
BMR = _calculateBMR(
|
|
form.gender,
|
|
form.age,
|
|
form.weight,
|
|
form.height,
|
|
);
|
|
|
|
static num _calculateBMI(num weight, num height) {
|
|
num value = weight.toDouble() / pow((height.toDouble() / 100), 2);
|
|
return double.parse(value.toStringAsFixed(1));
|
|
}
|
|
|
|
static num _calculateBMR(bool gender, int age, num weight, num height) {
|
|
num value;
|
|
if (gender) {
|
|
//male
|
|
value = 66 + 13.7 * weight.toDouble() + 5 * height.toDouble() - 6.8 * age;
|
|
} else {
|
|
//female
|
|
value =
|
|
65 + 9.6 * weight.toDouble() + 1.7 * height.toDouble() - 4.7 * age;
|
|
}
|
|
return double.parse(value.toStringAsFixed(1));
|
|
}
|
|
|
|
final double BMI_low_threshold = 18.5;
|
|
final double BMI_high_threshold = 24;
|
|
final int BMI_status_length = 3;
|
|
final List<int> BMI_status_thresh = <int>[1, 1, 1];
|
|
final List<Color> BMI_status_colors = [Colors.blue, Colors.green, Colors.red];
|
|
final List<String> BMI_status_labels = ['偏低', '標準', '偏高'];
|
|
|
|
int _BMI_status() {
|
|
if (BMI < BMI_low_threshold) return 0;
|
|
if (BMI <= BMI_high_threshold) return 1;
|
|
return 2;
|
|
}
|
|
|
|
double BMI_factor() {
|
|
final factor = (BMI < 0)
|
|
? 0
|
|
: (BMI <= 18.5)
|
|
? BMI / BMI_low_threshold
|
|
: (BMI <= BMI_high_threshold)
|
|
? ((BMI - BMI_low_threshold) /
|
|
(BMI_high_threshold - BMI_low_threshold) +
|
|
1)
|
|
: (min(1, ((BMI - BMI_high_threshold) / BMI_high_threshold)) +
|
|
2);
|
|
return factor / 3;
|
|
}
|
|
|
|
Color BMI_color() {
|
|
return BMI_status_colors[_BMI_status()];
|
|
}
|
|
|
|
String BMI_label() {
|
|
return BMI_status_labels[_BMI_status()];
|
|
}
|
|
}
|