๊ฐœ๋ฐœ/Flutter

[Flutter/ํ”Œ๋Ÿฌํ„ฐ] Drawer menu ๋งŒ๋“ค๊ธฐ!

๋ฐ(Ming) ๐Ÿˆ‍โฌ› 2022. 4. 6. 00:38
728x90
๋ฐ˜์‘ํ˜•

โ—พ ์™„์„ฑ๋ชจ์Šต

 

์•ž์„œ ์•„์ด์ฝ˜์„ ๋„ฃ์–ด์„œ ๋งŒ๋“  ๋ฉ”๋‰ด์— ํ–„๋ฒ„๊ฑฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์‚ฌ์ด๋“œ๋กœ ์—ด๋ฆฌ๋Š” ๋ฉ”๋‰ด๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! flutter์—์„œ๋Š” drawer์ด๋ผ๋Š” ๊ฒƒ์œผ๋กœ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

 

โ—พ ๊ธฐ๋ณธ ๊ตฌ์„ฑ

 

 

drawer ์„ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— ์ž์‹ ์œ„์ ฏ(child)์œผ๋กœ ListView๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๊ทธ ์ž์‹ ์œ„์ ฏ ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ํ•˜์œ„ ์œ„์ ฏ(children)์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ListView๋Š” ๋‘๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ์ผ๋‹จ Header๋ถ€๋ถ„์—์„œ ๊ณ„์ •์ด๋ฆ„, ํ”„๋กœํ•„ ์‚ฌ์ง„ ๋“ฑ์„ ๋„ฃ์–ด์ฃผ๊ณ , ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋‰ด๋ฅผ ListTile์ด๋ผ๋Š” ์ด๋ฆ„ ์•„๋ž˜ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค ListTile๊ตฌ์„ฑ์— ๋ณด์‹œ๋ฉด onTap์ด ์žˆ๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์€ ๋‚˜์ค‘์— ๊ฐ€๋ฉด ๋” ๊นŠ๊ฒŒ ๊ณต๋ถ€ํ•  ์˜ˆ์ •์ด์ง€๋งŒ ๊ฐ„๋‹จํ•˜๊ฒŒ onPressed์™€ ๋น„๊ตํ•ด๋ณด์ž๋ฉด  onPressed๋Š” ์ฃผ๋กœ ๋ฒ„ํŠผ์— ์‚ฌ์šฉ์ด ๋˜๊ณ  onTap์€ ์ œ์Šค์ณ๊ฐ€ ๋”ํ•ด์ง„ ํด๋ฆญ ๋™์ž‘์„ ํ‘œํ˜„ํ•  ๋•Œ ์“ด๋‹ค๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ๊ฒŒ ๋ˆ„๋ฅธ๋‹ค๊ฑฐ๋‚˜ ๋‘๋ฒˆ ํƒญํ•˜๋Š” ๊ทธ๋Ÿฐ ๋™์ž‘! ๐Ÿ˜€

 

 

 

โ—พ Code

class Grade extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // web ํ™”๋ฉด๊ตฌ์„ฑ์„ ์œ„ํ•ด ๋„์™€์ฃผ๋Š” ๋„ํ™”์ง€๊ฐ™์€ ์š”์†Œ ์ด๊ฑฐ ํ•„์ˆ˜์ž„
    return Scaffold(
      backgroundColor: Colors.amber[800],
      appBar: AppBar(
        title: Text('MENU'),
        centerTitle: true,
        backgroundColor: Colors.amber[700],
        elevation: 0.0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.shopping_cart),
            onPressed: () {
              print('shopping cart button is clicked');
            },
          ),
          IconButton(
            icon: Icon(Icons.search),
            //iconbutton action
            onPressed: () {
              print('search button is clicked');
            },
          ),
        ],
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            UserAccountsDrawerHeader(
              currentAccountPicture: CircleAvatar(
                backgroundImage: AssetImage('assets/moon.png'),
                backgroundColor: Colors.white,
              ),
              otherAccountsPictures: <Widget>[
                CircleAvatar(
                  backgroundImage: AssetImage('assets/redux.png'),
                  backgroundColor: Colors.white,
                ),
              ],
              accountName: Text('Moon'),
              accountEmail: Text('moon@gmail.com'),
              onDetailsPressed: () {
                print('open drawer');
              },
              decoration: BoxDecoration(
                  color: Colors.red[200],
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(30.0),
                    bottomRight: Radius.circular(30.0),
                  )),
            ),
            ListTile(
              leading: Icon(Icons.home, color: Colors.grey[850]),
              title: Text("Home"),
              //gesture
              onTap: () {
                print('Home is clicked');
              },
              trailing: Icon(Icons.add),
            ),
            ListTile(
              leading: Icon(Icons.settings, color: Colors.grey[850]),
              title: Text("Setting"),
              //gesture
              onTap: () {
                print('Setting is clicked');
              },
              trailing: Icon(Icons.add),
            ),
            ListTile(
              leading: Icon(Icons.question_answer, color: Colors.grey[850]),
              title: Text("Q&A"),
              //gesture
              onTap: () {
                print('Q&A is clicked');
              },
              trailing: Icon(Icons.add),
            ),
          ],
        ),
      ),
    );
  }
}

โ—พ leading ์ด ์™ผ์ชฝ ๋ฐฐ์น˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋ผ๋ฉด trailing์€ ์˜ค๋ฅธ์ชฝ ๋ฐฐ์น˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค

 

 

 

 

 

728x90