Flutter Basic

ν”ŒλŸ¬ν„°κ°€ Dart λ₯Ό μ‚¬μš©ν•˜λŠ” 이유

ν”ŒλŸ¬ν„°κ°€ Dart λ₯Ό μ‚¬μš©ν•˜λŠ” μ΄μœ μ— λŒ€ν•΄μ„œ 잘 μ •λ¦¬λœ 글에도 λ‚˜μ™€μžˆκ³  책에도 λ‚˜μ™€μžˆλ“―μ΄ λ‹€νŠΈκ°€ JIT 컴파일, AOT 컴파일 λͺ¨λ‘λ₯Ό μ§€μ›ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

ν”ŒλŸ¬ν„°μ˜ μž₯점(= λ¦¬μ•‘νŠΈ λ„€μ΄ν‹°λΈŒμ™€ λΉ„κ΅ν•˜μ—¬)

μ±…μ—μ„œλŠ” β€˜μžμ²΄ λ Œλ”λ§ 엔진을 νƒ‘μž¬ν•˜κ³  있고, λ°”λ‘œ ARM μ½”λ“œλ‘œ 컴파일 ν•œλ‹€.β€™λŠ” μž₯점 덕뢄에 β€˜μ•±μ΄ λ„€μ΄ν‹°λΈŒλ‘œ κ΅¬λ™β€™λ˜λ©° λ¦¬μ•‘νŠΈ λ„€μ΄ν‹°λΈŒμ²˜λŸΌ μžλ°” 슀크립트 λΈŒλ¦Ώμ§€μ™€ 같은 쀑간 계측이 ν•„μš”κ°€ μ—†λ‹€κ³  ν•˜κ³  μžˆλ‹€.

κ³΅μ‹λ¬Έμ„œμ—μ„œ μ„€λͺ…ν•˜κ³  μžˆλŠ” μ•„ν‚€ν…μ²˜μ—μ„œ 이와 κ΄€λ ¨λœ μ—¬λŸ¬ λ‚΄μš©λ“€μ„ 확인할 수 μžˆλŠ”λ° λͺ‡λͺ‡ ν‘œν˜„λ§Œ 봐도 같은 λ§₯λ½μ—μ„œ μ„€λͺ…ν•˜κ³  μžˆλ‹€.

Flutter is a cross-platform UI toolkit that is designed to allow code reuse across operating systems such as iOS and Android, while also allowing applications to interface directly with underlying platform services.

During development, Flutter apps run in a VM that offers stateful hot reload of changes without needing a full recompile. For release, Flutter apps are compiled directly to machine code, whether Intel x64 or ARM instructions, or to JavaScript if targeting the web.

To the underlying operating system, Flutter applications are packaged in the same way as any other native application. A platform-specific embedder provides an entrypoint; coordinates with the underlying operating system for access to services like rendering surfaces, accessibility, and input; and manages the message event loop.

μ „μ²΄μ μœΌλ‘œ ν•œ λ§ˆλ””λ‘œ μš”μ•½ν•˜μžλ©΄ ν”ŒλŸ¬ν„°λŠ” μ–΄λŠ ν”Œλž«νΌμ΄λ“  ν•΄λ‹Ή ν”Œλž«νΌμ—μ„œ λ™μž‘ν•˜λŠ” native application 처럼 νŒ¨ν‚€μ§•λ˜κ³  ν•΄λ‹Ή ν”Œλž«νΌμ˜ μ„œλΉ„μŠ€λ“€κ³Ό μ§μ ‘μ μœΌλ‘œ λ§žλ‹Ώμ•„ λ™μž‘ν•˜λ„λ‘ μ„€κ³„λ˜μ—ˆλ‹€λŠ” 것이닀.

λ°˜λŒ€λ‘œ λ¦¬μ•‘νŠΈ λ„€μ΄ν‹°λΈŒμ˜ 경우 Javascript bridge λ₯Ό κ°€μš΄λ°μ— 두고 ν”Œλž«νΌ μ„œλΉ„μŠ€λ“€κ³Ό ν˜‘λ ₯ν•΄μ•Όν•΄μ„œ κ³„μ†λ˜λŠ” 이벀트 λ“±μ—μ„œ λΉ„μš©μ΄ 크닀. μ΄λŸ¬ν•œ λ¦¬μ•‘νŠΈ λ„€μ΄ν‹°λΈŒ ꡬ동방식 및 단점듀은 μ •λ¦¬λœ λΈ”λ‘œκ·Έκ°€ λ§Žμ•„μ„œ 링크만 남긴닀.

λͺ¨λ“  것이 μœ„μ ―

ν”ŒλŸ¬ν„°λŠ” λͺ¨λ“  것이 μœ„μ ―μ΄λ©° μœ„μ ―μ˜ μ‘°ν•©μœΌλ‘œ UIλ₯Ό κ΅¬μ„±ν•œλ‹€. ν”ŒλŸ¬ν„°μ— λ‹€λ₯Έ 객체가 μ—†λ‹€λŠ” 뜻이 μ•„λ‹ˆκ³ , 앱을 κ΅¬μ„±ν•˜λŠ” λͺ¨λ“  ꡬ성듀이 μœ„μ ― ν˜Ήμ€ μœ„μ ―μœΌλ‘œ μ‘°ν•©λœ μœ„μ ―μ΄λΌλŠ” λœ»μ—μ„œ β€˜λͺ¨λ“  것이 μœ„μ ―β€™μ΄λΌλŠ” λœ»μ΄λ‹€.

μ—¬κΈ°μ„œ β€˜μ‘°ν•©β€™μ„ 잘 κΈ°μ–΅ν•΄μ•Όν•˜λŠ” 것이, ν”ŒλŸ¬ν„°μ—μ„œλŠ” 기본적으둜 β€˜μƒμ†β€™μ΄ μ•„λ‹Œ β€˜μ‘°ν•©β€™μ„ ν†΅ν•΄μ„œ μœ„μ ―μ„ κ΅¬μ„±ν•œλ‹€. 예λ₯Ό λ“€μ–΄ Button μœΌλ‘œμ„œ 곡톡 κΈ°λŠ₯, μ»€μŠ€ν…€ κΈ°λŠ₯이 λ‘˜ λ‹€ ν•„μš”ν•œ κ²½μš°μ— 일반적으둜 객체 μ§€ν–₯ λ°©μ‹μœΌλ‘œ κ³΅ν†΅λ˜λŠ” λΆ€λͺ¨ Button 을 λ§Œλ“€κ³  이λ₯Ό μƒμ†λ°›μ•„μ„œ μ»€μŠ€ν…€ Button 을 λ§Œλ“œλŠ”κ²Œ μ•„λ‹ˆλΌ μ»€μŠ€ν…€ Button 에 곡톡 Button 을 β€˜μ‘°ν•©β€™ν•΄μ„œ κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.

StatelessWidget vs StatefulWidget

StatelessWidget

StatelessWidget 은 ν”„λ ˆμž„μ›Œν¬μ— κ·Έ μ–΄λ–€ 것도 μ•Œλ¦¬μ§€ μ•ŠλŠ”λ‹€. ν”„λ ˆμž„μ›Œν¬μ—μ„œ μœ„μ ―μ— μ–Έμ œ λ¦¬λΉŒλ“œ ν•΄μ•Όν•˜λŠ”μ§€ μ•Œλ €μ€€λ‹€. μ² μ €ν•˜κ²Œ μ™ΈλΆ€(=ν”„λ ˆμž„μ›Œν¬)에 μ˜ν•΄μ„œ 생λͺ…이 κ²°μ •(β€˜μƒλͺ…이 결정’ λœλ‹€λŠ” 말은 μœ„μ ―μ˜ 생λͺ…μ£ΌκΈ°κ°€ μˆ˜λ™μ μœΌλ‘œ λ°œμƒν•œλ‹€λŠ” μ˜λ―Έμ΄λ‹€.)λœλ‹€.

μˆ˜λ™μ μœΌλ‘œ μ² μ €νžˆ μ •μ˜λœ κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€. 즉, μƒνƒœκ°€ μ—†λŠ” μœ„μ ―μœΌλ‘œμ„œ μƒˆλ‘œμš΄ 정보에 λ°˜μ‘(react)ν•œλ‹€.

