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

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();
});});

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

Related

Pass value when dialog closed

I'm trying to create an app in Flutter.
When a particular button is pressed a Dialog shows up. In the dialog, the user can write to TextField. I want to use this text in the previous screen when the dialog is closed with pop().
Is there any way to do it?
try this:
showDialog(
context: context,
builder: (context) => Dialog(),
).then((result){
// use the result here
});
and in dialog pop like this:
Navigator.pop(context, result);
You can await to get data from button. also You can pass data .pop(YourValue)
onPressed: () async {
final data = await showDialog(
context: context,
builder: (context) {
final TextEditingController controller =
TextEditingController(); // this can be outside to get direct text from it
return AlertDialog(
content: TextField(
controller: controller,
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(controller.text);
},
child: Text("Close"))
],
);
},
);
if (data != null) {
//your operation
}
print(data);
},

How to stop the loader after 5 seconds

I'm trying to display the loader on whole screen on button click and after 5 seconds displaying a dialogue box an want to stop the loader. here is my code
setState(() {
_isLoading = true;
});
_isLoading
? showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async {
return false;
},
child: Center(
child: CircularProgressIndicator(
color: Apptheme.primaryColor,
),
),
);
})
: null;
await Future.delayed(const Duration(seconds: 5), () {
AppDialogs()
.showInfoDialogue(context, "Sorry all drivers are busy!", () {
Navigator.pop(context);
});
});
setState(() {
_isLoading = false;
});
calling this code on button click, it shows the dialogue box after 5 seconds but not stop the loader. kindly help where i'm doing wrong.
Once you call showDialog the loader is mounted on the screen and it will not disappear, if you have a simple setup calling .pop() on the navigator will remove your loader before you show the dialog. Modification required:
await Future.delayed(const Duration(seconds: 5), () {
Navigator.of(context).pop();
AppDialogs().showInfoDialogue(context, "Sorry all drivers are busy!", () {
Navigator.pop(context);
});
});
You can define thai cde in instate event
Future.delqyed(duration:Duration(seconds:5),(){showdialog((BuildContext context,Widget child)=>Dialog());
});
You can try this

flutter) How can I make the dialog disappear when the back button is pressed?

I used a dialog to prevent the backbutton pressed like following
WillPopScope(
onWillPop: () async => await onBackPressed(context:context, msg: 'back?')
Future<bool> onBackPressed(
{required BuildContext context, required String msg}) async {
bool willLeave = false;
await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('$msg'),
actions: <Widget>[
TextButton(
child: const Text("OK"),
onPressed: () {
willLeave = true;
Navigator.of(context).pop();
},
),
TextButton(
child: const Text("No"),
onPressed: () => Navigator.of(context).pop())
]));
return willLeave;
}
By the way, when the dialog poped up, the dialog does not disappear although the button is pressed.
Does dialog disappear only the button or background screen tapped?
Can't I make it to disappear with the real back button?
Help me, please.. Thank you

How can I guarantee alertDialog to disappear automatically every time in Flutter with the correct context?

How can I improve this alert dialog so that it always displays for 1 second then disappears. Sometimes the navigator pop doesn't work on the correct context and "misses" it when code is layered in other widgets. Is there a way to guarantee the correct context? I need a consistent fix and already tried another thread's answer. barrierDismissible: false, did not fix this.
showAlertDialogTMPCoinsAdded(BuildContext context, String title, String content) {
Timer timer = Timer(Duration(milliseconds: 1000), (){
Navigator.of(context, rootNavigator: true).pop();
});
AlertDialog alert = AlertDialog(
title: Text(title),
content: Image.asset('assets/images/coins.gif'),
);
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
).then((value){
timer.cancel();
});
}

Autoclose dialog in Flutter

I want to autoclose dialog a few seconds after opening. The solution that I found is to call Navigator.of(context).pop(); delayed and it works. But the problem occurs if I closed it manually (by clicking outside) before the execution of the Navigator.pop command. Then Navigator.pop just closes the app and I see just a black screen.
I need a way to destroy this delay on closing the dialog or to find another workaround.
showDialog(
context: context,
builder: (BuildContext builderContext) {
Future.delayed(Duration(seconds: 5), () {
Navigator.of(context).pop();
});
return AlertDialog(
backgroundColor: Colors.red,
title: Text('Title'),
content: SingleChildScrollView(
child: Text('Content'),
),
);
}
);
You can use a Timer to achieve this. You can cancel the timer whenever you want.
Declare a timer property in your class:
Timer _timer;
And change your showDialog code like:
showDialog(
context: context,
builder: (BuildContext builderContext) {
_timer = Timer(Duration(seconds: 5), () {
Navigator.of(context).pop();
});
return AlertDialog(
backgroundColor: Colors.red,
title: Text('Title'),
content: SingleChildScrollView(
child: Text('Content'),
),
);
}
).then((val){
if (_timer.isActive) {
_timer.cancel();
}
});
In this case, you are using the wrong context.
Try to change the context you are using in the "pop"
You have this BuildContext builderContext, use that builderContext like:
Navigator.of(builderContext).pop();
You can use different way of executing pop() request using Timer
_timer = Timer(Duration(seconds: _timerTimeoutInterval), () {
Navigator.of(context).pop();
});
And in case you want to cancel the timer you can call this:
if (_timer != null && _timer.isActive) {
_timer.cancel();
}