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].*
This commit is contained in:
dsanagustin 2024-02-22 17:45:29 -05:00 committed by GitHub
parent b476e9674e
commit 16535924f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View file

@ -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<SnackBar> {
iconSize: 24.0,
color: widget.closeIconColor ?? snackBarTheme.closeIconColor ?? defaults.closeIconColor,
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(reason: SnackBarClosedReason.dismiss),
tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
)
: null;

View file

@ -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),