Unverified Commit 171d8c1e authored by Kate Lovett's avatar Kate Lovett Committed by GitHub

[Reland] Assert there are Scaffolds to present to for snackbars and banners (#103426)

parent a9ac7fb0
...@@ -265,6 +265,11 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide ...@@ -265,6 +265,11 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
/// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_snack_bar.0.dart ** /// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_snack_bar.0.dart **
/// {@end-tool} /// {@end-tool}
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar(SnackBar snackBar) { ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar(SnackBar snackBar) {
assert(
_scaffolds.isNotEmpty,
'ScaffoldMessenger.showSnackBar was called, but there are currently no '
'descendant Scaffolds to present to.',
);
_snackBarController ??= SnackBar.createAnimationController(vsync: this) _snackBarController ??= SnackBar.createAnimationController(vsync: this)
..addStatusListener(_handleSnackBarStatusChanged); ..addStatusListener(_handleSnackBarStatusChanged);
if (_snackBars.isEmpty) { if (_snackBars.isEmpty) {
...@@ -391,6 +396,11 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide ...@@ -391,6 +396,11 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
/// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_material_banner.0.dart ** /// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_material_banner.0.dart **
/// {@end-tool} /// {@end-tool}
ScaffoldFeatureController<MaterialBanner, MaterialBannerClosedReason> showMaterialBanner(MaterialBanner materialBanner) { ScaffoldFeatureController<MaterialBanner, MaterialBannerClosedReason> showMaterialBanner(MaterialBanner materialBanner) {
assert(
_scaffolds.isNotEmpty,
'ScaffoldMessenger.showMaterialBanner was called, but there are currently no '
'descendant Scaffolds to present to.',
);
_materialBannerController ??= MaterialBanner.createAnimationController(vsync: this) _materialBannerController ??= MaterialBanner.createAnimationController(vsync: this)
..addStatusListener(_handleMaterialBannerStatusChanged); ..addStatusListener(_handleMaterialBannerStatusChanged);
if (_materialBanners.isEmpty) { if (_materialBanners.isEmpty) {
......
...@@ -1073,6 +1073,35 @@ void main() { ...@@ -1073,6 +1073,35 @@ void main() {
await tester.tap(dismissTarget); await tester.tap(dismissTarget);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
}); });
testWidgets('ScaffoldMessenger will alert for MaterialBanners that cannot be presented', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/103004
await tester.pumpWidget(const MaterialApp(
home: Center(),
));
final ScaffoldMessengerState scaffoldMessengerState = tester.state<ScaffoldMessengerState>(
find.byType(ScaffoldMessenger),
);
expect(
() {
scaffoldMessengerState.showMaterialBanner(const MaterialBanner(
content: Text('Banner'),
actions: <Widget>[],
));
},
throwsA(
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains(
'ScaffoldMessenger.showMaterialBanner was called, but there are currently '
'no descendant Scaffolds to present to.'
)
),
),
);
});
} }
Material _getMaterialFromBanner(WidgetTester tester) { Material _getMaterialFromBanner(WidgetTester tester) {
......
...@@ -2090,6 +2090,34 @@ void main() { ...@@ -2090,6 +2090,34 @@ void main() {
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
await expectLater(find.byType(MaterialApp), matchesGoldenFile('snack_bar.goldenTest.backdropFilter.png')); await expectLater(find.byType(MaterialApp), matchesGoldenFile('snack_bar.goldenTest.backdropFilter.png'));
}); });
testWidgets('ScaffoldMessenger will alert for snackbars that cannot be presented', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/103004
await tester.pumpWidget(const MaterialApp(
home: Center(),
));
final ScaffoldMessengerState scaffoldMessengerState = tester.state<ScaffoldMessengerState>(
find.byType(ScaffoldMessenger),
);
expect(
() {
scaffoldMessengerState.showSnackBar(const SnackBar(
content: Text('SnackBar'),
));
},
throwsA(
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains(
'ScaffoldMessenger.showSnackBar was called, but there are currently '
'no descendant Scaffolds to present to.'
)
),
),
);
});
} }
/// Start test for "SnackBar dismiss test". /// Start test for "SnackBar dismiss test".
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment