show dialog when long pressed and pop it when finger up - flutter

I want to show a dialog when user long pressed on an item and pop it when finger up but it can't detect tap up.
I put dialog on another GestureDetector and use onTapUp property of it to pop dialog.
GestureDetector(
child: studentIcon(index, context),
onLongPress: () {
showDialog(
context: context,
builder: (context) {
return GestureDetector(
onTapUp: (detail) {
Navigator.pop(context);
},
child: DialogDetail(
index: index,
),
);
});
},
I expect to pop dialog after finger up after long pressed.

You can't do that as there is an context problem in GestureDetector.
Please follow this answer to implement this thing.

Try to make method to open dialog . i provide alert dialog code..
void _showText(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
content: Text(
"User name :${nameEditText.text} \nPassword : ${passwordEditText.text}"),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: new Text("OK"))
],
);
});
}
}
after that called on button click..
child: RaisedButton(
padding: EdgeInsets.all(15.0),
onPressed: () {
_showText(context);
},
child: Text(
"Submit",
style: TextStyle(fontSize: 15, color: Colors.white),
),
color: Colors.blue,
),

Related

Flutter showDialog is not shown on PopupMenuItem tap

I'm using PopupMenuButton in my application. I want to showDialog on tapping a PopupMenuItem.
My PopupMenuItem:
PopupMenuItem(
child: Text("Show dialog"),
onTap: () {
showDialog(
context: context,
barrierColor: Colors.black26,
builder: (context) => AlertDialog(
...
)
},
),
Also using onSelected inside the PopupMenuButton didn't work.
Thats because onTap of popupMenuItem tries to use Navigator.pop to close the popup but at same time you are trying to show the dialog, So it closes the dialog and leaves the popup so, you can wait till the all the animations or ongoing things complete then show dialog
code: dartPad code
PopupMenuItem(
child: const Text('Item 0'),
onTap: () {
WidgetsBinding?.instance?.addPostFrameCallback((_) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: const Icon(CupertinoIcons.info_circle),
content: const Text(
'Hello User, Welcome',
textAlign: TextAlign.center,
),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => Navigator.pop(context),
child: const Text('Thanks'),
),
],
);
});
});
}),
Try this one:
PopupMenuItem(
child: Text("Show dialog"),
onTap: () {
Future<void>.delayed(
const Duration(), // OR const Duration(milliseconds: 500),
() => showDialog(
context: context,
barrierColor: Colors.black26,
builder: (context) => AlertDialog(...),
),
),
},
);

Flutter show dialog show only if 2 showDialog() is called in one function

I have a showDialog() function in flutter web, but it will only works this way (2 show dialog in one function), if I comment out the other one, the dialog will not show. I don't really understand why I need to put 2 showDialog() in order for it to show up. Here is the code:
onDeleteTap(String id) async {
print(id);
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Hapus?'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
],
),
),
actions: <Widget>[
TextButton(
child: Text('Batal'),
onPressed: () {
},
),
SizedBox(
width: 150.0,
child: ErrorButton(
text: "Hapus",
onClick: () {
},
),
),
],
);
},
);
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Hapus?'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
],
),
),
actions: <Widget>[
TextButton(
child: Text('Batal'),
onPressed: () {
},
),
SizedBox(
width: 150.0,
child: ErrorButton(
text: "Hapus",
onClick: () {
},
),
),
],
);
},
);
I think before you are calling onDeleteTap you must be using navigator.pop(context). You can check by not showing any dialog to check if you are really poping a screen (If you are having a pop your current screen will close or you will have a black screen) or you can use the debbuger to check all the lines that passes before getting to this code.

how to update parent widget after showDialog is closed in Flutter?

