[flutter] showSnackBar (밑의 바텀 알림)

Lim-HN·2023년 9월 4일
0

flutter

목록 보기
2/3
post-thumbnail

showSnackBar
코드 복사할 때, 밑에 '복사되었습니다' 구현하기 위함

  1. 버튼 클릭 부분

    // 위의 코드 
    TextButton(
            onPressed: () => {
              showSnackBar(context),
            },
            child: Column(
              children: [
                const Text(
                  '코드 복사',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xFFA9A9B0),
                    fontSize: 14,
                    height: 1,
                  ),
                ),
                Container(
                  width: 80,
                  height: 1,
                  decoration: const BoxDecoration(color: Color(0xFFD2D2D2)),
                ),
              ],
            ),
          ),
    // 아래 코드
  1. showSnackBar 함수

  showSnackBar(BuildContext context) {
    final snackBar = SnackBar(
      content:  Text(
        '코드 복사 완!',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16,
          height: 1,
        ),
      ),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(10))),	//	둥글게
      backgroundColor: const Color(0xff9E9E9E),
      behavior: SnackBarBehavior.floating,	//	아래 플로팅 띄우기
      duration: const Duration(seconds: 2),	
      action:
      SnackBarAction(label: '', textColor: Colors.white, onPressed: () {}),
    );

    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

추가

  • 로딩표시
    // 로딩표시
    CircularProgressIndicator(),

에러

  • 'showSnackBar' isn't defined

    • 에러내용
      Error: The method 'showSnackBar' isn't defined for the class 'ScaffoldState'.
      Try correcting the name to the name of an existing method, or defining a method named 'showSnackBar'.
    • 에러부분
      Scaffold.of(context).showSnackBar(snackBar);
    • 수정 후
       ScaffoldMessenger.of(context).showSnackBar(snackBar);
    • 에러 내용
      . 플러터 버전에는 위와 같이 써야한다고 함

https://api.flutter.dev/flutter/material/ScaffoldMessengerState/showSnackBar.html

https://geojun.tistory.com/57

0개의 댓글

관련 채용 정보