Skip to content

Re-land ScaffoldMessenger - #66504

Merged
Piinks merged 28 commits into
flutter:masterfrom
Piinks:scaffoldMessenger4
Oct 5, 2020
Merged

Piinks merged 28 commits into
flutter:masterfrom
Piinks:scaffoldMessenger4

Conversation

@Piinks

@Piinks Piinks commented Sep 23, 2020 •

Copy link
Copy Markdown
Contributor

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. p: material_ui material_ui package in flutter/packages a: quality A truly polished experience customer: money (g3) labels Sep 23, 2020
@Piinks
Piinks requested a review from HansMuller September 23, 2020 23:31

@HansMuller HansMuller left a comment

Copy link
Copy Markdown
Contributor

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?

Comment thread packages/flutter/lib/src/material/debug.dart Outdated
Comment thread packages/flutter/lib/src/material/scaffold.dart
Comment thread packages/flutter/lib/src/material/scaffold.dart Outdated
Comment thread packages/flutter/lib/src/material/scaffold.dart Outdated
/// _counter++;
/// });
/// if (_counter % 10 == 0) {
/// _scaffoldMessengerKey.currentState.showSnackBar(const SnackBar(

Copy link
Copy Markdown
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.

Comment thread packages/flutter/lib/src/material/scaffold.dart Outdated
Comment thread packages/flutter/lib/src/material/snack_bar.dart Outdated
Comment thread packages/flutter/lib/src/material/snack_bar.dart Outdated
Comment thread packages/flutter/lib/src/widgets/framework.dart Outdated
Comment thread packages/flutter/lib/src/material/snack_bar.dart Outdated
@Piinks

Piinks commented Sep 24, 2020 •

Copy link
Copy Markdown
Contributor Author

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

Piinks commented Sep 25, 2020

Copy link
Copy Markdown
Contributor Author

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

Piinks commented Sep 25, 2020

Copy link
Copy Markdown
Contributor Author

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

/// tooltip: 'Show Snackbar',
/// onPressed: () {
/// scaffoldKey.currentState.showSnackBar(snackBar);
/// ScaffoldMessenger.of(context).showSnackBar(snackBar);

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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. :)

@gspencergoog gspencergoog left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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.

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fly-by comment

Comment thread packages/flutter/lib/src/material/scaffold.dart Outdated
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) framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show a SnackBar across Scaffolds

6 participants