본문 바로가기

Flutter-플러터/클론코딩

플러터 계산기 코드 읽기

반응형

 

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

 

 

1. 함수 읽기

 textController1,  textController2 텍스트 필드 2칸을 선언 함수는 무한대로 설정 

숫자칸을 따로 만들지 않고  

텍스트 필드의 키보드 설정을 이용해서 숫자칸을 만들었음 

연산자 +,-,*,/ 각 버튼마다 함수를 지정해서 계산이 되도록 함 

 

 

 

 

 

2. 위젯 읽기 

 

+,-,*,/ 연산자 칸만 만듬 

숫자칸 버튼은 키보드로 대체 

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        FocusScope.of(context).requestFocus(new FocusNode()); // 화면에 탭을 하면 키보드를 닥게 하는 함수 
      },
      child: MaterialApp(
        title: "My App",
        home: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final TextEditingController textController1 = TextEditingController();

  final TextEditingController textController2 = TextEditingController();

  double dataResult = double.infinity;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calculator"),
      ),
      body: SafeArea(
          child: Container(
            padding: EdgeInsets.all(5),
            constraints: BoxConstraints.expand(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                TextField(
                  controller: textController1,
                  textInputAction: TextInputAction.next,
                  keyboardType: TextInputType.number,
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  decoration: InputDecoration(
                      hintText: "Input Number 1",
                      // disabledBorder: InputBorder.none,
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(5))
                      )
                  ),
                ),
                SizedBox(height: 10),
                TextField(
                  controller: textController2,
                  textInputAction: TextInputAction.done,
                  keyboardType: TextInputType.number,
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  decoration: InputDecoration(
                      hintText: "Input Number 2",
                      // disabledBorder: InputBorder.none,
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(5))
                      )
                  ),
                ),
                SizedBox(height: 10),
                Center(
                  child: Text(
                      dataResult == double.infinity ? "" : "Ket qua = $dataResult" ,
                      style: TextStyle(
                          color: Colors.red ,
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          fontStyle: FontStyle.italic
                      )
                  ),
                ),
                SizedBox(height: 50),
                Row(
                  children: [
                    Expanded(
                      child: Center(
                        child: ElevatedButton(onPressed: (){
                          String text1 = textController1.text.toString();
                          String text2 = textController2.text.toString();

                          if (text1.isEmpty || text2.isEmpty){
                            ScaffoldMessenger
                                .of(context)
                                .showSnackBar(
                                SnackBar(content: Text("Ban chua nhap du thong tin"))
                            );
                            return;
                          }
                          double number1 = double.parse(text1);
                          double number2 = double.parse(text2);
                          setState(() {
                            dataResult = (number1 + number2);
                          });

                        }, child: Text("+")),
                      ),
                    ),
                    Expanded(
                      child: Center(
                        child: ElevatedButton(onPressed: (){
                          String text1 = textController1.text.toString();
                          String text2 = textController2.text.toString();

                          if (text1.isEmpty || text2.isEmpty){
                            ScaffoldMessenger
                                .of(context)
                                .showSnackBar(
                                SnackBar(content: Text("Ban chua nhap du thong tin"))
                            );
                            return;
                          }
                          double number1 = double.parse(text1);
                          double number2 = double.parse(text2);
                          setState(() {
                            dataResult = (number1 - number2);
                          });
                        }, child: Text("-")),
                      ),
                    )
                  ],
                ),
                SizedBox(height: 50),
                Row(
                  children: [
                    Expanded(
                      child: Center(
                        child: ElevatedButton(onPressed: (){
                          String text1 = textController1.text.toString();
                          String text2 = textController2.text.toString();

                          if (text1.isEmpty || text2.isEmpty){
                            ScaffoldMessenger
                                .of(context)
                                .showSnackBar(
                                SnackBar(content: Text("Ban chua nhap du thong tin"))
                            );
                            return;
                          }
                          double number1 = double.parse(text1);
                          double number2 = double.parse(text2);
                          setState(() {
                            dataResult = (number1 * number2);
                          });
                        }, child: Text("*")),
                      ),
                    ),
                    Expanded(
                      child: Center(
                        child: ElevatedButton(onPressed: (){
                          String text1 = textController1.text.toString();
                          String text2 = textController2.text.toString();

                          if (text1.isEmpty || text2.isEmpty){
                            ScaffoldMessenger
                                .of(context)
                                .showSnackBar(
                                SnackBar(content: Text("Ban chua nhap du thong tin"))
                            );
                            return;
                          }
                          double number1 = double.parse(text1);
                          double number2 = double.parse(text2);

                          if(number2 == 0){
                            ScaffoldMessenger
                                .of(context)
                                .showSnackBar(
                                SnackBar(content: Text("Khong chia cho so 0"))
                            );
                            return;
                          }

                          setState(() {
                            dataResult = (number1 + number2);
                          });
                        }, child: Text("/")),
                      ),
                    )
                  ],
                )
              ],
            ),
          )),
    );
  }
}

 

728x90
반응형