Shiny Sky Blue Star

개인 프로젝트

계산기 개인 프로젝트 (Dart, Flutter)

gamja00 2025. 1. 23. 19:51

프로젝트를 뭐라도 해봐야겠다 싶어서 무턱대고 시작한 프로젝트이다.

원래 Java로 GUI 기능을 이용한 프로젝트를 해보려다가 Java Swing이나 Fx는 현직에서 잘 사용하지 않는다고 하여 저번에 책을 한 번 읽었었던 플러터를 이용해 모바일 앱를 만들어보기로 했다.

사실 학교에서 배운 건 Kotlin이었는데 물론 코틀린도 좋은 언어지만 안드로이드에만 치중된 언어이기에 뭐라도 더 많이 되는 게 좋지 않나 싶어 Flutter 독학을 시작했다.

그래서 Dart 언어도 잘 모르지만 만들면서 이거저거 찾아보면 어떻게든 될 것 같아서 시작했다.

 

 

계산기 구현 초반

 

계산기 구조만 구현했을 때.

아이폰 기본 계산기를 토대로 기능과 버튼을 구현했다.

 

Flexible 함수로 세 개의 Container를 1 : 1 : 5로 설정하여 비율이 유지되도록 하였다.

계산기의 각 버튼은 ElevetedButton을 사용했는데 Column과 Row를 이용하여 Container에 배치시켰다.

이 때도 Flexible을 이용하여 각 버튼의 크기를 일정하게 만들었다.

근데 여기서 보면 지금 0과 . 버튼, = 버튼만 크기가 미묘하게 다른 걸 알 수 있다.

똑같이 만들었는데 왜인지 모르겠다.

 

 

계산기 각 기능들을 어느 정도 구현한 상태.

아직 결과 값을 계산하는 = 버튼은 구현하지 않았다.

← 버튼과 +/- (절대값) 버튼을 구현하는 것이 꽤 힘들었다.

 

내가 잘 몰라서 그러는 걸 수도 있지만 list 메소드가 좀... 값을 얻을 때 너무 어렵게 되어 있는 것 같다.

-> 다른 곳에서 간단하게 값을 얻는 방법을 찾았다... 단순하게 list[i] 형태로 부르면 된다.

 

다른 메소드들도 사용할 때 조금 복잡하게 사용해야 되는 것 같아서 좀 힘들었다.

 

 

 

최종 화면

 

 

기능 소개

 

아이폰에 기본적으로 깔려있는 계산기 어플의 기능과 구조를 따라 만든 계산기이다.

추가적으로 있는 기능은 계산한 값에서 이어서 계산을 할 때 마지막 수를 없애는 기능이다.

ex) 결과값이 569742라고 할 때 ← 버튼을 이용하여 뒤에서부터 지워 56974, 5697 으로 만들어 재사용이 가능하다.

 

가장 위에 있는 공간은 식을 계산한 이후에 나타나는 부분으로 계산한 값의 원래 식을 보여주는 공간이다.

기본적으로는 공백이며 식을 계산하는 = 버튼을 누른 이후에 값이 나타난다.

이후 다른 버튼을 입력할 때 사라진다.

 

 

두번째 공간은 계산식을 입력하고 결과를 출력하는 공간이다.

계산식을 입력할 때 이 곳에 입력이 되고 = 버튼을 누르면 결과값을 출력한다.

 

 

마지막 공간에는 값을 입력할 수 있는 버튼이 위치한 공간이다.

모든 버튼은 엘리베이터 버튼으로 만들어져 버튼을 누르면 두 번째 공간에 눌러진 버튼에 적힌 값이 출력된다.

 

AC 버튼은 초기화 버튼이다.

계산식, 결과값, 최종 식 출력 공간 모두를 초기화한다.

 

+/- 버튼은 부호를 변경하는 버튼이다.

해당 버튼을 누를 당시에 수가 양수이면 음수로, 음수이면 양수로 변경해준다.

수를 변경하면서 수가 양수일 때는 (- 양수 ) 로 변경하고 (- 수 ) (음수를 표현함.) 음수일 때는 각 기호를 빼고 수만 나타내도록 한다.

