
처음 플러터 프로젝트 생성시 widget_test.dart에 있는 테스트 코드들도
main.dart 의 예시 코드를 기준으로 만들어진 내용이라 전부 삭제해준다.

import 'package:flutter/material.dart';
class CalApp extends StatefulWidget {
const CalApp({super.key});
State<CalApp> createState() => _CalAppState();
}
class _CalAppState extends State<CalApp> {
Widget build(BuildContext context) {
return const Placeholder();
}
}
void main(){
runApp(CalApp());
}
return MaterialApp(
title : "4칙 연산 계산기",
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: Scaffold(
),
);
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("사칙연산 계산기"),
),
body: Container(
),
body: Container(
padding: EdgeInsets.all(10),
),
body: Container(
padding: EdgeInsets.all(10),
// 위에서 아래로 배치하는 레이아웃 요소를 설정한다.
child: Column(
children: [
],
),
),
// 첫 번째 입력 요소를 배치한다.
TextField(
// 입력 요소의 모양을 설정한다.
decoration: InputDecoration(
// 외곽선 모양
border: OutlineInputBorder(),
// hint
labelText: "첫 번째 숫자"
),
// 입력가능한 데이터 양식
inputFormatters: [
// 숫자만 입력 가능하게
FilteringTextInputFormatter.digitsOnly
],
// 나타나는 키보드 타입 설정, 숫자 키보드로
keyboardType: TextInputType.number,
),
// 위젯의 여백을 설정할 수 있는 Padding 요소를 사용한다.
Padding(
padding: EdgeInsets.only(top: 20),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "두 번째 숫자"
),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly
],
keyboardType: TextInputType.number,
),
),
// 버튼을 배치하기 위한 Row를 배치한다.
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
],
),
)
// 버튼을 배치하기 위한 Row를 배치한다.
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("+")
),
),
SizedBox(width: 10),
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("-")
),
),
],
mainAxisSize: MainAxisSize.max,
),
),
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("*")
),
),
SizedBox(width: 10),
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("/")
),
),
],
mainAxisSize: MainAxisSize.max,
),
),
// 출력 결과를 출력할 텍스트 요소
Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
"결과 : 100",
style: TextStyle(
fontSize: 30
),
),
),
class _CalAppState extends State<CalApp> {
// 제일 하단에 결과가 나오는 Text 요소의 문자열을 가지고 있는 변수
var result_text = "결과 : 0";
// 출력 결과를 출력할 텍스트 요소
Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
result_text,
style: TextStyle(
fontSize: 30
),
),
),
class _CalAppState extends State<CalApp> {
// 제일 하단에 결과가 나오는 Text 요소의 문자열을 가지고 있는 변수
var result_text = "결과 : 0";
// 첫 번째 숫자 입력 요소의 컨트롤러
var number1_controller = TextEditingController();
// 두 번째 숫자 입력 요소의 컨트롤러
var number2_controller = TextEditingController();
TextField(
// 입력 요소의 모양을 설정한다.
decoration: InputDecoration(
// 외곽선 모양
border: OutlineInputBorder(),
// hint
labelText: "첫 번째 숫자"
),
// 입력가능한 데이터 양식
inputFormatters: [
// 숫자만 입력 가능하게
FilteringTextInputFormatter.digitsOnly
],
// 나타나는 키보드 타입 설정, 숫자 키보드로
keyboardType: TextInputType.number,
// 포커스를 준다.
autofocus: true,
// 컨트롤러를 연결해준다.
controller: number1_controller,
),
Padding(
padding: EdgeInsets.only(top: 20),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "두 번째 숫자"
),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly
],
keyboardType: TextInputType.number,
// 컨트롤러를 연결해준다.
controller: number2_controller,
),
),
// 연산후 출력하는 함수
void setResult(String op){
setState(() {
// 입력한 값을 가져온다.
int number1 = int.parse(number1_controller.text);
int number2 = int.parse(number2_controller.text);
// 연산자에 따라 연산 결과를 출력해준다.
switch(op){
case '+':
result_text = "결과 : ${number1 + number2}";
case '-':
result_text = "결과 : ${number1 - number2}";
case '*':
result_text = "결과 : ${number1 * number2}";
case '/':
result_text = "결과 : ${number1 / number2}";
}
});
}
child: OutlinedButton(
onPressed: () => setResult("+"),
child: Text("+")
),
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main(){
runApp(CalApp());
}
class CalApp extends StatefulWidget {
const CalApp({super.key});
State<CalApp> createState() => _CalAppState();
}
class _CalAppState extends State<CalApp> {
// 제일 하단에 결과가 나오는 Text 요소의 문자열을 가지고 있는 변수
var result_text = "결과 : 0";
// 첫 번째 숫자 입력 요소의 컨트롤러
var number1_controller = TextEditingController();
// 두 번째 숫자 입력 요소의 컨트롤러
var number2_controller = TextEditingController();
Widget build(BuildContext context) {
return MaterialApp(
title : "4칙 연산 계산기",
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("사칙연산 계산기"),
),
body: Container(
// 내부의 여백을 설정한다.
padding: EdgeInsets.all(10),
// 위에서 아래로 배치하는 레이아웃 요소를 설정한다.
child: Column(
children: [
// 첫 번째 입력 요소를 배치한다.
TextField(
// 입력 요소의 모양을 설정한다.
decoration: InputDecoration(
// 외곽선 모양
border: OutlineInputBorder(),
// hint
labelText: "첫 번째 숫자"
),
// 입력가능한 데이터 양식
inputFormatters: [
// 숫자만 입력 가능하게
FilteringTextInputFormatter.digitsOnly
],
// 나타나는 키보드 타입 설정, 숫자 키보드로
keyboardType: TextInputType.number,
// 포커스를 준다.
autofocus: true,
// 컨트롤러를 연결해준다.
controller: number1_controller,
),
// 위젯의 여백을 설정할 수 있는 Padding 요소를 사용한다.
Padding(
padding: EdgeInsets.only(top: 20),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "두 번째 숫자"
),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly
],
keyboardType: TextInputType.number,
// 컨트롤러를 연결해준다.
controller: number2_controller,
),
),
// 버튼을 배치하기 위한 Row를 배치한다.
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () => setResult("+"),
child: Text("+")
),
),
SizedBox(width: 10),
Expanded(
child: OutlinedButton(
onPressed: () => setResult("-"),
child: Text("-")
),
),
],
mainAxisSize: MainAxisSize.max,
),
),
// *, / 버튼
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () => setResult("*"),
child: Text("*")
),
),
SizedBox(width: 10),
Expanded(
child: OutlinedButton(
onPressed: () => setResult("/"),
child: Text("/")
),
),
],
mainAxisSize: MainAxisSize.max,
),
),
// 출력 결과를 출력할 텍스트 요소
Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
result_text,
style: TextStyle(
fontSize: 30
),
),
),
],
),
) ,
),
);
}
// 연산후 출력하는 함수
void setResult(String op){
setState(() {
// 입력한 값을 가져온다.
int number1 = int.parse(number1_controller.text);
int number2 = int.parse(number2_controller.text);
// 연산자에 따라 연산 결과를 출력해준다.
switch(op){
case '+':
result_text = "결과 : ${number1 + number2}";
case '-':
result_text = "결과 : ${number1 - number2}";
case '*':
result_text = "결과 : ${number1 * number2}";
case '/':
result_text = "결과 : ${number1 / number2}";
}
});
}
}