๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
APP/FLUTTER

FLUTTER #2 Dicee & Dart

by mingzoo 2020. 9. 11.

Hot Reload 

์ฝ”๋”ฉ๊ณผ ๋™์‹œ์— ํ™”๋ฉด์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ธฐ๋Šฅ

stateless ์œ„์ ฏ ์•ˆ return์— MaterialApp์„ ๋„ฃ์œผ๋ฉด ์ˆœ์‹๊ฐ„์— ์ˆ˜์ •๊ฐ€๋Šฅํ•ด์ ธ์„œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ์ด ํ›จ์”ฌ ๋นจ๋ผ์ง

 

Container Widget

์‚ฌ๊ฐํ˜•์˜ ์‹œ๊ฐ์ ์š”์†Œ๋ฅผ ๋งŒ๋“ค์–ด์คŒ(๋ฐฐ๊ฒฝ, ํ…Œ๋‘๋ฆฌ, ๊ทธ๋ฆผ์ž, ํŒจ๋”ฉ, ์—ฌ๋ฐฑ, ํ–‰๋ ฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

Container ์œ„์ ฏ์—์„œ๋Š” children์ด ์—†์œผ๋ฉด ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง€๊ฒŒ๋œ๋‹ค.

- alt + enter → wrap with widget

 

Safe Area

์œ„์ ฏ์ด๋ฆ„์„ SafeArea๋ฅผ ์ž…๋ ฅํ•˜๋ฉด appbar๋ฐ‘์— ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ๊ธด๋‹ค.

 

Margin

  • margin ์ปจํ…Œ์ด๋„ˆ์˜ ์—ฌ๋ฐฑ margin : EdgeInsets.all(20,20) ๋ชจ๋“  ์ƒํ•˜์ขŒ์šฐ์˜ ์—ฌ๋ฐฑ์„ 20์œผ๋กœ ํ•จ margin:
  • EdgeInsets.symmetric(vertical:50.0,horizontal:10.0) top,bottom์„ ๋‚˜ํƒ€๋ƒ„
  • EdgeInsets.fromLTRB(30.0,10.0,50.0,20.0) ์ˆœ์„œ๋Œ€๋กœ left, top, right, bottom์˜ ์—ฌ๋ฐฑ์„ ๋‚˜ํƒ€๋ƒ„
  • EdgeInsets.only(left:30.0) ํŠน์ •ํ•œ ๋ช‡๋ถ€๋ถ„๋งŒ ์—ฌ๋ฐฑ์„ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ(์—ฌ๋Ÿฌ๊ฐœ๋„ ๊ฐ€๋Šฅ) ์„ ํƒ์ ์šฉ

Column & Row

์ˆ˜ํ‰์œผ๋กœ ๋ฐฐ์น˜(Row), ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜(column)

  • MainAxisAlignment - row, column ์ •๋ ฌ
  • MainAxisAlignment : MainAxisSize.min ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ฝ‰์ฐจ๊ฒŒ ๋ฐฐ์น˜
  • MainAxisAlignment : MainAxisSize.start ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์™ผ์ชฝ์ƒ๋ถ€์— ์œ„์น˜
  • MainAxisAlignment : MainAxisSize.end ์™ผ์ชฝ ํ•˜๋ถ€์— ์œ„์น˜
  • MainAxisAlignment : MainAxisSize.center ์™ผ์ชฝ ์ค‘์•™์— ์œ„์น˜
  • MainAxisAlignment : MainAxisSize.spaceEvenly ์ปจํ…Œ์ด๋„ˆ๊ฐ„์˜ ๊ฐ„๊ฒฉ์ด ๊ณ ๋ฅด๊ฒŒ ์œ„์น˜ํ•จ

crossAxisAlignment : CrossAxisAlignment.end → ์ปจํ…Œ์ด๋„ˆ ์ค‘ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ๊ฒƒ์˜ ๋์„ ๋งž์ถค

crossAxisAlignment : CrossAxisAlignment.end → ์ปจํ…Œ์ด๋„ˆ ์ค‘ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ๊ฒƒ์˜ ๋์„ ๋งž์ถค

 

* ctrl + q : ์“ธ ์ˆ˜ ์žˆ๋Š” child๋ฅผ ์†Œ๊ฐœ

 

ํฐํŠธ ๋ฐ”๊พธ๊ธฐ

https://flutter.io/custom-fonts/#from-packages ์ฐธ๊ณ 

fonts.google.com ์—์„œ ํฐํŠธ ๋‹ค์šด

์ƒ์—…์  ํฐํŠธ ๋‹ค์šด : https://noonnu.cc/about

directory ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ํฐํŠธ ๊ทธ ํด๋”์— ์‚ฝ์ž… pubspec.yaml์—

//pubspec.yaml
fonts:
-family: Pacifico
 fonts:
 - assets : fonts/Pacifico-Regulat.ttf
//main.dart
Text('Minjoo Park', style : TextStyle(
fontFamily : 'Pacifico',
fontSize : 40.0,
color : Colors.white,
fontWeight : FontWeight.bold,
),
),

๋ฉ”์ธํ•จ์ˆ˜์— ๊ฐ€์„œ Text์•ˆ์— style: TextStyle(fontfamily: 'Pacifico')

 

์ด๋ฏธ์ง€ ์‚ฝ์ž…

// before
image: AssetImage('images/urbanbrush-20190125013748099992.png')
//After
child: Image.asset('images/dice1.png')

Icon Class

Icon(Icons.add_shopping_cart) //์‡ผํ•‘์นดํŠธ ๋ชจ์–‘ ์•„์ด์ฝ˜ ๋„ฃ๊ธฐ

 

์œ„์ ฏ์„ ์„ผํ„ฐ๋กœ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•

Sol 1) Center(

           child : Row( ....

Sol 2) Row์— ์ปค์„œ๋ฅผ ๋†“๊ณ  Flutter Online - Center Widget ํด๋ฆญ

 

Random Number

$leftDiceNumber ⇒ String Interpolation(๋ฌธ์ž์—ด ๋ณด๊ฐ„)

์ฝ”๋“œ ์ƒ๋‹จ์— import 'dart:math' ์ž…๋ ฅ

leftDiceNumber = Random().nextint(int max)//์—ฌ๊ธฐ์„œ max๋Š” ๋ฒ”์œ„์— ํฌํ•จํ•˜์ง€ ์•Š์Œ.
//1~6๊นŒ์ง€์˜ ๋žœ๋ค์ˆซ์ž๋ฅผ ํ‘œ์‹œ
Random().nextint(int 6) + 1;

 

Dart : Statically Typed Language

Variable - Type Safe Language

Data Type

  • String
  • bool-True, False

๋ณ€์ˆ˜์„ ์–ธ

var ____; //dynamic์œผ๋กœ ์‚ฌ์šฉ
dynamic c = _____; //์•„๋ฌด๊ฑฐ๋‚˜ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ

๋‹คํŠธ๋Š” statically typed language์ด์ง€๋งŒ, dynamic์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ

 

Stateful/Stateless

stateless๋Š” ์ƒํƒœ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์Œ, ๋ณ€ํ•˜์ง€ ์•Š์Œ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฐ•๋ฌผ๊ด€ ๊ฐ™์€๊ฑฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์‹œ๋ฉด ๋ฐ•๋ฌผ๊ด€์— ๊ฐ€๋ฉด ๊ฐ์ž ์ž๋ฆฌ์— ์ž‘ํ’ˆ๋“ค์ด ์žˆ๊ณ  ๊ทธ ์ž‘ํ’ˆ์„ ๋งŒ์ ธ์„œ๋Š” ์•ˆ๋˜๊ณ  ๋ณด๊ธฐ๋งŒ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ ์ž‘ํ’ˆ๋“ค์€ ํ•ญ์ƒ ๊ทธ์ž๋ฆฌ์— ์œ„์น˜ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์— ์“ฐ์ด๋Š”๊ฒŒ Stateless Widget stateful๋Š” ์ƒํƒœ๋ฅผ ๊ฐ€์ง, ๋ณ€ํ•  ์ˆ˜ ์žˆ์Œ ⇒ stful(shortcut) setState()๋Š” ์•ˆ์— ๋ณ€๋™๋œ ์‚ฌํ•ญ์œผ๋กœ hot reload ํ–ˆ์„ ๋•Œ, ์žฌ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ - data has changed stateless ์œ„์ ฏ์€ ๊ทธ๋ƒฅ extends ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€๋งŒ stateful ์œ„์ ฏ์€ ์ƒ์†๋„ ๋ฐ›๊ณ  ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค๊ฑฐ๋‚˜ ์ด๋ฏธ์ง€๊ฐ€ ๋ฐ”๋€๋‹ค๋˜๊ฐ€ ๋“ฑ ์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ”์ค„ ์†Œ์Šค๋“ค์„ setState ๋ฉ”์„œ๋“œ ์•ˆ์— ๋„ฃ์–ด์ค˜์•ผํ•จ

 

728x90

'APP > FLUTTER' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Flutter #3] Xylophone & Quizzler  (0) 2021.01.05
FLUTTER #4 BMI Calculator  (0) 2020.01.19
FLUTTER #1 I_am_rich  (0) 2020.01.18