목차
- 들어가기
- 코딩과 개발언어
- 왜 Flutter를 배워야 할까?
- 용어 정리 1편 (다트.객체지향언어)
- 용어 정리 2편 (변수.자료형)
- 용어 정리 3편 (함수)
- 용어 정리 4편 (조건문)
- 용어 정리 5편 (반복문)
- 용어 정리 6편 (플러터의 위젯)
- 잠시 쉬어가기편 - 플러터는 어떤 IDE가 좋을까? IDE의 미래?
- Flutter가 실행되는 구조
Flutter 설치방법은 인터넷상에 자세하게 나와 있으므로 저는 따로 적지는 않았습니다
오늘은 Flutter가 실행되는 구조, 정확히는 입력된 코드가 어떻게 화면에 나오는지 그 순서에 대해서 설명해드리겠습니다.
처음 코딩을 공부했을 때 개인적으로 헷갈렸던 부분입니다.
아래 코드는 Flutter 파일을 새로 열면 나오는 예시 코드입니다.
Flutter가 코드 상에서 실행되는 순서는 다음과 같습니다.
1. void main() {runApp(const MyApp());}
- 어플을 시작한다는 함수입니다. 아직 화면은 열리지 않습니다.
2. class MyApp extends StatelessWidget {.....}
- 어플이 실행되고
MyApp 위젯이 있다는 내용입니다. 이 위젯안에 title, theme : , home : 값들을 넣을 수 있습니다.
아직 화면은 나오지 않습니다.
title : 어플의 이름을 짓습니다.
theme : 어플의 테마를 지정할 수 있습니다.
home : 앞으로 어플에 필요한 기능과,위젯들을 넣을 공간입니다.
title값, theme값은 굳이 넣지 않으셔도 됩니다. 하지만
home : 값은 앞으로 제작할 위젯들의 내용들이 들어가야 하는 곳이므로 꼭 입력해주셔야 합니다.
home :아래 예시코드는 MyHomePage(), 이런식으로 작성되며 MyHomePage라는 이름은 원하는 이름으로 바꾸셔도 됩니다.
저는 개인적으로 HomePage(), 이름을 많이 쓰고 있습니다.
3. class MyHomePage extends StatefulWidget {......} 그리고 Scaffold
- MyApp 안에 있는 home: 이란 공간안에 만든 MyHomePage 위젯입니다.
- 이 위젯안에 Scaffold라는 위젯이 있습니다. Scaffold라는 영어의 뜻은 건물을 지을 때 건물 외벽에 설치하는 '발판' 이라는 뜻을 가지고 있습니다. 그 '발판' 을 통해서 건물을 멋지게 지을 수 있는 것처럼 여러분이 만들고 싶은 어플에 필요한 위젯,함수들은 이 Scaffold 안에서 만드시면 됩니다.
Scaffold까지 만드시고 그 안을 채워넣어야 화면이 그려지게 됩니다.
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: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}