Skip to content

Instantly share code, notes, and snippets.

@wilburx9
Last active February 18, 2021 17:30
Show Gist options
  • Save wilburx9/bcb942b9297a0321ab25c4e56595b772 to your computer and use it in GitHub Desktop.
Save wilburx9/bcb942b9297a0321ab25c4e56595b772 to your computer and use it in GitHub Desktop.
Confirming a Flutter Dismissible swipe event
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
var item = items[index];
return Dismissible(
...
confirmDismiss: (direction) => promptUser(direction));
},
),
);
}
Future<bool> promptUser(DismissDirection direction) async {
String action;
if (direction == DismissDirection.startToEnd) {
// This is a delete action
action = "delete";
} else {
archiveItem();
// This is an archive action
action = "archive";
}
return await showCupertinoDialog<bool>(
context: context,
builder: (context) => CupertinoAlertDialog(
content: Text("Are you sure you want to $action?"),
actions: <Widget>[
CupertinoDialogAction(
child: Text("Ok"),
onPressed: () {
// Dismiss the dialog and
// also dismiss the swiped item
Navigator.of(context).pop(true);
},
),
CupertinoDialogAction(
child: Text('Cancel'),
onPressed: () {
// Dismiss the dialog but don't
// dismiss the swiped item
return Navigator.of(context).pop(false);
},
)
],
),
) ??
false; // In case the user dismisses the dialog by clicking away from it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment