How to make Alert Dialog disappear after 30 seconds in Flutter? - flutter

So I am using an Alert Dialog in my project which should either be closed if the user clicks 'Ok' or 'Cancel' else it should disappear on its own after 30 seconds if the user gives no response. I was thinking of using setState and Duration or something like that but can't exactly figure out.

You can use Future.delayed to have a delayed action:
// dialog builder
showDialog(context, builder: (BuildContext context) {
bool manuallyClosed = false;
Future.delayed(Duration(seconds: 30)).then((_) {
if (!manuallyClosed) {
Navigator.of(context).pop());
}
});
// Build the dialog window
// Set manuallyClosed to true on the OK or Cancel button tap
});

Related

How can I change or override android/ios back button's action in flutter

I need to make a change in my page (changing a bool in setState etc...) when the user presses the back button.
I looked it up and I know people use WillPopScope to override the default "back" action, but in all of the examples they were just popping the page, and I need to do a custom function instead of that, and have no Idea how to do so.
my function looks like this, and I need to run in when the user pressed the back button:
Future<void> backToCats() async{
setState(() {
isLoaded=false;
});
if(isFromSearch){
loadCategories();
}
setState(() {
showingBrands=false;
isLoaded=true;
});
}
You can perform custom logic in the onWillPop argument of a WillPopScope widget. Just make sure to return true or false at the end to indicate whether the page is allowed to pop. WillPopScope will activate for the back button, and other actions that automatically navigate back such as swiping from the side of the screen on iOS.
For example, if you need to set a bool to false in your stateful widget, it would look something like this
WillPopScope(
onWillPop: () async {
setState(() => myBool = false);
return true;
}
child: ...
);

Assign keyboard commands like mute, volume up, volume down, brightness down in Flutter

I have an arduino setup and flutter code. I have 1 button in Arduino and when the button is pressed, the page transition code I wrote in flutter, for example, becomes active and switches to the other page. This area I want is to type commands such as mute, turn on brightness, volume down on the keyboard. How can I use Keyboard Listener
final reader = SerialPortReader(_serialPort!);
reader.stream.listen((data) {
debugPrint('received: $data');
receiveDataList.add(data);
if (data.contains(51)) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FA(),
));
} else if (data.contains(49)) {
print('button1');
}
setState(() {});
}, onError: (error) {
if (error is SerialPortError) {
debugPrint(
'error: ${cp949.decodeString(error.message)}, code: ${error.errorCode}');
}
});
As seen in the code, I wrote a code like go to the relevant page when the value 51 comes. Since Button1 has the value 51, it goes to the relevant page when pressed. What I want is to assign a keyboard method there.

Is there any method that I can open an alert dialog with list of choices in flutter based on an if condition?

So basically I have a countdown timer for a minute and when the minute finishes I want an alert dialog to pop on the screen for the user e.g:
if (isCompleted) {
//open the dialog widget here
}
I can't find solutions for that. I'm only finding solutions that opens the dialog on button press.
Can anyone help?
I used this in a project of mine to display an info dialog when opening the app for the first time:
Future.delayed(Duration(milliseconds: 500), () {
showDialog(
context: context,
builder: (context) => AlertDialog(
//etc
)
You can probably wrap your widgets in a FutureBuilder (so you have access to context) with your countdown as the future argument, and, when the countdown is over, you can call showDialog from there.

Flutter - Alert Dialog showing multiple times inside listener

I am using a flutter_local_notifications to get a notification when a countdown timer has finished.
When i click the notification using onSelectNotification (callback of flutter_local_notification), I add a Subject (RxDart).
var selectNotificationSubject = PublishSubject<String>();
onSelectNotification: (String payload) async {
selectNotificationSubject.add(payload);
},
So the Subject is added successfully on the Stream.
I listen for the stream in initState. The AlertDialog is shown, but the problem is that it is shown multiple times for all the list with countdown timers.
I want to show the AlertDialog only for the timer that i clicked on the notification, not for all the timers that are available on the list.
A way to fix this may be to add to the AlertDialog widget a id so i can show it only once by the id of the countdown timer that is clicked on notification. Widget showDialogById()
subs = selectNotificationSubject.stream.listen((event) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Repeat Timer ${timer.id}"),
content: Text(
"Do you want to repeat your timer ${timer.description} ?"),
actions: [
RaisedButton(
child: Text("Repeat"),
onPressed: () async {
//Repeat timer logic here
});
),
});
});
The way you coded it, it is expected to spam repeatedly to show the dialog.
If you don't want a new trigger dialog show, create a boolean to control it, whatever the bool is true it won't show, otherwise it will show

How to hide alertDialog after one second with Navigator.of(context).pop()

I am trying to auto-hide an alertDialog after one second
This is the code:
Widget popupWidget(BuildContext context, ...) {
Future.delayed(Duration(seconds: 1), () {
Navigator.of(context).pop();
});
return AlertDialog(...);}
What works:
I can click somewhere else on the screen to close the alertDialog
I can wait for one second and it closes automatically
The bug:
If, after (exactly?) one second, I click somewhere else on the screen (which closes the alertDialog), the Future.delayed(...) will not hide the alertDialog, but the whole screen
I unsuccessfully tried making showDialog async, also tried the line
Navigator.of(context, rootNavigator: true).pop();
I think I've found a workaround with:
bool popupIsActive = true;
Future.delayed(Duration(seconds: 1), () {
if (popupIsActive) Navigator.of(context).pop();
});
and
showDialog(...).then((_) {
popupIsActive = false;
});
You might want to ignore any taps outside the dialog in order to prevent it from dismissing and just wait for the future to complete. You can set barrierDismissible to false in showDialog().