forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error message and documentation when a
SnackBar
is off screen (…
…#102073)
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
examples/api/lib/material/scaffold/scaffold_messenger_state.show_snack_bar.1.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Flutter code sample for SnackBar | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
void main() => runApp(const SnackBarApp()); | ||
|
||
class SnackBarApp extends StatelessWidget { | ||
const SnackBarApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
home: SnackBarExample(), | ||
); | ||
} | ||
} | ||
|
||
class SnackBarExample extends StatefulWidget { | ||
const SnackBarExample({super.key}); | ||
|
||
@override | ||
State<SnackBarExample> createState() => _SnackBarExampleState(); | ||
} | ||
|
||
class _SnackBarExampleState extends State<SnackBarExample> { | ||
bool _largeLogo = false; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('SnackBar Sample')), | ||
body: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Column( | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () { | ||
const SnackBar snackBar = SnackBar( | ||
content: Text('A SnackBar has been shown.'), | ||
behavior: SnackBarBehavior.floating, | ||
); | ||
ScaffoldMessenger.of(context).showSnackBar(snackBar); | ||
}, | ||
child: const Text('Show SnackBar'), | ||
), | ||
const SizedBox(height: 8.0), | ||
ElevatedButton( | ||
onPressed: () { | ||
setState(() => _largeLogo = !_largeLogo); | ||
}, | ||
child: Text(_largeLogo ? 'Shrink Logo' : 'Grow Logo'), | ||
), | ||
], | ||
), | ||
), | ||
// A floating [SnackBar] is positioned above [Scaffold.floatingActionButton]. | ||
// If the Widget provided to the floatingActionButton slot takes up too much space | ||
// for the SnackBar to be visible, an error will be thrown. | ||
floatingActionButton: Container( | ||
constraints: BoxConstraints.tightFor( | ||
width: 150, | ||
height: _largeLogo ? double.infinity : 150, | ||
), | ||
decoration: const BoxDecoration( | ||
color: Colors.blueGrey, | ||
borderRadius: BorderRadius.all(Radius.circular(20)), | ||
), | ||
child: const FlutterLogo(), | ||
), | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/api/test/material/scaffold/scaffold_messenger_state.show_snack_bar.1_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/material/scaffold/scaffold_messenger_state.show_snack_bar.1.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('Floating SnackBar is visible', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.SnackBarApp(), | ||
); | ||
|
||
final Finder buttonFinder = find.byType(ElevatedButton); | ||
await tester.tap(buttonFinder.first); | ||
// Have the SnackBar fully animate out. | ||
await tester.pumpAndSettle(); | ||
|
||
final Finder snackBarFinder = find.byType(SnackBar); | ||
expect(snackBarFinder, findsOneWidget); | ||
|
||
// Grow logo to send SnackBar off screen. | ||
await tester.tap(buttonFinder.last); | ||
await tester.pumpAndSettle(); | ||
|
||
final AssertionError exception = tester.takeException() as AssertionError; | ||
const String message = 'Floating SnackBar presented off screen.\n' | ||
'A SnackBar with behavior property set to SnackBarBehavior.floating is fully ' | ||
'or partially off screen because some or all the widgets provided to ' | ||
'Scaffold.floatingActionButton, Scaffold.persistentFooterButtons and ' | ||
'Scaffold.bottomNavigationBar take up too much vertical space.\n' | ||
'Consider constraining the size of these widgets to allow room for the SnackBar to be visible.'; | ||
expect(exception.message, message); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters