//네이버 서치어드바이저 bmi 계산기 (플러터 코드)
본문 바로가기

플러터

bmi 계산기 (플러터 코드)

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;

  // 결과값 멘트용 변수
  String calcResult = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    heightController = TextEditingController();
    weightController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI 계산기'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        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.red,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // --- 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 = '저체중';
      } else if (bmi >= 18.5 && bmi <= 22.9) {
        resultStr = '정상체중';
      } else if (bmi >= 23 && bmi <= 24.9) {
        resultStr = '과체중';
      } else if (bmi >= 25 && bmi <= 29.9) {
        resultStr = '비만';
      } else {
        resultStr = '고도비만';
      }

      calcResult = '귀하의 bmi지수는 $bmi이고 \n $resultStr 입니다.';
    });
  }
} // End
 

딱 계산기능만 있는 코드다.

 

여기에 이미지만 넣으면 예뻐질 듯..