Skip to content

Commit 1653592

Browse files
authored
Add CloseButtonTooltip to the 'X' button on a SnackBar (#143934)
Adds a localized Close Button tooltip to the 'X' Button on a SnackBar, making it readable by screen readers. Github Issue #143793 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent b476e96 commit 1653592

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

packages/flutter/lib/src/material/snack_bar.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'colors.dart';
1111
import 'icon_button.dart';
1212
import 'icons.dart';
1313
import 'material.dart';
14+
import 'material_localizations.dart';
1415
import 'material_state.dart';
1516
import 'scaffold.dart';
1617
import 'snack_bar_theme.dart';
@@ -656,6 +657,7 @@ class _SnackBarState extends State<SnackBar> {
656657
iconSize: 24.0,
657658
color: widget.closeIconColor ?? snackBarTheme.closeIconColor ?? defaults.closeIconColor,
658659
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(reason: SnackBarClosedReason.dismiss),
660+
tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
659661
)
660662
: null;
661663

packages/flutter/test/material/snack_bar_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,6 +3243,40 @@ void main() {
32433243
'snack_bar.goldenTest.floatingWithActionWithIcon.png'));
32443244
});
32453245

3246+
testWidgets('SnackBar has tooltip for Close Button', (WidgetTester tester) async {
3247+
// Regression test for https://github.com/flutter/flutter/issues/143793
3248+
await tester.pumpWidget(MaterialApp(
3249+
theme: ThemeData(useMaterial3: false),
3250+
debugShowCheckedModeBanner: false, // https://github.com/flutter/flutter/issues/143616
3251+
home: const Scaffold(
3252+
bottomSheet: SizedBox(
3253+
width: 200,
3254+
height: 50,
3255+
child: ColoredBox(
3256+
color: Colors.pink,
3257+
),
3258+
),
3259+
),
3260+
));
3261+
3262+
final ScaffoldMessengerState scaffoldMessengerState = tester.state(find.byType(ScaffoldMessenger));
3263+
scaffoldMessengerState.showSnackBar(
3264+
SnackBar(
3265+
content: const Text('Snackbar with close button'),
3266+
duration: const Duration(days: 365),
3267+
action: SnackBarAction(label: 'ACTION', onPressed: () {}),
3268+
behavior: SnackBarBehavior.floating,
3269+
showCloseIcon: true,
3270+
),
3271+
);
3272+
await tester.pumpAndSettle(); // Have the SnackBar fully animate in.
3273+
3274+
expect(
3275+
find.byTooltip(MaterialLocalizations.of(scaffoldMessengerState.context).closeButtonLabel),
3276+
findsOneWidget
3277+
);
3278+
});
3279+
32463280
testWidgets('Material2 - Fixed width snackbar can display optional icon', (WidgetTester tester) async {
32473281
await tester.pumpWidget(MaterialApp(
32483282
theme: ThemeData(useMaterial3: false),

0 commit comments

Comments
 (0)