Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
eb9bac64
Unverified
Commit
eb9bac64
authored
Jan 22, 2021
by
Kate Lovett
Committed by
GitHub
Jan 22, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ScaffoldMessenger only shows to root nested Scaffold (#74093)
parent
4bdea117
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
1 deletion
+47
-1
scaffold.dart
packages/flutter/lib/src/material/scaffold.dart
+15
-1
snack_bar_test.dart
packages/flutter/test/material/snack_bar_test.dart
+32
-0
No files found.
packages/flutter/lib/src/material/scaffold.dart
View file @
eb9bac64
...
...
@@ -68,6 +68,12 @@ enum _ScaffoldSlot {
/// [BuildContext] via [ScaffoldMessenger.of] and use the
/// [ScaffoldMessengerState.showSnackBar] function.
///
/// When the [ScaffoldMessenger] has nested [Scaffold] descendants, the
/// ScaffoldMessenger will only present a [SnackBar] to the root Scaffold of
/// the subtree of Scaffolds. In order to show SnackBars in the inner, nested
/// Scaffolds, set a new scope for your SnackBars by instantiating a new
/// ScaffoldMessenger in between the levels of nesting.
///
/// {@tool dartpad --template=stateless_widget_scaffold_center}
///
/// Here is an example of showing a [SnackBar] when the user presses a button.
...
...
@@ -377,10 +383,18 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
void
_updateScaffolds
()
{
for
(
final
ScaffoldState
scaffold
in
_scaffolds
)
{
scaffold
.
_updateSnackBar
();
if
(
_isRoot
(
scaffold
))
scaffold
.
_updateSnackBar
();
}
}
// Nested Scaffolds are handled by the ScaffoldMessenger by only presenting a
// SnackBar in the root Scaffold of the nested set.
bool
_isRoot
(
ScaffoldState
scaffold
)
{
final
ScaffoldState
?
parent
=
scaffold
.
context
.
findAncestorStateOfType
<
ScaffoldState
>();
return
parent
==
null
||
!
_scaffolds
.
contains
(
parent
);
}
/// Removes the current [SnackBar] (if any) immediately from registered
/// [Scaffold]s.
///
...
...
packages/flutter/test/material/snack_bar_test.dart
View file @
eb9bac64
...
...
@@ -2291,4 +2291,36 @@ void main() {
await
expectLater
(
find
.
byType
(
MaterialApp
),
matchesGoldenFile
(
'snack_bar.goldenTest.workWithBottomSheet.png'
));
});
testWidgets
(
'ScaffoldMessenger presents SnackBars to only the root Scaffold when Scaffolds are nested.'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
MaterialApp
(
home:
Scaffold
(
body:
const
Scaffold
(),
floatingActionButton:
FloatingActionButton
(
onPressed:
()
{}),
),
));
final
ScaffoldMessengerState
scaffoldMessengerState
=
tester
.
state
<
ScaffoldMessengerState
>(
find
.
byType
(
ScaffoldMessenger
),
);
scaffoldMessengerState
.
showSnackBar
(
SnackBar
(
content:
const
Text
(
'ScaffoldMessenger'
),
duration:
const
Duration
(
seconds:
2
),
action:
SnackBarAction
(
label:
'ACTION'
,
onPressed:
()
{}),
behavior:
SnackBarBehavior
.
floating
,
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
byType
(
SnackBar
),
findsOneWidget
);
// The FloatingActionButton helps us identify which Scaffold has the
// SnackBar here. Since the outer Scaffold contains a FAB, the SnackBar
// should be above it. If the inner Scaffold had the SnackBar, it would be
// overlapping the FAB.
await
expectLater
(
find
.
byType
(
MaterialApp
),
matchesGoldenFile
(
'snack_bar.scaffold.nested.png'
),
);
final
Offset
snackBarTopRight
=
tester
.
getTopRight
(
find
.
byType
(
SnackBar
));
expect
(
snackBarTopRight
.
dy
,
465.0
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment