
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late TextEditingController heightController;
late TextEditingController weightController;
late String bmiImage;
// 결과값 멘트용 변수
String calcResult = '';
@override
void initState() {
// TODO: implement initState
super.initState();
heightController = TextEditingController();
weightController = TextEditingController();
bmiImage = 'images/bmi.png';
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Scaffold(
appBar: AppBar(
title: Text('BMI 계산기'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: SingleChildScrollView(
child: Center(
child: Column(
children: [
TextField(
controller: heightController,
decoration: InputDecoration(
labelText: '신장을 입력하세요(cm)',
),
keyboardType: TextInputType.number,
),
SizedBox(
height: 10,
),
TextField(
controller: weightController,
decoration: InputDecoration(
labelText: '몸무게을 입력하세요(kg)',
),
keyboardType: TextInputType.number,
),
SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
//---
calcFunction();
},
child: Text('BMI 계산'),
),
SizedBox(
height: 50,
),
Text(
calcResult,
style: TextStyle(
color: Colors.green,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 50,
),
Image.asset(bmiImage),
],
),
),
),
),
),
);
}
// --- Fucntions ---
calcFunction() {
double doubleWeight = double.parse(weightController.text);
double doubleHeight = double.parse(heightController.text) / 100;
double bmi = double.parse(
(doubleWeight / (doubleHeight * doubleHeight)).toStringAsFixed(1));
setState(() {
String resultStr;
if (bmi <= 18.4 && bmi >= 0) {
resultStr = '저체중';
bmiImage = 'images/underweight.png';
} else if (bmi >= 18.5 && bmi <= 22.9) {
resultStr = '정상체중';
bmiImage = 'images/normal.png';
} else if (bmi >= 23 && bmi <= 24.9) {
resultStr = '과체중';
bmiImage = 'images/risk.png';
} else if (bmi >= 25 && bmi <= 29.9) {
resultStr = '비만';
bmiImage = 'images/overweight.png';
} else {
resultStr = '고도비만';
bmiImage = 'images/obese.png';
}
calcResult = '귀하의 bmi지수는 $bmi이고 \n $resultStr 입니다.';
});
}
} // End
[이미지 파일들]
bmi.png
0.52MB
normal.png
0.54MB
obese.png
0.54MB
overweight.png
0.54MB
risk.png
0.54MB
underweight.png
0.54MB
'플러터' 카테고리의 다른 글
플러터 keystore 생성 안될 때 해결방법 (0) | 2022.05.18 |
---|---|
플러터 웹뷰 만들 때 인터넷 권한 허용 코드 (0) | 2022.05.18 |
안드로이드 스튜디오 emulator has terminated 오류 해결!!.ㅠ.ㅠ (0) | 2022.05.02 |
bmi 계산기 (플러터 코드) (0) | 2022.04.29 |
플러터 개발자가 알아두면 좋은 유용한 사이트 (0) | 2022.04.29 |