How to close Flutter dialog from outside - flutter

This is how we close a dialog from INSIDE
showSomeDialog() {
return showDialog(
context: context,
builder: (BuildContext contextPopup) {
return AlertDialog(
content: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(contextPopup).pop();
},
child: Text('Close me inside'),
),
),);
}
);
}
How to close this dialog from OUTSIDE ? In other words, how does the stateful widget creating this dialog can access the variable contextPopup ? The problem with Navigator.of(context).pop() is that it will close the topmost widget on the Navigation stack, not this specific dialog.
In Android, to close a dialog programmatically from outside, you just do:
dialog.dismiss();

Related

How to change state to previous state in bloc?

I have sigIn function that get data from Api and move to another screen if request is successful and if request is not successful then show alertDialog
I change state and before fetching data I show CircularProgressIndicator to make user know that data is fetching.
But when alertDialog window pops up and I close it then CircularProgressIndicator doesn't disappear. How to remove WaitingSignInAuth and show me Scaffold with inputs
When error comes then I emit ErrorSignInAuth but why there is WaitingSignInAuth to.. Why ErrorSignInAuth doesn't replace WaitingSignInAuth or it should work differently?
This is the code:
#override
Widget build(BuildContext context) {
return BlocConsumer<AuthCubit, AuthState>(
listener: (context, state) {
if (state is WaitingSignInAuth) {
showDialog(
context: context,
builder: (context) => Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.black.withOpacity(0.6),
child: const Center(
child: CircularProgressIndicator(
strokeWidth: 1,
color: Colors.black,
backgroundColor: Colors.white,
),
),
));
}
if (state is ErrorSignInAuth) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Bad request"),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text("Error")
],
),
),
actions: <Widget>[
TextButton(
child: const Text("Close"),
onPressed: () {
Navigator.pop(context); // If i press it then alertDialog diappears but not the loading circle
},
)
],
);
}
);
}
},
);
}
This is how it looks like:
The issue is, that you showDialog() twice. First is shown when WaitingSignInAuth state is fired, and the second one when you receive ErrorSignInAuth.
So Navigator.pop(context); in the "error" alert dialog closes only the showDialog that you've triggered in if (state is ErrorSignInAuth), the second dialog, which contains progress indicator is still visible.
To easy fix it, you can change your code to:
TextButton(
child: const Text("Close"),
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
)
But in my opinion thats not the best fix to your issue. IMO you don't need to show ProgressIndicator for WaitingSignInAuth in the dialog. Adjust your UI in the way, that the container with progress indicator will be displayed over it, but this doesn't need to be a dialog.

Dismiss or pop alert dialog on press of a fire stick remote button

I'm building an app for fire tv/fire stick using flutter.
My requirement: Show a dialog on press of a button in the fire TV remote and hide the dialog when the user presses the back button on the remote.
What I have accomplished: I'm able to show the dialog on the press of a button on the remote.
I need help in dismissing the dialog when the user presses back button on the remote.
What is happening now: When I press back while the dialog is shown, the entire page gets rerouted to the previous page.
I'm guessing it has something to do with the context, not sure how to fix this issue.
_showInfoModal(context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.transparent,
content: Container(
child: Center(
child: Text(widget.message, style: TextStyle(color: Colors.white)),
),
)
);
},
);
}
This is the code I'm using to show the dialog.
Navigator.of(context, rootNavigator: true).pop();
This is how I'm trying to pop the alert dialog.
Can someone please help me out here? TIA.
Try below code hope its works because I also build apk for TV and use below Alert Code
showAlert(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Top '),
content: Text('Bottom '),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Ok'),
),
],
);
},
);
}
So I was able to accomplish this using a workaround.
Having a boolean variable and showing either the dialog or a container with 0 height.
Not a good approach but gets the work done.

Is it possible to make alert dialog dissapear after page navigation?

The "OK" button on my alert dialog navigates to a new page. But when I go back to the previous page, the alert is still there. Is there any way for the alert to disappear after I navigate to the new page?
// Alert Dialog
Future<void> _handlePhoto(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text('Please Position Crosshair'),
content: const Text(
'Before detecting cancer, ensure your focus area is centered.'),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pushNamed(
'/camerapage',
arguments: cameras,
);
},
),
],
);
},
);
}
Before pushing the new page, you need to pop the dialog like this. So when you come back it is not there.
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed(
'/camerapage',
arguments: cameras,
);
}

How to dismiss flutter Get library dialog?

I am showing a dialogue with Get library and I want to dismiss automatically after a few seconds with Future. But I found no suitable function for that. So how do I dismiss a get dialogue after showing it?
code:
Get.dialog(Center(
child: Material(
color: Colors.blue,
child: Text('Hello'),
),
)); // how to dismiss? Like Get.Dismiss().
for dismiss dialog try using :
Get.back();
or
navigator.pop();
I have not used Get, but if you really want to do that thing, that I can suggest my way of doing it.
So, we will be using Flutter AlerDialog Class, which works the same, that is popping up the dialog, and you can always edit the content.
Now let us do these things:
Creating a dialog
Pop Up when the button is clicked
Auto dismiss the dialog
This will help you organize your solution. And we will be using Future only.
showAlertDialog(BuildContext context) {
// set up the button
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () { },
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("My title"),
content: Text("This is my message."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
This method will pop-up the dialog, and then autodismiss, the dialog:
void _ourAutoDismissDialog(BuildContext context){
//Calling out showdialog method
showAlertDialog(context);
//Auto dismissing after the 2 seconds
// You can set the time as per your requirements in Duration
// This will dismiss the dialog automatically after the time you
// have mentioned
Future.delayed(const Duration(seconds: 2), (){
Navigator.of(context).pop();
});
}
FlatButton(
onPressed: () => _ourAutoDismissDialog(context)
child: Text('Hello')
)
To dismiss the dialog, we need to do back operation, and we do it via Navigator.of(context).pop()
This is the result we get after implementing the above:

How to pass context to a child widget in flutter

I have a statefull widget W1 which calls a stateless widget W2.
W2 has onTap functionality. I want to show an alert dialog in W2's onTap().
onTap:() {Alert(context: context, title:'Hi');},
I dont get any error, but no alert is shown on tap. I tried passing context as a parameter to W2 but I still dont see any dialog box.
What is the right way to show a dialog box from W2?
I am using rflutter_alert package Link
Thanks
You have to wrap your Alert(context: context, title:'Hi'); with showDialog(context: context, builder: (BuildContext context) => Alert(context: context, title:'Hi'));
Here is the cookbook sample:
Future<void> _neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Anyway, for your question about how to pass context, If you are creating a Stateless or Stateful widget you dont need to pass the context, you can get it from build(BuildContext context) {}.
Adding .show() in end solved it.
onTap:() {Alert(context: context, title:'Hi').show();}
Its clearly documented in rflutter_alert package, but I somehow missed it.
Pass the context in the function call in onTap:
onTap:(context) {Alert(context: context, title:'Hi');},