Last active
January 16, 2023 07:02
-
-
Save wilburx9/c5b522bdfc2fd8e79e58c60a93a173ab to your computer and use it in GitHub Desktop.
Animated Flutter Dismissible
This file contains hidden or 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
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