'Flutter Dismiss Alert Dialog Navigation Issue
I am currently trying to navigate back to the existing screen from an opened alert dialog box. When I try to do so using this code:
onPressed: () => Navigator.of(context).pop(),
I am diverted back to the opened alert dialog box but I would like it to be closed and not open on return to the original screen. Is there a way of doing this besides using
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
CreatePost()));
This is what I currently am diverted back to: an opened dialog box.
Here is the alert dialog code:
AlertDialog(
contentPadding: EdgeInsets.all(5.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
right: -40.0,
top: -40.0,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(
Icons.close,
color: Colors.white,
),
backgroundColor: Colors.red,
maxRadius: 20.0,
),
),
),
I can use this code to navigate back to the appropriate screen but it doesn't work well with multiple screens using the same logic:
Navigator.of(context) .push(MaterialPageRoute( builder: (context) => CampaignPage1())) .then((result) { Navigator.of(context).pop();
Solution 1:[1]
You can try calling your Alert Dialog like this:
class FancyAlertDialog {
static showFancyAlertDialog(
BuildContext context,
String title,
String message, {
bool dismissable = false,
Icon icon,
String labelPositiveButton,
String labelNegativeButton,
VoidCallback onTapPositiveButton,
VoidCallback onTapNegativeButton,
}) {
return showDialog(
context: context,
barrierDismissible: dismissable,
child: Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(12.0),
),
),
child: Wrap(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12.0),
topRight: Radius.circular(12.0),
),
color: Colors.white,
),
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Stack(
children: <Widget>[
Align(
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: icon ?? Container(height: 0),
),
alignment: Alignment.topRight,
)
],
),
),
Padding(
padding: EdgeInsets.only(
left: 16.0,
top: 2.0,
right: 16.0,
bottom: 8.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(
child: Text(title,
style: khomeStyle.copyWith(
color: Colors.black, fontSize: 16)),
),
SizedBox(height: 8.0),
Text(message,
textAlign: TextAlign.center,
style: khomeStyle.copyWith(
color: Colors.black,
fontSize: 13,
fontWeight: FontWeight.w300)),
SizedBox(height: 16.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
color: Colors.grey,
child: Text(
labelNegativeButton.toUpperCase(),
style: TextStyle(
color: Colors.white,
),
),
onPressed: onTapNegativeButton,
),
),
SizedBox(width: 16.0),
Center(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
color: kOrange,
child: Text(
labelPositiveButton.toUpperCase(),
style: TextStyle(
color: Colors.white,
),
),
onPressed: onTapPositiveButton,
),
),
],
)
],
),
),
],
),
),
);
}
}
When you want to call this Alert Dialog you can call it like this:
FancyAlertDialog.showshowFancyAlertDialog(*Supply your arguments here*)
When you call this you can call the Navigator.pop(context)
function on the call of the alert dialog as a parameter
FancyAlertDialog.showshowFancyAlertDialog(
...
onTapPositiveButton: () {
Navigator.pop(context);
print('tap positive button');
},
)
The text and function for the two buttons can be specified on call.
Solution 2:[2]
use this
Navigator.of(context, rootNavigator: true).pop('dialog')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Simran Aswani |
Solution 2 | Shailandra Rajput |