본문 바로가기

Dead Code/Flutter_Dart

[플러터] package : fluttertoast

생각보다 처리하기 귀찮은 toast sample을 미리 작성해둔다.

 

 

 

Toast 변수 선언, initState에서 초기화

초기화를 해주고, 현재 context를 연결해준다.

 

late FToast fToast;

 

  void initState() {
    super.initState();
    fToast = FToast();
    fToast.init(context);
  }

 

 

toast 디자인 method

구지 이럴필요가 있을까 싶지만, toast custom 디자인을 만들어 둘 수 있는게 더 낫겠지?

 

_showToast() {
    Widget toast = Container(
      padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.0),
        color: ThemeData.dark().primaryColor.withAlpha(80),
      ),
      child: const Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.check,
            color: Colors.white,
          ),
          SizedBox(
            width: 12.0,
          ),
          Text(
            "This is a Custom Toast",
            style: TextStyle(color: Colors.white),
          ),
        ],
      ),
    );
    fToast.showToast(
      child: toast,
      gravity: ToastGravity.BOTTOM,
      toastDuration: const Duration(seconds: 2),
    );
  }

 

 

버튼 function으로 구현

버튼에서는 위에 만들어둔 _showToast() 만 호출하면 된다.

 

ElevatedButton(
  onPressed: () {
    _showToast();
  },
  child: const Text('show toast'),
),

 

 

끝.