Show delete confirmation in Flutter
1 min read

Show delete confirmation in Flutter

To show a confirmation dialog in Flutter, use AlertDialog widget along with showDialog method.

showDialog(
  context: context,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('Delete Item'),
      content: Text('Are you sure you want to delete this item?'),
      actions: [
        TextButton(
          child: Text('Cancel'),
          onPressed: () => Navigator.pop(context, false),
        ),
        TextButton(
          child: Text('Delete'),
          onPressed: () => Navigator.pop(context, true),
        ),
      ],
    );
  },
).then((confirmed) {
  if (confirmed) {
    // Perform the delete operation
  }
});

Navigator is used for returning what the user has opted for; whether he has confirmed deletion or not.