Skip to content

Instantly share code, notes, and snippets.

@wilburx9
Last active January 16, 2023 07:02
Show Gist options
  • Save wilburx9/c5b522bdfc2fd8e79e58c60a93a173ab to your computer and use it in GitHub Desktop.
Save wilburx9/c5b522bdfc2fd8e79e58c60a93a173ab to your computer and use it in GitHub Desktop.
Animated Flutter Dismissible
Tween<Offset> _offSetTween = Tween(
begin: Offset(1, 0),
end: Offset.zero,
);
...
@override
Widget build(BuildContext context) {
return Scaffold(
...
body: AnimatedList(
key: _animatedListKey,
initialItemCount: items.length,
itemBuilder: (context, index, animation) {
var item = items[index];
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: _offSetTween.animate(animation),
child: Dismissible(
...
),
),
);
},
),
);
}
handleDismiss(DismissDirection direction, int index) {
...
// Remove it from the list
items.removeAt(index);
// Remove it from the animated list
_animatedListKey.currentState.removeItem(
index,
(context, animation) {
return SizedBox();
},
...
_scaffoldKey.currentState
.showSnackBar(
SnackBar(
...
action: SnackBarAction(
label: "Undo",
textColor: Colors.yellow,
onPressed: () {
// Deep copy the email
final copiedEmail = Email.copy(swipedEmail);
// Insert it at swiped position and set state
items.insert(index, copiedEmail);
// Also insert the item to the AnimatedList
_animatedListKey.currentState.insertItem(index);
}),
),
)
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment