๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Flutter

[ Flutter / State ] Flutter์˜ ์ƒํƒœ๊ด€๋ฆฌ ํ›‘์–ด๋ณด๊ธฐ.

 

* Flutter ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์˜ State management ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์„ & ์ •๋ฆฌ ํ•œ ๊ธ€.

 

 

 

Flutter UI์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ๋‹ค๋ฅธ UI ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ นํ˜• ๋ฐฉ์‹๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์„ ์–ธํ˜• UI๋ผ๋Š” ์ ์ด๋‹ค.

๋ช…๋ นํ˜• ๋ฐฉ์‹์˜ UI ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ UI๊ฐœ์ฒด๋ฅผ ์ผ์ผ์ด ๊ตฌ์„ฑํ•˜๊ณ , ๋‚˜์ค‘์— UI๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฉ”์„œ๋“œ์™€ setter๋“ค์„ ์ด์šฉํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๋Š”

์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ ๋ฐฉ์‹์ด๋‹ค.

 

์ด์™€ ๋ฐ˜๋Œ€๋กœ Flutter๋Š” ํ˜„์žฌ์˜ UI ์ƒํƒœ๋งŒ ๋ฌ˜์‚ฌํ•˜๊ณ , ๋ณ€ํ™”๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์— ๋งก๊ธฐ๋„๋ก ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ Flutter์—์„œ UI๋ฅผ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃจ๋Š”์ง€์— ๋Œ€ํ•ด ์‚ฌ๊ณ  ์ „ํ™˜์ด ํ•„์š”ํ•˜๋‹ค.

 

 

Flutter์˜ ์ƒํƒœ ์†Œ๊ฐœ.

- ์ˆ˜๋ช…์ด ์งง์€ ์ƒํƒœ ( Ephemeral State )

- ์•ฑ ์ƒํƒœ ( App State )

 

 

 

1. ์ˆ˜๋ช…์ด ์งง์€ ์ƒํƒœ ( Ephemeral State ) / ์‚ฌ์šฉ ํ›„ ์‚ญ์ œ๋˜๋Š” ์ƒํƒœ : UI State, Local State๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค.

 ๋‹จ์ผ ์œ„์ ฏ์— ๊น”๋”ํ•˜๊ฒŒ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋กœ ์˜๋„์ ์œผ๋กœ ๋ชจํ˜ธํ•˜๊ฒŒ ์ •์˜ํ–ˆ์œผ๋ฏ€๋กœ ์•„๋ž˜์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด๊ณ  ํŒŒ์•…ํ•˜๋ฉด ๋œ๋‹ค.

 

- ํŽ˜์ด์ง€๋ทฐ(View)์˜ ํ˜„์žฌ ํŽ˜์ด์ง€

- ๋ณต์žกํ•œ ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ํ˜„์žฌ ์ง„ํ–‰๋ฅ .

- BottomNavigationBar์˜ ํ˜„์žฌ ์„ ํƒํ•œ ํƒญ.

 

์ฆ‰ ๋ณ„๋„์˜ ์ƒํƒœ ๊ด€๋ฆฌ(Redux, Getx, ScopedModel ๋“ฑ)๊ฐ€ ํ•„์š” ์—†๋Š” ์ƒํƒœ์ผ ๋•Œ, StatefulWidget์œผ๋กœ๋งŒ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

 

class MyHomepage extends StatefulWidget {
  @override
  _MyHomepageState createState() => _MyHomepageState();
}

class _MyHomepageState extends State<MyHomepage> {
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      currentIndex: _index,
      onTap: (newIndex) {
        setState(() {
          _index = newIndex;
        });
      },
      // ... items ...
    );
  }
}

 

_index ์„ ์–ธ ํ›„, onTap ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด _index์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•œ๋‹ค. _index๋Š”  MyHomepage ์œ„์ ฏ ์•ˆ์—์„œ๋งŒ ๋ณ€๊ฒฝ๋œ๋‹ค.

์‚ฌ์šฉ์ž๊ฐ€ ์•ฑ์„ ๋‹ซ๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋ฉด _index๋Š” ์ดˆ๊ธฐ ์„ค์ • ๊ฐ’์ธ 0์œผ๋กœ ์žฌ์„ค์ •๋œ๋‹ค.

 

 

 

 

 

2. ์•ฑ ์ƒํƒœ ( App State ) / ์‚ฌ์šฉ ํ›„ ์‚ญ์ œ๋˜์ง€ ์•Š๋Š” ์ƒํƒœ : Shared State๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค.

์•ฑ ์ƒํƒœ๋Š” ์‚ฌ์šฉ ํ›„ ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ  ์•ฑ์˜ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์—์„œ ๊ณต์œ ํ•˜๋ฉฐ ์‚ฌ์šฉ์ž ์„ธ์…˜ ๊ฐ„์— ์œ ์ง€ํ•˜๋ ค๋Š” ์ƒํƒœ์ด๋‹ค.

์•ฑ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€์˜ ์˜ˆ์‹œ๋Š” ์•„๋ž˜๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

 

- ์‚ฌ์šฉ์ž ๊ธฐ๋ณธ ์„ค์ •

- ๋กœ๊ทธ์ธ ์ •๋ณด

- SNS ์•ฑ์—์„œ ์•Œ๋ฆผ(Notification)

- E-Commerce ์•ฑ์—์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ

- ์ฝ์Œ / ์ฝ์ง€ ์•Š์Œ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„

 

 

 


 

 

> Flutter์—์„œ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ์–ด๋–ค state๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•œ ๊ทœ์น™์€ ์—†๋‹ค๊ณ  ๋งํ•œ๋‹ค.

๋‹ค์–‘ํ•œ ์ƒํ™ฉ์— ๋งž๊ฒŒ ๊ฐ์ž ์•Œ์•„์„œ ๊ณ ๋ฏผํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ผ๋Š” ๋œป์ธ ๊ฒƒ ๊ฐ™๋‹ค.

state๋ฅผ ์•Œ์•„๋ณด๋‹ค ๋ณด๋‹ˆ, Flutter์—์„œ  UI๊ฐ€ rebuild ๋˜๋Š” ์กฐ๊ฑด์ด๋‚˜ ์ˆ˜๋™ rebuild์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ์‹ถ์–ด์กŒ๋‹ค.

๋‹ค์Œ์—๋Š” Flutter UI ๋นŒ๋“œ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด์•ผ ๋ด์•ผ๊ฒ ๋‹ค!