Home > Mobile >  logged out in 30 seconds, unless the user select select Continue
logged out in 30 seconds, unless the user select select Continue

Time:03-25

I want to make the user log out in 30 seconds unless the user select Continue

void startTimer() {
    Timer(Duration(seconds: 10), () {
      logOut(); //It will redirect  after 30 seconds
    });
  }

  void logOut() {
    Navigator.of(context).pop();
    Navigator.of(context, rootNavigator: true)
        .pushReplacementNamed(AppRoutes.LOGIN_ROUTE);
  }

CodePudding user response:

create a global variable in widget class Timer? _timer
assign it like

_timer = Timer(Duration(seconds: 10), () {
      logOut(); //It will redirect  after 30 seconds
    });

you can cancel the future like

_didUserTapContinue() {
   _timer?.cancel();
}

CodePudding user response:

Add a flag that changes state when the continue button is pressed;

bool canContinue = false;
void startTimer() {
    Timer(Duration(seconds: 10), () {
    if(!canContinue)  logOut(); //It will redirect  after 30 seconds
    });
  }

  void logOut() {
    Navigator.of(context).pop();
    Navigator.of(context, rootNavigator: true)
        .pushReplacementNamed(AppRoutes.LOGIN_ROUTE);
  }

Then set the value of canContinue to true when continue button is pressed

  • Related