์žฌ๋ฐŒ๋Š”๊ฑฐ๐ŸŒˆ

Flutter_11. Drawer, ListTitle, Navigator, body, class, Stateful ๋ณธ๋ฌธ

๊ฐœ๋ฐœํ•˜๊ธฐ/Flutter Start

Flutter_11. Drawer, ListTitle, Navigator, body, class, Stateful

uraon 2023. 5. 5. 23:22
728x90
๋ฐ˜์‘ํ˜•

์˜ค๋Š˜์€ ์ง€๋‚œ๋ฒˆ ํฌ์ŠคํŒ…์— ์ด์–ด์„œ Drawer์— ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ํŽ˜์ด์ง€ ์ด๋™์„ ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

2023.05.04 - [๊ฐœ๋ฐœํ•˜๊ธฐ] - Flutter_10. Drawer, AppBar IconButton

 

Flutter_10. Drawer, AppBar IconButton

์˜ค๋Š˜์€ AppBar์— IconButton์„ ๋„ฃ๊ณ , ํด๋ฆญ์‹œ Drawer๋ฅผ ์—ด์–ด๋ณด๋Š” ๊ฒƒ์„ ๋ฉ”๋ชจํ•˜๋ คํ•œ๋‹ค. AppBar์— ์‚ฌ์šฉ๋˜๋Š” IconButton์€ ์œ„์น˜์— ๋”ฐ๋ผ leading, actions๋กœ ๋‚˜๋‰œ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ ํ™•์ธ! :) https://api.flutter.dev/

uraon.tistory.com

 

 

 


Routing, Drawer, ListView

 

 

 

1) ํด๋” "page"์— ํŒŒ์ผ ์ถ”๊ฐ€ํ•˜๊ณ , main.dart ํŒŒ์ผ์— Routing ์…‹ํŒ…

 

 

2) home.dart ํŒŒ์ผ > Drawer - Header ๋งŒ๋“ค๊ธฐ ๋ณต์Šต ๋ฉ”๋ชจ!

UserAccountsDrawerHeader
- ๋ฉ”์ธ ์ด๋ฏธ์ง€ currentAccountPicture: CircleAvatar
- ์„œ๋ธŒ ์ด๋ฏธ์ง€ otherAccountsPictures: [CircleAvatar, CircleAvatar, ...]
- ๊ณ„์ • ๋„ค์ด๋ฐ accountName: Text
- ๊ณ„์ • ์ด๋ฉ”์ผ accountEmail: Text
- ๊พธ๋ฏธ๊ธฐ decoration: BoxDecoration()

 

 

3) home.dart ํŒŒ์ผ > Drawer - ListTitle ๋งŒ๋“ค๊ธฐ

ListTitle
- ์•„์ด์ฝ˜ leading: Icon
- ๋ฆฌ์ŠคํŠธ title: Text
- ์•„์ด์ฝ˜ trailing: Icon
- ํด๋ฆญ ์ด๋ฒคํŠธ onTap: (){}

 

 

4) Navigator.pop์œผ๋กœ ํƒญํ•˜๋ฉด Drawer ๋‹ซํž˜ ์ฒ˜๋ฆฌ

    ListTile(
    leading: const Icon(
        Icons.label_important_outline_rounded,
        color: Color.fromARGB(255, 70, 12, 81),
    ),
    title: const Text('01 - Image Swipe'),
    trailing: const Icon(
        Icons.arrow_right
    ),
    onTap: () {
       Navigator.pop(context);
        },
    ),

 

 

5) ์ €์žฅํ•˜๊ณ  Drawer ํด๋ฆญ ํ™•์ธ! ์ด์ƒ์ด ์—†๋‹ค๋ฉด, ListTitle ๋ณต์‚ฌ~*-*// 

#Navigator.pop์œผ๋กœ ๋‹ซ๊ณ , pushNamed๋กœ ์ด๋™ ๊ฒฝ๋กœ ์„ค์ •

    onTap: () {
        Navigator.pop(context);
        Navigator.pushNamed(context, '/page01');
    },

 

 

 

6) ์ €์žฅํ•˜๊ณ  Drawer ํด๋ฆญ๊ณผ ํŽ˜์ด์ง€ ์ด๋™ ํ™•์ธ!

 

 

 

 

 

body, class, StatefulWidget

 

 

Stateless๋กœ home.dart ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ , Drawer๋ฅผ ๋„ฃ์—ˆ๋”๋‹ˆ ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๊ธธ์–ด์กŒ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ํ™ˆ๋ฉ”์ธ์— ์ฒซ๋ฒˆ์งธ๋กœ ๋ณต์Šตํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ์ด๋ฏธ์ง€ ์Šค์™€์ดํ”„ ํ™”๋ฉด์ž„์œผ๋กœ ์›€์ง์ž„ ๋ณ€ํ™”๊ฐ€ ์žˆ๋Š” ์œ„์ ฏ์ด ํ•„์š”ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ body๋ฅผ ๋ณ„๋„์˜ Stateful ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•ด๋ณด์•˜๋‹ค. ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ–ˆ๋‹ค :)

