[ Flutter ] Snackbar & Toast

from.kΒ·2023λ…„ 11μ›” 29일
0

Mobile-Software

λͺ©λ‘ 보기
13/15
post-thumbnail

🍬 μŠ€λ‚΅λ°”

ν™”λ©΄ ν•˜λ‹¨λΆ€μ— 메세지창을 λ„μš°λŠ” 것

πŸ’β€β™€οΈ ν† μŠ€νŠΈμ°½κ³Ό 차이점은?
μŠ€λ‚΅λ°”λŠ” μœ„μΉ˜κ°€ μ›€μ§μ΄μ§€λ§Œ,
ν† μŠ€νŠΈμ°½μ˜ 경우 μœ„μΉ˜κ°€ ν•˜λ‹¨λΆ€μ— κ³ μ •



κΈ°μ‘΄ (#18κ°•μ˜)

Scanffold.of(context)
-> ν˜„μž¬μ˜ BuildContext μ—μ„œ μœ„λ‘œ 거슬러 μ˜¬λΌκ°€ κ°€μž₯ κ°€κΉŒμš΄ Scanffold μœ„μ ―μ„ λ°˜ν™˜ν•˜λΌλŠ” 것


이 κ³Όμ •μ—μ„œ Scanffold.of(context) κ°€ κ°–κ³ μžˆλŠ” ν˜„μž¬μ˜ Context λ‘œλŠ” Scanffold λ₯Ό 찾을 수 μ—†μŒ
-> 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ Builder Widget 을 생성해 μƒˆλ‘œμš΄ Context λ₯Ό 전달받아 κ·Έ μœ„μΉ˜μ—μ„œλΆ€ν„° Widget Tree 상 κ·Έ Builder μœ„μ ― μœ„μ— μœ„μΉ˜ν•˜κ³  μžˆλŠ” Scanffold μœ„μ ―μ„ μ°Ύμ•„λ‚˜κ°€κ³ μžν•¨


  • 주문확인 νŽ˜μ΄μ§€μ—μ„œ κ΅¬ν˜„λœ μŠ€λ‚΅λ°”λŠ” ν˜„μž¬ 주문확인 νŽ˜μ΄μ§€ λ‚΄μ˜ Build Context - Scanffold μƒμ—μ„œλ§Œ κ΅¬ν˜„λ  수 있기 λ•Œλ¬Έμ— λ‹€λ₯Έ λΌμš°ν„°(즉 νŽ˜μ΄μ§€)둜 μ΄λ™ν•˜λ©΄ μƒˆλ‘œμš΄ Buld Context 의 Scanffold 둜 λ°”κ»΄ μŠ€λ‚΅λ°”λŠ” κ΅¬ν˜„ν•  수 μ—†κ²Œλ¨
    => 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ -> μœ„μ ― 트리 μ΅œμƒμ˜ μœ„μΉ˜μ—μ„œ μ—¬κΈ°μ €κΈ° 흩어져 μžˆλŠ” μ—¬λŸ¬ μžμ† Scanffold 듀을 등둝해 μ–Έμ œλΌλ„ μŠ€λ‚΅λ°”λ₯Ό μˆ˜μ‹ ν•  수 μžˆλ„λ‘ λ¬Άμ–΄ 관리


  • ScaffoldMessenger.of(context) -> κ°€μž₯ κ°€κΉŒμš΄ ScaffoldMessenger 을 μ°Ύμ•„ λ°˜ν™˜
  • ScaffoldMessenger -> λͺ¨λ“  μžμ† Scaffold 듀이 λ“±λ‘λ˜μ–΄ μžˆμ–΄ κ°€μž₯ κ°€κΉŒμš΄ Scaffold λ₯Ό μ°Ύμ•„ λ°˜ν™˜ν•΄μ€Œ



import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyPage(),
    );
  }
}

class MyPage extends StatelessWidget {
  const MyPage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Scanffold Messenger - Main'),
      ),
      body: HomeBody(),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.thumb_up),
        onPressed: () {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Like ad new Snack bar!'),
              duration: Duration(seconds: 5),
              action: SnackBarAction(
                label: 'Undo',
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => ThirdPage()));
                },
              ),
            ),
          );
        },
      ),
    );
  }
}

class HomeBody extends StatelessWidget {
  const HomeBody({super.key});

  
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: Text('GO to the secod page'),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SecondPage()),
          );
        },
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Center(
        child: Text(
          ' 2. μ’‹μ•„μš”!',
          style: TextStyle(
            fontSize: 20.0,
            color: Colors.redAccent,
          ),
        ),
      ),
    );
  }
}

class ThirdPage extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Third Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '3. μ’‹μ•„μš”λ₯Ό μ·¨μ†Œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?',
              style: TextStyle(
                fontSize: 20.0,
                color: Colors.redAccent,
              ),
            ),
            ElevatedButton(
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text('μ’‹μ•„μš”κ°€ μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.'),
                    duration: Duration(seconds: 3),
                  ),
                );
              },
              child: Text('μ·¨μ†Œ'),
            )
          ],
        ),
      ),
    );
  }
}



μ–΄λŠ ν•œ νŽ˜μ΄μ§€μ—μ„œλ§Œ μŠ€λ‚΅λ°”λ₯Ό 보여주고 λ‹€λ₯Έ ν™”λ©΄μœΌλ‘œ λ„˜μ–΄κ°ˆμ‹œ μŠ€λ‚΅λ°”λ₯Ό μ—†μ• λ €λ©΄?

class ThirdPage extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return ScaffoldMessenger(
      child: Scaffold(
        appBar: AppBar(
          title: Text('Third Page'),
        ),
        body: Builder(
          builder: (context) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '3. μ’‹μ•„μš”λ₯Ό μ·¨μ†Œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?',
                    style: TextStyle(
                      fontSize: 20.0,
                      color: Colors.redAccent,
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: Text('μ’‹μ•„μš”κ°€ μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.'),
                          duration: Duration(seconds: 3),
                        ),
                      );
                    },
                    child: Text('μ·¨μ†Œ'),
                  )
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}



μŠ€λ‚΅λ°” μŠ€νƒ€μΌλ§

ElevatedButton(
                    onPressed: () {
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: Text(
                            'μ’‹μ•„μš”κ°€ μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Colors.white,
                            ),
                          ),
                          backgroundColor: Colors.teal,
                          duration: Duration(seconds: 3),
                        ),
                      );
                    },
                    child: Text('μ·¨μ†Œ'),
                  ),





🍞 ν† μŠ€νŠΈμ°½

⭐ μ•„λž˜μ™€ 같이 μ½”λ“œ μž‘μ„± 및 import


ν”ŒλŸ¬ν„° ν† μŠ€νŠΈμ°½ 이슈 => 버전 였λ₯˜
ν† μŠ€νŠΈμ°½ 버전 였λ₯˜ ν•΄κ²°


ElevatedButton(
            onPressed: () {
              showToast();
            },
            child: Text('Toast'),
            style: ElevatedButton.styleFrom(
              primary: Colors.orange,
            ),
          ),
          

void showToast() {
  Fluttertoast.showToast(
    msg: 'This is Toast!!',
    gravity: ToastGravity.BOTTOM,
    toastLength: Toast.LENGTH_SHORT,
  );
}






πŸ“š μ½”λ”©μ…°ν”„ < ν”ŒλŸ¬ν„°(flutter) μˆœν•œλ§› κ°•μ’Œ >

0개의 λŒ“κΈ€