728x90
반응형
728x170
▶ main.dart
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,
);
}
}
}
728x90
반응형
그리드형(광고전용)
'Flutter' 카테고리의 다른 글
[FLUTTER] 할 일 관리 앱 만들기 (0) | 2021.05.10 |
---|---|
[FLUTTER] Timer 클래스 : periodic 팩토리 생성자 사용하기 (0) | 2021.05.02 |
[FLUTTER] BottomAppBar 클래스 사용하기 (0) | 2021.05.02 |
[FLUTTER] Positioned 클래스 사용하기 (0) | 2021.05.02 |
[FLUTTER] 스톱워치(Stopwatch) 만들기 (0) | 2021.05.02 |
[FLUTTER] Opacity 클래스 : opacity 속성을 사용해 불투명도 표시하기 (0) | 2021.05.01 |
[FLUTTER] ScaffoldState 클래스 : showSnackBar 메소드를 사용해 스낵바 표시하기 (0) | 2021.05.01 |
[FLUTTER] TextFormField 클래스 : validator 속성을 사용해 입력 값 검증하기 (0) | 2021.05.01 |
[FLUTTER] Form 클래스 : 입력 값 검증하기 (0) | 2021.05.01 |
[FLUTTER] TextEditingController 클래스 사용하기 (0) | 2021.05.01 |
댓글을 달아 주세요