1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-04-07 16:22:38 +00:00

BMI calculator example (#130)

This commit is contained in:
lutaii
2023-10-12 18:08:26 +02:00
committed by GitHub
parent f761e553e4
commit 36c8bed38c
19 changed files with 780 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import '../palette.dart';
import 'result_content_widget.dart';
class ResultBodyWidget extends StatelessWidget {
const ResultBodyWidget({
super.key,
required this.result,
});
final double result;
@override
Widget build(BuildContext context) {
return Container(
color: Palette.background,
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(72.0),
child: ResultContentWidget(result: result),
),
);
}
}

View File

@@ -0,0 +1,79 @@
import 'package:flutter/material.dart';
import '../palette.dart';
class ResultContentWidget extends StatelessWidget {
const ResultContentWidget({
super.key,
required this.result,
});
final double result;
Color resultColor() {
if (result < 18.5) {
return Palette.underweightResult;
} else if (result > 25) {
return Palette.overWeightResult;
} else {
return Palette.normalResult;
}
}
String diagnosis() {
if (result < 18.5) {
return 'UNDERWEIGHT';
} else if (result > 25) {
return 'OVERWEIGHT';
} else {
return 'NORMAL';
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
diagnosis(),
style: TextStyle(
color: resultColor(),
fontWeight: FontWeight.w600,
fontSize: 25,
),
),
Text(
// [toStringAsFixed] for decimal-point string-representation
result.toStringAsFixed(1),
style: const TextStyle(
color: Palette.textActive,
fontWeight: FontWeight.w600,
fontSize: 100,
),
),
const Column(
children: [
Text(
'Normal BMI range:',
style: TextStyle(
color: Palette.textInactive,
fontWeight: FontWeight.w500,
fontSize: 20,
),
),
SizedBox(height: 8),
Text(
'18.5 - 25 kg/m2',
style: TextStyle(
color: Palette.textActive,
fontWeight: FontWeight.w500,
fontSize: 20,
),
),
],
),
],
);
}
}

View File

@@ -0,0 +1,35 @@
import 'package:bmi_calculator/body_model.dart';
import 'package:bmi_calculator/palette.dart';
import 'package:bmi_calculator/result/result_body_widget.dart';
import 'package:flutter/material.dart';
import '../bmi_calculator.dart';
class ResultPage extends StatelessWidget {
final BodyModel model;
const ResultPage({
super.key,
required this.model,
});
@override
Widget build(BuildContext context) {
final result = calculateBMI(bodyModel: model);
return Scaffold(
appBar: AppBar(
title: const Text('RESULT'),
backgroundColor: Palette.background,
leading: IconButton(
onPressed: () {
// Navigate back to previous page
Navigator.pop(context);
},
icon: const Icon(Icons.arrow_back_ios),
),
),
body: ResultBodyWidget(result: result),
);
}
}