본문 바로가기
프로그래밍/Flutter

[Flutter] BackPress to exit

by YuminK 2022. 4. 16.
int iPrevClickTime = 0;
void updatePrevClickTime() => iPrevClickTime = DateTime.now().millisecondsSinceEpoch;
bool clickedToExit() {
  int iClickTime = DateTime.now().millisecondsSinceEpoch;
  if(iClickTime - iPrevClickTime < 2000) {
    return true;
  }
  return false;
}

// wiget에서
WillPopScope(
   onWillPop: () async {
     if (clickedToExit()) {
       return true;
     }
     else {
       Fluttertoast.showToast('Press one more to exit');
       updatePrevClickTime();
     }
     return false;
   }

 

댓글