-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Add onVisible callback to snackbar. #42344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the method called "showSnackBar"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| if (widget.onVisible != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should work, modulo handling the disable animation from accessibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added it as an animation status listener
| } | ||
|
|
||
| @override | ||
| State createState() => _SnackBarState(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing a generic type annotation on State
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surprised the analyzer didn't catch it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is because of the optionTypeArgs annotation:
| @optionalTypeArgs |
| widget.animation.removeStatusListener(_onAnimationStatusChanged); | ||
| super.dispose(); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a didUpdateWidget in case the animation changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| } | ||
|
|
||
| void _onAnimationStatusChanged(AnimationStatus animationStatus) { | ||
| if (animationStatus == AnimationStatus.completed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always compare enums using switches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| if (animationStatus == AnimationStatus.completed) { | ||
| if (widget.onVisible != null && !_wasVisible) { | ||
| widget.onVisible(); | ||
| _wasVisible = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we set _wasVisible regardless of whether we fire the callback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
this approach LGTM |
goderbauer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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