StatefulWidget

StatefulWidget 은 항상 State 객체λ₯Ό κ°–λŠ”λ‹€. State κ°μ²΄λŠ” setState()λž€λŠ” λ©”μ„œλ“œλ₯Ό κ°–κ³  μžˆλŠ”λ°, μœ„μ ―μ„ λ‹€μ‹œ 그렀야함을 ν”ŒλŸ¬ν„°μ— μ•Œλ¦¬λŠ” κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€. 이 ν¬μΈνŠΈμ—μ„œ StatelessWidget 와 크게 κ΅¬λ³„λœλ‹€. StatelessWidget λŠ” ν”„λ ˆμž„μ›Œν¬μ— μ˜μ‚¬ν‘œν˜„μ„ ν•  수 μ—†λŠ”λ° λ°˜ν•΄, StatefulWidget λŠ” μ–Έμ œ μœ„μ ―μ„ λ‹€μ‹œ κ·Έλ €μ•Όν•˜λŠ”μ§€μ— λŒ€ν•΄μ„œ ν”„λ ˆμž„μ›Œν¬μ— μœ„μ ―μ΄ λ°œμ–ΈκΆŒμ„ κ°€μ§€κ³  μžˆλŠ” 것이닀.

setState() 이 λ°œμƒν•  경우 ν•΄λ‹Ή μœ„μ ― 뿐만 μ•„λ‹ˆλΌ ν•΄λ‹Ή μœ„μ ―μ— μ˜μ‘΄ν•˜λŠ” λͺ¨λ“  μœ„μ ―μ„ λ‹€μ‹œ κ·Έλ €μ•Όν•œλ‹€κ³  ν”„λ ˆμž„μ›Œν¬μ— μ§€μ‹œν•œλ‹€.

StatefulWidget μœ„μ ― 생λͺ… μ£ΌκΈ°

좜처: https://betterprogramming.pub/stateful-widget-lifecycle-a01c44dc89b0

StatefulWidget μœ„μ ― 생λͺ… μ£ΌκΈ° 이미지 μΆœμ²˜μ΄κΈ°λ„ ν•œ ν¬μŠ€νŒ…μ— μ•„μ£Ό μžμ„Ένžˆ 각 생λͺ…μ£ΌκΈ° λ©”μ„œλ“œμ— λŒ€ν•΄ 정리가 λ˜μ–΄ μžˆλ‹€. 이 쀑 didUpdateWidget(), setState() 이 같은 계측에 μžˆμ–΄ 쑰금 더 ν™•μ‹€ν•˜κ²Œ ꡬ뢄 해두면 쒋을 것 κ°™μ•„μ„œ λ”°λ‘œ μ•„λž˜μ— 적어둔닀.

didUpdateWidget() Gets called if the parent widget changes its configuration and has to rebuild the widget. The framework gives you the old widget as an argument that you can use to compare it with the new widget. Flutter will call the build() method after it. Tip: Use this method if you need to compare the new widget to the old one.

@override
void didUpdateWidget(covariant MyHomePage oldWidget) {
  super.didUpdateWidget(oldWidget);
  // TODO: implement didUpdateWidget
}

setState() This method is often called from the Flutter framework itself and from the developer. The setState() method notifies the framework that the internal state of the current object is β€œdirty”, which means that it has been changed in a way that might impact the UI. After this notification, the framework will call the build() method to update and rebuild a widget. Tip: Whenever you change the internal state of a State object, make that change in the setState() method.

setState(() {
        // implement setState
        });

ν”ŒλŸ¬ν„° λ Œλ”λ§ λ™μž‘ 원리

ν”ŒλŸ¬ν„°κ°€ μœ„μ ―μ„ 화면에 λ Œλ”λ§ ν•˜κΈ°κΉŒμ§€ λ§Žμ€ 과정을 κ±°μΉœλ‹€. 이λ₯Ό μžμ„Ένžˆ μ•Œμ•„λ³΄λŠ” μ±•ν„°μ˜€λ‹€. λ Œλ”λ§ ν•˜κΈ°κΉŒμ§€μ˜ 과정은 μ•„λž˜μ™€ κ°™λ‹€.

