From 16535924f2c4335258d8ca72b82249a72ac0fc7f Mon Sep 17 00:00:00 2001 From: dsanagustin <110420204+dsanagustin@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:45:29 -0500 Subject: [PATCH] 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].* --- .../flutter/lib/src/material/snack_bar.dart | 2 ++ .../flutter/test/material/snack_bar_test.dart | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 5f38e64b932..78e1b2eb81e 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -11,6 +11,7 @@ import 'colors.dart'; import 'icon_button.dart'; import 'icons.dart'; import 'material.dart'; +import 'material_localizations.dart'; import 'material_state.dart'; import 'scaffold.dart'; import 'snack_bar_theme.dart'; @@ -656,6 +657,7 @@ class _SnackBarState extends State { iconSize: 24.0, color: widget.closeIconColor ?? snackBarTheme.closeIconColor ?? defaults.closeIconColor, onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(reason: SnackBarClosedReason.dismiss), + tooltip: MaterialLocalizations.of(context).closeButtonTooltip, ) : null; diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 302cb321af1..6225e485788 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -3243,6 +3243,40 @@ void main() { 'snack_bar.goldenTest.floatingWithActionWithIcon.png')); }); + testWidgets('SnackBar has tooltip for Close Button', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/143793 + await tester.pumpWidget(MaterialApp( + theme: ThemeData(useMaterial3: false), + debugShowCheckedModeBanner: false, // https://github.com/flutter/flutter/issues/143616 + home: const Scaffold( + bottomSheet: SizedBox( + width: 200, + height: 50, + child: ColoredBox( + color: Colors.pink, + ), + ), + ), + )); + + final ScaffoldMessengerState scaffoldMessengerState = tester.state(find.byType(ScaffoldMessenger)); + scaffoldMessengerState.showSnackBar( + SnackBar( + content: const Text('Snackbar with close button'), + duration: const Duration(days: 365), + action: SnackBarAction(label: 'ACTION', onPressed: () {}), + behavior: SnackBarBehavior.floating, + showCloseIcon: true, + ), + ); + await tester.pumpAndSettle(); // Have the SnackBar fully animate in. + + expect( + find.byTooltip(MaterialLocalizations.of(scaffoldMessengerState.context).closeButtonLabel), + findsOneWidget + ); + }); + testWidgets('Material2 - Fixed width snackbar can display optional icon', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp( theme: ThemeData(useMaterial3: false),