Unable to Display CupertinoDialog in Flutter - flutter

I have the following code to display Cupertino AlertDialog but I keep getting an error:
showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: Text("Attention"),
content: Text(this),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
);
Here is the error I keep getting
Unhandled Exception: No MaterialLocalizations found.
I thought CupertinoAlertDialog has nothing to do with MaterialApp, so why do I keep getting this error.
The parent app here is a CupertinoApp.
How do I fix this?

ShowDialog displays a Material dialog above the current contents of the app, with Material entrance and exit animations, modal barrier color, and modal barrier behavior (dialog is dismissible with a tap on the barrier).
So, Basically to show a Cupertino dialog you can use
showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(),
);

Related

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.

How to handle back button of browser in Flutter Web

this question had already asked in this link How to configure go back button in Browser for Flutter Web App
and I implement onWillPop, so when user is in A screen and then click the button See Detail I will navigate them to B screen and then.. when user is in B screen and click the back button of browser I will navigate them back to A screen... the first trial is success but when I do it second time I got an error like this
Error: Assertion failed: org-dartlang-sdk:///flutter_web_sdk/lib/_engine/engine/navigation/history.dart:288:14
_userProvidedRouteName != null
is not true
in my first screen (A Screen), I have a button like this:
FlatButton(
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext ctx) =>
Detail(
value:x[i])));
},
child: Text("See Detail"),
textColor: Colors.white,
color: Colors.blueAccent,
)
and for the Detail Screen (B Screen) I use onWillPop like this
WillPopScope(
onWillPop: () {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext ctx) => FirstScreen()));
return Future.value(true);
},
child: Scaffold(
key: _globalKey,
....
is there something that I should add more?
Call Navigator.pop(context); inside the onWillPop of screen B.
Make sure when you navigate from Screen A to Screen B, you should not use Navigator.pushReplacement as it removes ScreenA from the screen stack(not sure if this is the right term)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScreenB()),
);
Here is an example by flutter.
https://flutter.dev/docs/cookbook/navigation/navigation-basics

How to close Flutter dialog from outside

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

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');},

How to create custom Popup menu in flutter

Hi i am newbie into flutter, how can i achieve custom PopUpMenu as shown in below screenshot.
ScreenShot
Any help would be appreciated, Thanks..
You should use the method showDialog to display an AlertDialog Widget.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("PopUp title"),
content: Text("PopUp content text"),
actions: <Widget>[
FlatButton(child: Text("Close"), onPressed: () => Navigator.pop(context))
],
);
});