I have a widget with an icon when I click on it a dialog widget is shown, here is the call for the dialog :
// this icon is in widget parent
IconButton(
icon: Icon(
Icons.info_outline,
size: mobileWidth * 0.07,
),
tooltip: 'information',
color: Colors.blueGrey,
onPressed: () {
showAlertInfo(context, code);
setState(() {});
},
),
here is my dialog :
showAlertInfo(BuildContext context, String code) {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(
"Information sur le client $code", ),
content: SingleChildScrollView(
child: Container( .....
/* ...
this dialog has some operations that changes info
values of the widget that called this in first place
it is a big code to put here*/
// here I have a close button
actions: [
FlatButton(
child: Text(
"Fermer",
style: TextStyle(
color: Colors.red,
fontSize: mobileWidth * 0.035,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
Navigator.of(context).pop(); // dismiss dialog
},
),
],
);
what I'm trying to achieve is when the dialog is dismissed I want the parent widget to be updated, so how I call setState of the parent widget when the dialog widget is closed ?
For Async function
Add await before showDialog() and call setState((){}) on the next line.
await showDialog(
context: context,
builder: (context) {
return yourWidget;
}
);
setState((){});
For Sync Function
Use the .then() callback, and call setState((){}) in it.
showDialog(
context: context,
builder(context) {
return yourWidget;
}).then((_){
setState((){});
}
);

AlertDialog not closing Flutter

my button code is as below and previously it closed the AlertDialog + Signed Out of Google when clicking on the continue button, but now it only signs out, the AlertDialog is still there... Anyone knows whats going on?
showAlertDialog(BuildContext context) {
// set up the buttons
Widget cancelButton = FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
);
Widget continueButton = FlatButton(
child: Text("Confirm Sign Out"),
onPressed: () {
signOutGoogle();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return LoginPage();
}), ModalRoute.withName('/'));
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Confirmation"),
content: Text("Are you sure you want to Sign Out?"),
actions: [
cancelButton,
continueButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
showAlertDialog invoked here :
RaisedButton(
onPressed: () {
showAlertDialog(context);
},
color: Colors.deepPurple,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Sign Out',
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)),
)
Add Navigator.of(context).pop(); after signOutGoogle(); to close the dialog.
showAlertDialog(BuildContext context) {
// set up the buttons
Widget cancelButton = FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
);
Widget continueButton = FlatButton(
child: Text("Confirm Sign Out"),
onPressed: () {
signOutGoogle();
Navigator.of(context).pop(); // Pop the dialog
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return LoginPage();
}), ModalRoute.withName('/'));
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Confirmation"),
content: Text("Are you sure you want to Sign Out?"),
actions: [
cancelButton,
continueButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

How to dismiss alert dialog when clicked outside of it

I have an alert dialog that I want to be dismissed when clicked outside of it, I know this is the default behavior of alert dialogs in flutter, but I don't know what's the problem that's preventing it from closing when clicked outside.
I tried to use barrierDismissable to true, but still, it doens't work.
This is my dialog :
termsAndConditionsDialog(BuildContext context) {
AlertDialog alert = AlertDialog(
title: Text("Terms and Conditions", style: TextStyle(fontSize: 18, color: AppColors.accentColor),),
content: Text(
generalSettings.policyForCustomer,
style: TextStyle(fontSize: 16),
),
);
// show the dialog
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
and this is how I call it from button's onPressed :
termsAndConditionsDialog(context);
call this upon onPressed or onTap:
void showMessage(){
showDialog(
context:context,
builder: (Buildcontext context){
return AlertDialog(
backgroundColor: Colors.transparent,
content: Container(
width: MediaQuery.of(context).size.width,
height: 100,
alignment: AlignmentDirectional.center
child: Text("TRIAL",style: TextStyle(color: Colors.white))
)
)
}
)
}
Custom Alert method
typedef ResponseCallbackValueLess = void Function();
showAlertDialogWithOkButton(
BuildContext context,
String message,
String heading,
String buttonAcceptTitle,
ResponseCallbackValueLess callback) {
Widget continueButton = FlatButton(
child: Text(buttonAcceptTitle),
onPressed: () {
callback();
},
);
called like below:
showAlertDialogWithOkButton(context,
'No items found in your cart.',
"app name", "Ok", dismissAlert(context));
dismissAlert(context){
Navigator.pop(context);
}