Last active
February 18, 2021 17:30
-
-
Save wilburx9/bcb942b9297a0321ab25c4e56595b772 to your computer and use it in GitHub Desktop.
Confirming a Flutter Dismissible swipe event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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