첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

test_app.zip
0.13MB

▶ main.dart

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,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요