Skip to content
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

Re-land ScaffoldMessenger #66504

Merged
merged 28 commits into from
Oct 5, 2020
Merged

Re-land ScaffoldMessenger #66504

merged 28 commits into from
Oct 5, 2020

Conversation

Piinks
Copy link
Contributor

@Piinks Piinks commented Sep 23, 2020

Description

This will re-introduce the ScaffoldMessenger, this time leaving in place the old API for SnackBars via the Scaffold. This will allow for an easier migration for our customers.
As such, this adds an assertion to ensure we are only using one SnackBar API, since they are not meant to be used interchangeably.

The documentation and samples have been updated to recommend ScaffoldMessenger in order to mitigate confusion during the interim.

Original PR: #64101
Migration Guide: flutter/website#4527

Follow-up work:

  • Migrate the rest of the framework (see [WIP] ScaffoldMessenger Migration #64170)
  • There is a SnackBar demo in the cookbook that will need updating
  • Check the rest of website for snackbar use
  • Notify & migrate customers, deprecate old methods after migration

Related Issues

Fixes #62921

Tests

Oodles.
All existing SnackBar tests have been migrated and passed. Added a few more for new persistence across routes.
Several control tests for the original API have been preserved to ensure we are not breaking the old API while bringing in the new.

Checklist

Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.

@Piinks Piinks added c: new feature Nothing broken; request for a new capability framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. a: quality A truly polished experience customer: money (g3) labels Sep 23, 2020
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

I haven't looked at the tests yet.

Is there some way to avoid temporarily adding SnackBarAction,SnackBar.useScaffoldMessenger?

packages/flutter/lib/src/material/debug.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/scaffold.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/scaffold.dart Outdated Show resolved Hide resolved
/// _counter++;
/// });
/// if (_counter % 10 == 0) {
/// _scaffoldMessengerKey.currentState.showSnackBar(const SnackBar(
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case you do have access to the context, because State.context. If the incrementCounter method and its state were in a separate class, this "no context" scenario would be clearer.

packages/flutter/lib/src/material/scaffold.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/snack_bar.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/snack_bar.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/widgets/framework.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/snack_bar.dart Outdated Show resolved Hide resolved
@Piinks
Copy link
Contributor Author

Piinks commented Sep 24, 2020

Is there some way to avoid temporarily adding SnackBarAction,SnackBar.useScaffoldMessenger?

I haven't been able to come up with another way yet. 🤔 Since SnackBarAction and SnackBar call on the SnackBar methods, and we are going to support both APIs temporarily, they have to know which one to call, Scaffold or ScaffoldMessenger.

@Piinks
Copy link
Contributor Author

Piinks commented Sep 25, 2020

Marking as WIP, @HansMuller and I discussed a re-work that will simplify the way SnackBars can handle themselves in this 2-way API interim.

@Piinks
Copy link
Contributor Author

Piinks commented Sep 25, 2020

@HansMuller applied the changes from our brainstorming session, PTAL. :)

@@ -137,7 +137,7 @@ class _ToolbarContainerLayout extends SingleChildLayoutDelegate {
/// icon: const Icon(Icons.add_alert),
/// tooltip: 'Show Snackbar',
/// onPressed: () {
/// scaffoldKey.currentState.showSnackBar(snackBar);
/// ScaffoldMessenger.of(context).showSnackBar(snackBar);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't the of call return null if there's no messenger in the tree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can, but in this case it there is a MaterialApp above in the sample code, so it has a ScaffoldMessenger by default.

///
/// * [debugCheckHasScaffoldMessenger], which asserts that the given context
/// has a [ScaffoldMessenger] ancestor.
static ScaffoldMessengerState of(BuildContext context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this include a call to debugCheckHasScaffoldMessenger to assert with a nice message when it fails to find one it expected to find?

Also, a common pattern with these of calls is to include a nullOk optional bool that allows it to not assert and just return null.

I realize we do also have this pattern in some places of just returning null, but I guess I'm asking which is more appropriate for the use case: defaulting to throwing if it isn't found, or defaulting to not throwing and making the developer check for it? I think I'd lean toward the former, because it'll make them think about their choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At one point I had it the way you describe, but it was massively breaking at the time. It's been re-worked a fair bit since then, so I think I should be able to add this back now. Thank you!

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 applied your suggestions, PTAL. :)

Copy link
Contributor

@gspencergoog gspencergoog left a comment

Choose a reason for hiding this comment

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

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

@fluttergithubbot
Copy link
Contributor

This pull request is not suitable for automatic merging in its current state.

  • The status or check suite Linux framework_tests has failed. Please fix the issues identified (or deflake) before re-applying this label.

@fluttergithubbot
Copy link
Contributor

This pull request is not suitable for automatic merging in its current state.

  • The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

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.

Fly-by comment

packages/flutter/lib/src/material/scaffold.dart Outdated Show resolved Hide resolved
Co-authored-by: Michael Goderbauer <goderbauer@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: quality A truly polished experience c: new feature Nothing broken; request for a new capability customer: money (g3) 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.

Show a SnackBar across Scaffolds
6 participants