지워질 때는 (- 수) 에서 수와 앞의 기호 (- 를 하나로 묶어 수 부분이 지워지면 같이 없어지도록 하였다.

 

% 버튼은 나머지 계산이다.

나누기 계산에서 몫이 아닌 나머지를 구하는 계산이다.

 

÷ 버튼은 나누기 계산이다.

 

× 버튼은 곱하기 계산이다.

 

- 버튼은 빼기 계산이다.

 

+ 버튼은 더하기 계산이다.

 

← 버튼은 한 칸 지우기 버튼이다.

계산식이나 결과값에서 맨 뒤부터 한 자리씩 지울 수 있다.

숫자뿐만 아니라 연산 기호도 지워진다.

음수 기호를 지울 때는 맨 뒤 괄호도 한 자리로 친다. 맨 앞 기호인 (- 는 숫자가 10보다 작을 때 숫자를 지우면 같이 지워진다.

 

. 버튼은 소수점 버튼이다.

숫자에서 해당 버튼을 누른 이후의 수는 소수 자리의 수로 인식된다.

0.3과 .3은 모두 0.3으로 인식된다.

 

= 버튼은 값을 계산하는 버튼이다.

식을 입력한 후 이 버튼을 누르면 식에 대한 결과값이 출력되게 된다.

 

0 ~ 9 버튼들은 숫자 버튼이다.

누르면 해당 버튼에 적힌 숫자가 두 번째 공간에 입력된다.

 

 

최종 수정

버튼 글씨가 조금 작은 것 같아 수정했다.

 

 

코드

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Calculator',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Calculator'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> list = [];
  String _num = "0";
  String calculationFormula = "0";
  bool absoluteValueFlag = false;
  String lastCalculationFormula = "";

  void _onPressedButton(String s) {
    lastCalculationFormula = ""; // 이부분이문제일지도

    if (s == "%" || s == "÷" || s == "×" || s == "-" || s == "+") {
      String st = calculationFormula.substring(calculationFormula.length - 1);
      if (st != "%" && st != "÷" && st != "×" && st != "-" && st != "+") {
        calculationFormula = calculationFormula + s;
        list.add(_num);
        list.add(s);
        _num = "";
        absoluteValueFlag = false;
      }
    } else if (s == "AC") {
      _num = "0";
      lastCalculationFormula = "";
      calculationFormula = "0";
      absoluteValueFlag = false;
      list.clear();
    } else if (s == "+/-") {
      if (double.parse(_num) > 0) {
        _num = (0 - double.parse(_num)).toString();
        absoluteValueFlag = true;
        if (list.isEmpty) {
          calculationFormula = "(-" + calculationFormula + ")";
        } else {
          for (int i = calculationFormula.length - 1; i >= 0; i--) {
            if (calculationFormula.substring(i, i + 1) == "%" ||
                calculationFormula.substring(i, i + 1) == "÷" ||
                calculationFormula.substring(i, i + 1) == "×" ||
                calculationFormula.substring(i, i + 1) == "-" ||
                calculationFormula.substring(i, i + 1) == "+") {
              calculationFormula = calculationFormula.replaceRange(
                  i + 1,
                  calculationFormula.length,
                  "(-" + calculationFormula.substring(i + 1) + ")");
              break;
            }
          }
        }
      } else {
        _num = double.parse(_num).abs().toString();
        absoluteValueFlag = false;
        if (list.isEmpty) {
          calculationFormula = _num;
        } else {
          calculationFormula = calculationFormula.replaceRange(
              calculationFormula.length - _num.length - 3,
              calculationFormula.length,
              _num);
        }
      }
    } else if (s == "←") {
      String st = calculationFormula.substring(calculationFormula.length - 1);
      if (list.isNotEmpty) {
        if (st == "%" || st == "÷" || st == "×" || st == "-" || st == "+") {
          list.removeLast();
          calculationFormula =
              calculationFormula.substring(0, calculationFormula.length - 1);
          _num = list.removeLast().toString();
        }
      } else {
        _num = _num.substring(0, _num.length - 1);
        calculationFormula =
            calculationFormula.substring(0, calculationFormula.length - 1);
      }
      if (calculationFormula == "-" || calculationFormula == "(-") {
        absoluteValueFlag = false;
        calculationFormula = "0";
        _num = "0";
      }
    } else if (s == ".") {
      if (!_num.contains(".")) {
        calculationFormula = calculationFormula + ".";
        _num = _num + ".";
      }
    } else if (s == "=") {
      list.add(_num);
      _num = "";
      absoluteValueFlag = false;

      list.removeWhere((list) => list == "");

      while (list.contains('%') || list.contains('÷') || list.contains('×')) {
        int temp = 0;
        double value = 0.0;
        double num1 = 0.0;
        double num2 = 0.0;

        if (!list.contains('÷') ||
            list.contains('%') && list.indexOf('%') < list.indexOf('÷')) {
          temp = list.indexOf('%');
        } else {
          temp = list.indexOf('÷');
        }
        if (list.contains('×') && (temp > list.indexOf('×')) || temp == -1) {
          temp = list.indexOf('×');
        }

        num1 = double.parse(list[temp - 1].toString());
        num2 = double.parse(list[temp + 1].toString());

        if (temp == list.indexOf('%')) {
          value = double.parse((num1.toInt() % num2.toInt()).toString());
          list.removeRange(temp - 1, temp + 2);
          list.insert(temp - 1, value.toString());
        } else if (temp == list.indexOf('÷')) {
          value = num1 / num2;
          list.removeRange(temp - 1, temp + 2);
          list.insert(temp - 1, value.toString());
        } else {
          value = num1 * num2;
          list.removeRange(temp - 1, temp + 2);
          list.insert(temp - 1, value.toString());
        }
      }

      while (list.contains('+') || list.contains('-')) {
        double value = 0.0;
        double num1 = 0.0;
        double num2 = 0.0;

        num1 = double.parse(list[0].toString());
        num2 = double.parse(list[2].toString());

        if (list.indexOf('+') == 1) {
          value = num1 + num2;
          list.removeRange(0, 3);
          list.insert(0, value.toString());
        } else {
          value = num1 - num2;
          list.removeRange(0, 3);
          list.insert(0, value.toString());
        }
      }

      _num = list[0].toString();
      lastCalculationFormula = calculationFormula;
      calculationFormula = _num;
      list.clear();
    } else {
      if (_num == "0" && _num == calculationFormula) {
        _num = s;
        if (calculationFormula.length == 1) {
          calculationFormula = s;
        } else {
          calculationFormula = calculationFormula + s;
        }
      } else {
        if (absoluteValueFlag) {
          _num = _num + s;
          calculationFormula = calculationFormula.replaceRange(
              calculationFormula.length - _num.length,
              calculationFormula.length - 1,
              _num);
        } else {
          calculationFormula = calculationFormula + s;
          _num = _num + s;
        }
      }
    }

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            flex: 1,
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(width: 2, color: Colors.grey),
                borderRadius: BorderRadius.all(Radius.circular(2)),
              ),
              width: Size.infinite.width,
              padding: EdgeInsets.all(3),
              margin: EdgeInsets.symmetric(vertical: 2),
              child: Text(
                '$lastCalculationFormula',
                style: TextStyle(
                  fontWeight: FontWeight.w900,
                  fontSize: 40.0,
                ),
              ),
            ),
          ),
          Flexible(
            flex: 1,
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(width: 2, color: Colors.grey),
                borderRadius: BorderRadius.all(Radius.circular(2)),
              ),
              width: Size.infinite.width,
              padding: EdgeInsets.all(3),
              margin: EdgeInsets.symmetric(vertical: 2),
              child: Text(
                '$calculationFormula',
                style: TextStyle(
                  fontWeight: FontWeight.w900,
                  fontSize: 40.0,
                ),
              ),
            ),
          ),
          Flexible(
            flex: 5,
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(width: 2, color: Colors.grey),
                borderRadius: BorderRadius.all(Radius.circular(2)),
              ),
              height: Size.infinite.height,
              width: Size.infinite.width,
              margin: EdgeInsets.symmetric(vertical: 2),
              child: Column(
                children: [
                  Row(
                    children: [
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("AC");
                          },
                          child: Text('AC'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("+/-");
                          },
                          child: Text('+/-'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("%");
                          },
                          child: Text('%'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("÷");
                          },
                          child: Text('÷'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("7");
                          },
                          child: Text('7'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("8");
                          },
                          child: Text('8'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("9");
                          },
                          child: Text('9'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("×");
                          },
                          child: Text('×'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("4");
                          },
                          child: Text('4'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("5");
                          },
                          child: Text('5'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("6");
                          },
                          child: Text('6'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("-");
                          },
                          child: Text('-'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("1");
                          },
                          child: Text('1'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("2");
                          },
                          child: Text('2'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("3");
                          },
                          child: Text('3'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("+");
                          },
                          child: Text('+'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("←");
                          },
                          child: Text('←'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("0");
                          },
                          child: Text('0'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton(".");
                          },
                          child: Text('.'),
                          style: ElevatedButton.styleFrom(
                            minimumSize: Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        child: ElevatedButton(
                          onPressed: () {
                            return _onPressedButton("=");
                          },
                          child: Text('='),
                          style: ElevatedButton.styleFrom(
                            minimumSize: const Size.fromHeight(100),
                            padding: EdgeInsets.symmetric(
                                horizontal: 10.0, vertical: 10.0),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                                side:
                                    BorderSide(color: Colors.grey, width: 1.5)),
                            textStyle: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

기본으로 나오는 화면에서 필요한 값들을 수정하여 만들었다.

 

화면이 전환되는 것이 없어서 dart 파일을 따로 만들어 연결하진 않았고, main파일만 수정하여 만들었다.

 

후기

다트를 따로 배우진 않았고 책 한 권만 읽고 검색을 통해 이 코드를 완성했는데  단순한 코드라 그런지 생각보다 어렵진 않았다.

처음 만들어 보는 거라서 이거 저거 찾아보며 만들었고, 여유롭게 만들어서 3일 정도 걸린 것 같다.

조금 숙달된다면 하루 내에 만들 수 있을 것 같다.

처음엔 막막했으나 아무래도 계산기를 만드는 것은 화면 구성도 단순하고 기능도 단순해서 한 부분만 만들면 그 부분을 복붙해서 여러 번 사용하기만 해도 화면이 만들어져서 그 부분은 쉬웠던 것 같다.

모든 것이 쉽진 않았지만 + / - 버튼 기능을 구현할 때와 ← 버튼 구현이 어려웠고, 가장 어려웠던 부분은 아무래도 결과값을 출력하는 = 버튼이었다.

= 버튼의 기능을 구현할 때 연산 기호들의 계산 순서를 조정하느라 더하기와 빼기를 제외한 나머지 연산 기호들을 먼저 계산하도록 해야 됐는데, 먼저 계산해야 되는 세 개의 연산 기호들 중 가장 먼저 계산될 기호를 고르는 조건문을 만들 때가 어려웠다. 내가 잘 못하는 것도 있지만 에러가 나지 않고 실행되는 부분 중 오류를 찾기가 어려워서 더 힘들었다.

코딩을 잠시 쉬었던 탓도 있는 것 같았다.

뇌를 자주 쓰지 않으면 금방 굳는 것 같아 항상 조금이라도 하려고 노력하는데 일이 좀 있어서 요즘 코딩을 하지 못한 탓에 뇌가 좀 굳은 것 같다.

 

 

보완해야 될 점

이번에는 파일 하나로만 만들었는데 다음에는 화면 전환 기능을 넣으면서 파일을 여러 개로 나누는 것을 해봐야 될 것 같다.

router와 navigator 연습이 필요하다.

그리고 화면 크기를 조정하는 것에서 화면 크기를 고정하거나 비율을 조정, 각 위젯의 크기를 조절하는 연습이 필요한 것 같다.

이 부분이 쉽게 되지 않아서 어려웠다.

기능을 구현하는 부분은 어렵지 않으나 화면을 조정하고 위젯을 배치하거나 하는 화면 구성하는 부분을 구현하는 것이 어려웠다.