Created
April 27, 2024 18:18
-
-
Save antoniomldev/3fea1fdfeb7736dc56d4e4eb96ac9647 to your computer and use it in GitHub Desktop.
Swipe to Dismiss
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
void showSnackBar() { | |
ScaffoldMessenger.of(context) | |
.showSnackBar(SnackBar(content: Text('Você arquivou essa mensagem!'))); | |
} | |
Future<bool> showAlertDialog() async { | |
return await showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('Deletar item'), | |
content: Text('Você tem certeza que deseja deletar?'), | |
actions: [ | |
ElevatedButton( | |
onPressed: () { | |
Navigator.of(context).pop(true); | |
}, | |
child: Text('Deletar')), | |
ElevatedButton( | |
onPressed: () { | |
Navigator.of(context).pop(false); | |
}, | |
child: Text('Cancelar')) | |
], | |
); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('SwipeToDismiss - Mensagens'), | |
centerTitle: true, | |
), | |
body: ListView.builder( | |
itemCount: minhasMensagens.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Dismissible( | |
key: UniqueKey(), | |
child: ListTile( | |
title: Text(minhasMensagens[index].titulo), | |
subtitle: Text(minhasMensagens[index].corpo), | |
), | |
background: Container( | |
padding: EdgeInsets.all(20), | |
color: Colors.green[400], | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.start, | |
children: [ | |
Icon(Icons.archive_rounded), | |
], | |
), | |
), | |
secondaryBackground: Container( | |
padding: EdgeInsets.all(20), | |
color: Colors.red[400], | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: [ | |
Icon(Icons.delete_sweep_rounded), | |
], | |
)), | |
onDismissed: (DismissDirection direction) { | |
if (direction == DismissDirection.startToEnd) { | |
return showSnackBar(); | |
} | |
}, | |
confirmDismiss: (DismissDirection direction) async { | |
if (direction == DismissDirection.endToStart) { | |
return await showAlertDialog(); | |
} | |
return false; | |
}); | |
}, | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment