Icon을 정의하고 변수로 불러오기

2024. 6. 14. 10:10flutter

import 'package:flutter/material.dart';

class ResultScreen extends StatelessWidget {
  final double height;
  final double weight;

  const ResultScreen({
    Key? key,
    required this.weight,
    required this.height,
  }) : super(key: key);

  String _calcBmi(double bmi) {
    String result = '저체중';

    if (bmi >= 35.0) {
      result = '고도 비만';
    } else if (bmi >= 30.0) {
      result = '2단계 비만';
    } else if (bmi >= 25.0) {
      result = '1단계 비만';
    } else if (bmi >= 23.0) {
      result = '과체중';
    } else if (bmi >= 18.5) {
      result = '정상';
    }

    return result;
  }

  Widget _buildIcon(double bmi) {
    Icon icon = Icon(
      Icons.sentiment_dissatisfied,
      size: 100,
      color: Colors.green,
    );

    if (bmi >= 23.0) {
      icon = Icon(
        Icons.sentiment_very_dissatisfied,
        size: 100,
        color: Colors.red,
      );
    } else if (bmi >= 18.5) {
      icon = Icon(
        Icons.sentiment_satisfied,
        size: 100,
        color: Colors.blue,
      );
    }
    return icon;
  }
  
  // Widget _buildIcon(double bmi) {
  //   Icon icon = const Icon(Icons.sentiment_dissatisfied);
  //
  //   if (bmi >= 23.0) {
  //     icon = const Icon(Icons.sentiment_very_dissatisfied);
  //   } else if (bmi >= 18.5) {
  //     icon = const Icon(Icons.sentiment_satisfied);
  //   }
  //   return icon;
  // }

  @override
  Widget build(BuildContext context) {
    final double bmi = weight / ((height / 100.0) * (height / 100.0));
    final String bmiResult = _calcBmi(bmi);

    return Scaffold(
      appBar: AppBar(
        title: const Text('결과'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              bmiResult,
              style: const TextStyle(fontSize: 36.0),
            ),
            _buildIcon(bmi),
            // Icon(ice_skating),
            // Icon(
            //   Icons.sentiment_very_dissatisfied,
            //     // _buildIcon(bmi),
            //   color: Colors.green,
            //   size: 100,
            // ),
          ],
        ),
      ),
    );
  }
}