//네이버 서치어드바이저 플러터로 만든 BMI 계산기2(이미지 삽입, 코드 첨부)
본문 바로가기

플러터

플러터로 만든 BMI 계산기2(이미지 삽입, 코드 첨부)

 
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

 

normal.png
obese.png
overweight.png
risk.png
underweight.png
bmi.png
0.52MB
normal.png
0.54MB
obese.png
0.54MB
overweight.png
0.54MB
risk.png
0.54MB
underweight.png
0.54MB