■ 사칙연산 계산기를 만드는 방법을 보여준다.
▶ 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 |
import 'package:flutter/material.dart'; void main() => runApp(TestApplication()); class TestApplication extends StatelessWidget { static const String _title = 'Test Application'; @override Widget build(BuildContext context) { return MaterialApp( title: _title, home: MainPage(), ); } } class MainPage extends StatefulWidget { MainPage({Key key}) : super(key: key); @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { List<String> _buttonTextList = ['더하기', '빼기', '곱하기', '나누기']; List<DropdownMenuItem<String>> _menuItemList = List<DropdownMenuItem<String>>(); String _currentButtonText; String _resultText = ''; TextEditingController controller1 = TextEditingController(); TextEditingController controller2 = TextEditingController(); @override void initState() { super.initState(); for (String buttonText in _buttonTextList) { _menuItemList .add(DropdownMenuItem(value: buttonText, child: Text(buttonText))); } _currentButtonText = _menuItemList[0].value; } Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Test Application'), ), body: Container( child: Center( child: Column( children: <Widget>[ Padding( padding: EdgeInsets.all(15), child: Text( '결과 : $_resultText', style: TextStyle(fontSize: 20), ), ), Padding( padding: EdgeInsets.only(left: 20, right: 20), child: TextField( keyboardType: TextInputType.number, controller: controller1, ), ), Padding( padding: EdgeInsets.only(left: 20, right: 20), child: TextField( keyboardType: TextInputType.number, controller: controller2, ), ), Padding( padding: EdgeInsets.all(15), child: RaisedButton( child: Row( children: <Widget>[ Icon(Icons.add), Text(_currentButtonText), ], ), color: Colors.amber, onPressed: () { setState(() { double value1 = double.parse(controller1.value.text); double value2 = double.parse(controller2.value.text); double result; if (_currentButtonText == '더하기') { result = value1 + value2; } else if (_currentButtonText == '빼기') { result = value1 - value2; } else if (_currentButtonText == '곱하기') { result = value1 * value2; } else { result = value1 / value2; } _resultText = result.toString(); }); }), ), Padding( padding: EdgeInsets.all(15), child: DropdownButton( items: _menuItemList, onChanged: (value) { setState(() { _currentButtonText = value; }); }, value: _currentButtonText, ), ), ], ), ), ), ); } } |