How to create custom Popup menu in flutter - 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))
],
);
});

Related

Unable to Display CupertinoDialog in 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(),
);

How to?: Flutter RaisedButton onPressed opens a Form

Flutter:
I would like to open a Form after a RaisedButton is pressed. How do I have to compose the onPressed function of the RaisedButton to achieve this?
Thanks for your support.
As you have not mentioned more detail, i hope you need something to show or input. You can use showBottomSheet to fulfill this like this :
RaisedButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext bc) {
return Container(
child: Text('Implement your form here')
);
});
},
child: Text('BottomSheet Demo')
)

Make flutter web Image widgets clickable

How can I download an image from flutter web?
The image is not clickable/dragable and shows no context menu on right click, for example to "save as".
Web implementation - http://watools.xyz/ankush_apis/flutter_projects/youtube/#/
Is there an alternative Image widget, to make it web clickable?
Like Text and SelectableText.
Maybe an Property, a pub package?
Any answers appreciated.
Did you try to use the InkWell widget? It supports gesture and already implements the clickable effect.
InkWell(
child: Image.network(
'https://i.picsum.photos/id/9/250/250.jpg?hmac=tqDH5wEWHDN76mBIWEPzg1in6egMl49qZeguSaH9_VI'),
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Options'),
actions: [
TextButton(
child: Text('Save image'),
onPressed: () {
//Code for download the image
},
),
],
),
);
},
),

Call SnackBar from within an AlertDialog

When a user clicks on delete for an employee, an AlertDialog shall pop up to warn the user.
If the user confirms the deletion, then the AlertDialog disappears and at the bottom of the Scaffold a SnackBar should appear with an Undo function.
Problem:
When I implement the SnackBar method showSnackBar(context, index, employee) within the AlertDialog class I get the following error:
he following assertion was thrown while handling a gesture:
Scaffold.of() called with a context that does not contain a Scaffold.
showDeleteDialog(BuildContext context, Employee employee, int index) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title:
Text('Are you sure you want to delete: ${employee.name} ?'),
actions: <Widget>[
Row(
children: <Widget>[
FlatButton(
child: Text('Yes'),
onPressed: () {
DatabaseProvider.db.deleteEmployee(employee.id).then(
(_) => BlocProvider.of<EmployeeBloc>(context)
.add(DeleteEmployee(index)));
Navigator.pop(context,employee);
showSnackBar(context, index, employee);
}),
FlatButton(
child: Text('No!'),
onPressed: () => Navigator.pop(context)),
],
)
],
));
}
Instead, I thought I could return an employee from the showDeleteDialog when I confirm the deletion. When the result is not null, then I should show the SnackBar. I tried to implement this with Future/Async but with no success.
onPressed: () async {
Employee deletedEmployee = await showDeleteDialog(context, employee, index);
await showSnackBar(context, index, deletedEmployee);
},
Edit: I would like to avoid using GlobalKey if possible, since I read it is not good for the App's performance.
As the error says Scaffold.of() called with a context that does not contain a Scaffold., that means the current context that you are passing to showSnackBar() method doesn't contain a Scaffold in the immediate parent.
We can fix this by using GlobalKey and assign it to the Scaffold. Declare a global key in your stateful widget and pass this as a key in your Scaffold, as below:
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
....
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(widget.title),
),
I call a method _showSnackBar() after navigator.pop() on OK button click inside the alertDialog, as below:
return AlertDialog(
title: Text('Not in stock'),
content: const Text('This item is no longer available'),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
_showSnackBar();
},
),
],
);
Then in the _showSnackBar() method, use the key to show the snackbar, as below:
void _showSnackBar() {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text('Snackbar is displayed'),
));
}
With this approach, once you tap on OK button on alertDialog, the dialog will close and you'll see the snackbar. You may need to customize this per your code as you shared above.
Hope this answers your question and resolves your issue.
Found the solution and it is super easy...
I only had to re-name one of the context to a dialogContext
showDeleteDialog(BuildContext context, Employee employee, int index) {
showDialog(
context: context,
builder: (dialogContext) => AlertDialog(
title:
Text('Are you sure you want to delete: ${employee.name} ?'),
actions: <Widget>[
Row(
children: <Widget>[
FlatButton(
child: Text('Yes'),
onPressed: () {
DatabaseProvider.db.deleteEmployee(employee.id).then(
(_) => BlocProvider.of<EmployeeBloc>(dialogContext)
.add(DeleteEmployee(index)));
Navigator.pop(dialogContext);
showSnackBar(context, index, employee);
}),
FlatButton(
child: Text('No!'),
onPressed: () => Navigator.pop(context)),
],
)
],
));
}

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