Skip to content

Conversation

@jonahwilliams
Copy link
Contributor

@jonahwilliams jonahwilliams commented Oct 9, 2019

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

@fluttergithubbot fluttergithubbot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Oct 9, 2019
@jonahwilliams jonahwilliams marked this pull request as ready for review October 10, 2019 19:39
@jonahwilliams jonahwilliams added the a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) label Oct 10, 2019
/// Called the first time that the snackbar is visible within a [Scaffold].
final VoidCallback onVisible;

// API for Scaffold.addSnackBar():
Copy link
Member

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"?

Copy link
Contributor Author

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) {
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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();
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

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:

widget.animation.removeStatusListener(_onAnimationStatusChanged);
super.dispose();
}

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

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) {
Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Hixie
Copy link
Contributor

Hixie commented Oct 15, 2019

this approach LGTM

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jonahwilliams jonahwilliams merged commit bc396d1 into flutter:master Oct 17, 2019
@jonahwilliams jonahwilliams deleted the add_snackbar_callback branch October 17, 2019 22:12
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Nov 26, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SnackBar not announced for a11y on iOS

5 participants