Add onVisible callback to snackbar.#42344
Conversation
| /// Called the first time that the snackbar is visible within a [Scaffold]. | ||
| final VoidCallback onVisible; | ||
|
|
||
| // API for Scaffold.addSnackBar(): |
There was a problem hiding this comment.
Isn't the method called "showSnackBar"?
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| if (widget.onVisible != null) { |
There was a problem hiding this comment.
It feels very odd doing this in the initState. I wonder if it would be better to tie this to the animation? When the animation is complete, the snackbar is visible?
/cc @HansMuller
There was a problem hiding this comment.
That should work, modulo handling the disable animation from accessibility
There was a problem hiding this comment.
I've added it as an animation status listener
| } | ||
|
|
||
| @override | ||
| State createState() => _SnackBarState(); |
There was a problem hiding this comment.
missing a generic type annotation on State
There was a problem hiding this comment.
surprised the analyzer didn't catch it
There was a problem hiding this comment.
I wonder if this is because of the optionTypeArgs annotation:
| widget.animation.removeStatusListener(_onAnimationStatusChanged); | ||
| super.dispose(); | ||
| } | ||
|
|
There was a problem hiding this comment.
needs a didUpdateWidget in case the animation changes
| } | ||
|
|
||
| void _onAnimationStatusChanged(AnimationStatus animationStatus) { | ||
| if (animationStatus == AnimationStatus.completed) { |
There was a problem hiding this comment.
always compare enums using switches
| if (animationStatus == AnimationStatus.completed) { | ||
| if (widget.onVisible != null && !_wasVisible) { | ||
| widget.onVisible(); | ||
| _wasVisible = true; |
There was a problem hiding this comment.
shouldn't we set _wasVisible regardless of whether we fire the callback?
|
this approach LGTM |
Description
Add a callback to the snackbar that is invoked the first time it is called. Requires updating the snackbar to a StatefulWidget.
Fixes #41427