μœ„μ ― 트리 μ™„μ„± -> λ ˆμ΄μ•„μ›ƒ 처리(μœ„μ—μ„œ μ•„λž˜λ‘œ β€˜μ œμ•½β€™ μ „νŒŒ -> μ•„λž˜μ—μ„œ μœ„λ‘œ β€˜ν¬κΈ°β€™ μ„€μ •) -> 쑰립 -> Rasterization

좜처 : Flutter In Action

  • μ œμ•½ : μžμ‹ λ…Έλ“œμ•Ό. λ„ˆλŠ” μ΅œλŒ€ 50px λ„ˆλΉ„λ₯Ό κ°€μ§ˆ 수 있고 ν™”λ©΄ ν•˜λ‹¨μ˜ μ ˆλ°˜μ„ μ°¨μ§€ν•  수 μžˆμ–΄.

  • 크기 : λΆ€λͺ¨ λ…Έλ“œλ‹˜. μ €λŠ” 25 x 25 px 을 μ°¨μ§€ν•˜κΈ°λ‘œ κ²°μ •ν–ˆκ³ , μ™Όμͺ½ μœ„ μ‹œμ μ μ€ (200, 30) μ΄μ—μš”.

μœ„μ ― 트리 μ™„μ„±

화면을 κ΅¬μ„±ν•˜λŠ” μœ„μ ―λ“€ 자체의 정보와 μœ„μ ―κ°„μ˜ 정보가 λ‹΄κΈ΄ μœ„μ ― 트리λ₯Ό λ§Œλ“ λ‹€.

λ ˆμ΄μ•„μ›ƒ 처리(μœ„μ—μ„œ μ•„λž˜λ‘œ β€˜μ œμ•½β€™ μ „νŒŒ -> μ•„λž˜μ—μ„œ μœ„λ‘œ β€˜ν¬κΈ°β€™ μ„€μ •)

μœ„μ ― νŠΈλ¦¬κ°€ μ™„μ„±λ˜λ©΄ μœ„μ—μ„œ μ•„λž˜λ‘œ(λΆ€λͺ¨μ—μ„œ μžμ‹ λ°©ν–₯으둜) β€˜μ œμ•½β€™μ΄ μ „νŒŒλœλ‹€. β€˜μ œμ•½β€™μ΄ λͺ¨λ‘ μ „νŒŒλ˜κ³  μ•„λž˜μ—μ„œ μœ„λ‘œ(μžμ‹μ—μ„œ λΆ€λͺ¨ λ°©ν–₯으둜) β€˜ν¬κΈ°β€™κ°€ μ „νŒŒλœλ‹€.

쑰립

μœ„μ ― 트리 μ™„μ„± 및 λ ˆμ΄μ•„μ›ƒ μ²˜λ¦¬κ°€ λͺ¨λ‘ λλ‚˜κ³  λ°”λ‘œ λž˜μŠ€ν„°λΌμ΄μ§•ν•˜μ§€ μ•Šκ³ , β€˜μ‘°λ¦½β€™μ˜ 과정을 κ±°μΉœλ‹€. 이 κ³Όμ •μ—μ„œ μœ„μ ―μ€ μ‹€μ œ μžμ‹ μ΄ μ²˜λ¦¬ν•  ν”½μ…€μ˜ 수λ₯Ό μ•Œκ²Œ λœλ‹€. 그리고 이 κ³Όμ •μ—μ„œ 기쑴의 μ»΄ν¬λ„ŒνŠΈλ₯Ό μž¬μ‚¬μš©μ²˜λ¦¬ ν•˜κ²Œ λœλ‹€.

Rasterization

Rasterization 단어 μžμ²΄κ°€ μ˜λ―Έν•˜λŠ” κ·ΈλŒ€λ‘œμ˜ ν–‰μœ„μ΄λ‹€. ν”ŒλŸ¬ν„° 자체 λ Œλ”λ§ 엔진을 ν†΅ν•΄μ„œ 픽셀을 μ±„μš°λŠ” 과정이닀.

Last updated