■ 비만도 계산기를 만드는 방법을 보여준다.
▶ main.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import 'package:flutter/material.dart'; void main() => runApp(TestApplication()); class TestApplication extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Test Application', theme: ThemeData( primarySwatch: Colors.blue, ), home: MainPage(), ); } } class MainPage extends StatefulWidget { @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); final _heightController = TextEditingController(); final _weightController = TextEditingController(); @override void dispose() { _heightController.dispose(); _weightController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('비만도 계산기')), body: Container( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( children: <Widget>[ TextFormField( decoration: InputDecoration( border: OutlineInputBorder(), hintText: '신장', ), controller: _heightController, keyboardType: TextInputType.number, validator: (value) { if (value.trim().isEmpty) { return '신장(cm)을 입력해 주시기 바랍니다.'; } return null; }, ), SizedBox( height: 16.0, ), TextFormField( decoration: InputDecoration( border: OutlineInputBorder(), hintText: '체중', ), controller: _weightController, keyboardType: TextInputType.number, validator: (value) { if (value.trim().isEmpty) { return '체중(kg)을 입력해 주시기 바랍니다.'; } return null; }, ), Container( margin: const EdgeInsets.only(top: 16.0), alignment: Alignment.centerRight, child: RaisedButton( onPressed: () { if (_formKey.currentState.validate()) { Navigator.push( context, MaterialPageRoute( builder: (context) => ResultPage( double.parse(_heightController.text.trim()), double.parse(_weightController.text.trim()), ), ), ); } }, child: Text('결과'), ), ) ], ), ), ), ); } } class ResultPage extends StatelessWidget { final double height; final double weight; ResultPage(this.height, this.weight); @override Widget build(BuildContext context) { final double bmi = weight / ((height / 100) * (height / 100)); return Scaffold( appBar: AppBar(title: Text('비만도 계산기')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( _getText(bmi), style: TextStyle(fontSize: 36), ), SizedBox( height: 16, ), _getIcon(bmi), ], ), ), ); } String _getText(double bmi) { String result = '저체중'; if (bmi >= 35) { result = '고도 비만'; } else if (bmi >= 30) { result = '2단계 비만'; } else if (bmi >= 25) { result = '1단계 비만'; } else if (bmi >= 23) { result = '과체중'; } else if (bmi >= 18.5) { result = '정상'; } return result; } Widget _getIcon(double bmi) { if (bmi >= 23) { return Icon( Icons.sentiment_very_dissatisfied, color: Colors.red, size: 100, ); } else if (bmi >= 18.5) { return Icon( Icons.sentiment_satisfied, color: Colors.green, size: 100, ); } else { return Icon( Icons.sentiment_dissatisfied, color: Colors.orange, size: 100, ); } } } |