배 타다 개발자

맥에서 플러터 설치 / 폴더 구성 / 생명주기 본문

Graphics/Flutter

맥에서 플러터 설치 / 폴더 구성 / 생명주기

노 아 2021. 12. 9. 23:20

맥에서 플러터 설치

<영상참조> 맥에 플러터 설치하기

플러터 프로젝트 폴더 구성

주요 폴더

제목 내용
android 안드로이드 프로젝트 관련 파일(안드로이드 스튜디오로 실행가능)
ios ios 프로젝트 관련파일(엑스코드로 실행가능)
lib 플러터 앱 개발을 위한 다트 파일(플러터 SDK 설치 필요) 우선은 lib 폴더에만 집중
test 플러터 앱 개발 중 테스트 파일(테스트 편의성 제공)

직접 관리하는 파일

제목 내용
pubspec.yaml 패키지, 이미지, 폰트 설정
README.md 프로젝트 소개
gitgnore git에 커밋,푸시할 때 필요없는 파일 기록

자동 관리되는 파일

제목 내용
.metadata FLutter SDK 정보
.packages Flutter SDK에 사용되는 기본 패키지 경로
first_flutter_app.iml 자동생성되는 만들어지는 폴더 위치
pubspec.lock pubspec.yaml 파일에 적용된 패키지 위치

위젯이란?

클랫그로 구현하며 이를 상속받는 다양한 위젯이 존재한다.
텍스트 클래스는 글자를 전문적으로 표시하는 위젯이고,
이미지 클래스는 이미지를 전문적으로 표시하는 위젯
버튼 클래스는 버튼을 표시하는 위젯이다.


위젯 생명주기

createState()


When the Framework is instructed to build a StatefulWidget, it immediately calls createState()

 

class MyHompate extends StatefulWidget
{
@override
_MyHomepateState createState() => new _MyHomePageState();
}

 

 

mounted is true


When createState creates your state class, a buildContext is assigned to that state. buildContext is, overly simplified, the place in the widget tree in which this widget is placed. Here's a longer explanation. All widgets have a bool this.mounted property. It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.

 

if (mounted)
{
    setState();
}

 

 

initState()
: State에서 제일 먼저 실행되는 함수, 주로 데이터 목록을 만들거나 처음 필요한 데이터를 주고받을 때 호출

This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must call super.initState().

 

 

@override
initState()
{
 super.initState();
 _getJsonDate();
 }

 

didChangeDependencies()


This method is called immediately after initState on the first time the widget is built.

build()
 : 위젯을 랜더링하는 함수

This method is called often. It is required, and it must return a Widget.

 

Widget build(BuilContext context){
  return MaterialApp(
    title: 'Flutter Demo';,
    theme: ThemeData(
    primarySwatch: Color.amber,
    ),
    home: MyHoomePage(title: ' Demo Home'),
  );
}

 

 

didUpdateWidget(Widget oldWidget)
: initState()함수는 생성될 때 한번만 호출되므로 이후에 위젯에 변경사항이 생길 때는 didUpdateWidget(Widget oldWidget) 함수를 호출하여 변경사항을 갱신한다.

 

If the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it's being rebuilt with the same runtimeType, then this method is called. This is because Flutter is re-using the state, which is long lived. In this case, you may want to initialize some data again, as you would in initState.

 

 

@override
void didUpdateWidget(Widget oldWidget){
    if(oldWidget.importantProperty != widget.importantProperty)
    {
    _init();
    }
}

 

위젯을 변경해야 할 때 호출하는 함수

setState()
: 데이터가 변경되었음을 알리는 함수 변경된 데이털르 UI에 적용하기 위해 필요하다.


This method is called often from the framework itself and from the developer. Its used to notify the framework that data has changed

 

void updateProfile(String name)
{
    setSate(() => this.name = name));
}

 

deactivate()
: 위젯의 상태관리를 중지,State가 제거될 때 호출, 아직 메모리상에서는 제거되지 않았다.


Deactivate is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another.

 

dispose()
: State가 완전히 제거 되었을 때 호출

dispose() is called when the State object is removed, which is permanent. This method is where you should unsubscribe and cancel all animations, streams, etc.

 

mounted is false


The state object can never remount, and error will be thrown if setState is called.

모든 프로세서가종료된 후 mounted가 flase