๋”๋ณด๊ธฐ
  • Stateless : ์ƒํƒœ๊ฐ€ ์—†๋Š” ์ •์  ์œ„์ ฏ
  • Statefull : ์›€์ง์ž„ ๋ณ€ํ™”๊ฐ€ ์žˆ๋Š” ์œ„์ ฏ

 

 

1) home.dart ํŒŒ์ผ body์— const๋กœ class ์ด๋ฆ„ "ImageSwipe"๋กœ ๋„ฃ๊ธฐ

 

2) img_swipe.dart ํŒŒ์ผ ์‹ ๊ทœ ์ƒ์„ฑ

3) sateful๋กœ class ๋งŒ๋“ค๊ณ , return์— body์˜ Center๋ถ€ํ„ฐ ๋‚ด์šฉ ์ถ”๊ฐ€

- stf + Enter : StatefulWidget์œผ๋กœ ๊ธฐ๋ณธ ์–‘์‹ ์ž๋™์ถœ๋ ฅ
- ํด๋ž˜์Šค ์ด๋ฆ„ "ImageSwipe"๋กœ ๋ณ€๊ฒฝ
- ์ž๋™ ์ƒ์„ฑ๋œ import ์ง€์šฐ๊ณ , material ์ถ”๊ฐ€
- ๋ฆฌํ„ด๊ฐ’ Center();๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , home.dart์˜ Center๋‚ด์šฉ ์งค๋ผ์„œ ๋ถ™์—ฌ๋„ฃ๊ธฐ

 

์ €์žฅํ•˜๊ณ  ํ™•์ธํ•˜๋ฉด, ํŒŒ์ผ๋งŒ ๋ถ„๋ฆฌ๋˜์—ˆ๊ณ  ํ™”๋ฉด์€ ๊ทธ๋Œ€๋กœ:)

 

 

 

 

 

 

 

Stateful Life Cycle

 

 

๋”๋ณด๊ธฐ

๐Ÿ’‹ Stateless Widgets

Constructor Function -> build()

 

๐Ÿ’‹ Stateful Widgets์˜ Life Cycle

Constructor Function -> initState() -> build() -> setState() -> didUpdateWidget() -> build()

initState() : ์ƒํƒœ ์ดˆ๊ธฐํ™”(=๋ณ€์ˆ˜ ์ •์˜)
build() : ๋งŒ๋“ค๊ณ 
setState() : ์ƒํƒœ ๊ฐฑ์‹  -> build()

 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด, initState()๋Š” ์ตœ์ดˆ 1ํšŒ๋งŒ ์‹คํ–‰๋œ๋‹ค.

๋ฒ„ํŠผ ํด๋ฆญ์— ๋”ฐ๋ผ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉฐ, ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค setState๋ฅผ ํ†ตํ•ด build()๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {


  // 1. ํ•„๋“œ์— (์ „์—ญ)๋ณ€์ˆ˜ late๋กœ ์„ ์–ธ
  late String _buttonState;
  late Color _buttonColor;

  // 2. initState : ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •(์•ฑ ์ฒ˜์Œ ์‹คํ–‰ํ• ๋•Œ๋งŒ run)
  @override
  void initState() {
    super.initState();
    print('initState()');
    _buttonState = "OFF";
    _buttonColor  = Colors.amber;
  }


  @override
  void deactivate() {
    print('deactivate()');
    super.deactivate();
  }

  // 3. dispose : ์•ฑ์ด ์ข…๋ฃŒ์ •์˜
  @override
  void dispose() {
    print('dispose()');
    super.dispose();
    
  }


  @override
  Widget build(BuildContext context) {
    print('build()');
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Life Cycle'),
        backgroundColor: const Color.fromARGB(255, 98, 104, 255),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(_buttonColor)
              ),
              onPressed: () {
                // -------------
                _onClick();
                
              }, 
              child: const Text('๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์„ธ์š”'),
              ),
              const SizedBox(
                height: 50,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text('๋ฒ„ํŠผ ์ƒํƒœ :'),
                  const SizedBox(
                    width: 10,
                  ),
                  Text(_buttonState)
                ],
              )

          ],
        ),
      ),
    );
  }

  // --- Functions ---
  _onClick(){
    // setState : buildํ•œํ…Œ ๋ณ€์ˆ˜ ๋ณ€๊ฒฝ ์•Œ๋ ค์ฃผ๊ธฐ!
    setState(() {
      if(_buttonState == "OFF"){
        _buttonState = 'ON';
        _buttonColor  = Colors.red;
      }else{
        _buttonState = 'OFF';
        _buttonColor  = Colors.amber;
      }
      
    });
    print(_buttonState);
  }

}//End

 

 

๋ฐ˜์‘ํ˜•

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•