Skip to content

Conversation

@bleroux
Copy link
Contributor

@bleroux bleroux commented Oct 28, 2024

Description

This PR makes it possible for the MaterialApp built in ScaffoldMessenger state to access the ambient theme.

Before this PR, the built in messenger was above the theme.
After this PR, the build in messenger is below the theme.

Related Issue

Fixes Can't access useMaterial3 from the theme in the showSnackBar method

Tests

Adds 1 test.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Oct 28, 2024
@bleroux bleroux requested a review from Piinks October 28, 2024 19:25
@justinmc
Copy link
Contributor

@nate-thegrate To review (from triage).

Copy link
Contributor

@nate-thegrate nate-thegrate left a comment

Choose a reason for hiding this comment

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

It's super great to see the simple fix, thank you!

I had an idea for the test and would love to hear your thoughts :)

Comment on lines 2881 to 2905
await tester.pumpWidget(MaterialApp(
theme: customTheme,
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
key: tapTarget,
onTap: () {
scaffoldMessenger = ScaffoldMessenger.maybeOf(context);
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));

await tester.tap(find.byKey(tapTarget));
await tester.pump();

final ThemeData messengerTheme = Theme.of(scaffoldMessenger!.context);
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we could make use of MaterialApp's scaffoldMessengerKey parameter here:

Suggested change
await tester.pumpWidget(MaterialApp(
theme: customTheme,
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
key: tapTarget,
onTap: () {
scaffoldMessenger = ScaffoldMessenger.maybeOf(context);
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));
await tester.tap(find.byKey(tapTarget));
await tester.pump();
final ThemeData messengerTheme = Theme.of(scaffoldMessenger!.context);
await tester.pumpWidget(
MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey,
theme: customTheme,
home: const SizedBox.shrink(),
),
);
final ThemeData messengerTheme = Theme.of(scaffoldMessengerKey.currentContext!);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! I will make the change tomorrow morning (it is the evening here).

testWidgets('ScaffoldMessenger showSnackBar default animation', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/115924.
testWidgets('Default ScaffoldMessenger can access ambient theme', (WidgetTester tester) async {
ScaffoldMessengerState? scaffoldMessenger;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ScaffoldMessengerState? scaffoldMessenger;
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

@nate-thegrate
Copy link
Contributor

bleroux requested a review from Piinks

(Kate is out this week btw)

@bleroux bleroux force-pushed the fix_default_ScaffoldMessenger_cannot_access_theme branch from fd4b72c to a1b4402 Compare October 31, 2024 09:19
@bleroux bleroux requested a review from nate-thegrate October 31, 2024 14:36
Copy link
Contributor

@nate-thegrate nate-thegrate left a comment

Choose a reason for hiding this comment

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

Flutter_LGTM

Thank you very much!

Comment on lines +992 to +996
child: DefaultSelectionStyle(
selectionColor: effectiveSelectionColor,
cursorColor: effectiveCursorColor,
child: childWidget,
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not in the scope of this PR, but I noticed that the Theme widget already builds a DefaultSelectionStyle:

Widget _wrapsWidgetThemes(BuildContext context, Widget child) {
final DefaultSelectionStyle selectionStyle = DefaultSelectionStyle.of(context);
return IconTheme(
data: data.iconTheme,
child: DefaultSelectionStyle(
selectionColor: data.textSelectionTheme.selectionColor ?? selectionStyle.selectionColor,
cursorColor: data.textSelectionTheme.cursorColor ?? selectionStyle.cursorColor,
child: child,
),
);
}

Would be nice to remove the duplicate widget here at some point :)

@bleroux bleroux added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 31, 2024
@auto-submit auto-submit bot merged commit 5f65bd0 into flutter:master Oct 31, 2024
74 checks passed
@bleroux bleroux deleted the fix_default_ScaffoldMessenger_cannot_access_theme branch October 31, 2024 21:05
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 1, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Nov 1, 2024
flutter/flutter@0fe6153...f86b777

2024-11-01 [email protected] Roll Packages from 7cc1caa to 796afa3 (15 revisions) (flutter/flutter#158003)
2024-11-01 [email protected] Marks Linux_pixel_7pro service_extensions_test to be flaky (flutter/flutter#157853)
2024-11-01 [email protected] Roll Flutter Engine from 0a0d5c9be6ff to 3a090b46dd35 (1 revision) (flutter/flutter#157994)
2024-11-01 [email protected] Roll Flutter Engine from bacc5e1e73b7 to 0a0d5c9be6ff (3 revisions) (flutter/flutter#157991)
2024-11-01 [email protected] Add test for `interactive_viewer.transformation_controller.0.dart` (flutter/flutter#157986)
2024-11-01 [email protected] Roll Flutter Engine from d7e928911ac2 to bacc5e1e73b7 (1 revision) (flutter/flutter#157982)
2024-11-01 [email protected] Add test for `notification.0.dart` (flutter/flutter#157909)
2024-11-01 [email protected] performance: Override .elementAt in CachingIterable (flutter/flutter#152477)
2024-11-01 [email protected] Roll Flutter Engine from cd46383cd55e to d7e928911ac2 (4 revisions) (flutter/flutter#157978)
2024-11-01 [email protected] Roll Flutter Engine from bb77cf867aef to cd46383cd55e (11 revisions) (flutter/flutter#157972)
2024-11-01 [email protected] Add a warning/additional handlers for parsing`synthetic-package`. (flutter/flutter#157934)
2024-10-31 [email protected] Roll Flutter Engine from f2154ef3e31c to bb77cf867aef (6 revisions) (flutter/flutter#157960)
2024-10-31 [email protected] Renames `injectBuildTimePluginFilesForWebPlatform` and removes unused named parameter. (flutter/flutter#157944)
2024-10-31 [email protected] [flutter_driver] use mostly public screenshot API. (flutter/flutter#157888)
2024-10-31 [email protected] Made insetPadding configurable for Date Picker Dialog (flutter/flutter#155651)
2024-10-31 [email protected] Fix showSnackBar can't access useMaterial3 from the theme (flutter/flutter#157707)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 12, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App 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.

Can't access useMaterial3 from the theme in the showSnackBar method to conditionally change SnackBar animation for Material 3.

3 participants