Search code examples
fluttertimerdialog

How to make a timer when screen is inactice show dialogbox?


i try to make a timer which detect if the screen is not using then it will pop up a dialogbox. The popup working fine after 5sec but how can i make it only popup one times instead of every 5sec will popup a new dialog box?

timer = Timer.periodic(Duration(seconds: 5), (time) {
  Navigator.of(context, rootNavigator: true).pop();
  print('Something');
  showDialog(

      context: context,
      builder: (BuildContext context) {
        Future.delayed(Duration(seconds: 5), () {
          Navigator.of(context).pop(true);
        });
        return Dialog(

          child: _imageslideshowProductDetails(),
        );
      }).then((value){
    timer?.cancel();
});});

Solution

  • use

    Timer.run(); // it will only run once and will pop after 5 secs