0

In the question "Open a view after a delay", copsOnRoad gives the following result:

import 'dart:async';

// 1. Option with Timer
Timer(Duration(seconds: 5), () {
  // 5s over, navigate to a new page
  Navigator.pushNamed(context, MaterialPageRoute(builder: (_) => Screen2()));
});


// 2. Option -- Using Future.delayed class
Future.delayed(Duration(seconds: 5), () {
  // 5s over, navigate to a new page
  Navigator.pushNamed(context, MaterialPageRoute(builder: (_) => Screen2()));
});

I would like to give the user an option to stop the execution of Navigator.pushNamed during the 5 seconds while waiting. How is that possible?

Uwe.Schneider
  • 1,112
  • 1
  • 15
  • 28

1 Answers1

0

you can define a bool that if it is cancelled or not then push or not like:


import 'dart:async';

bool isCancelled = false;

// 1. Option with Timer
Timer(Duration(seconds: 5), () {
  // 5s over, navigate to a new page
  Navigator.pushNamed(context, MaterialPageRoute(builder: (_) => Screen2()));
});


// 2. Option -- Using Future.delayed class
Future.delayed(Duration(seconds: 5), () {
  if(!isCancelled )
  Navigator.pushNamed(context, MaterialPageRoute(builder: (_) => Screen2()));
});

and a button to change state of isCancelled