diff --git a/packages/material_ui/lib/src/drawer.dart b/packages/material_ui/lib/src/drawer.dart index 7070dfdc06a..884b3e59a9c 100644 --- a/packages/material_ui/lib/src/drawer.dart +++ b/packages/material_ui/lib/src/drawer.dart @@ -727,15 +727,6 @@ class DrawerControllerState extends State with SingleTickerPro return const SizedBox.shrink(); } } else { - final bool platformHasBackButton = switch (defaultTargetPlatform) { - TargetPlatform.android => true, - TargetPlatform.iOS || - TargetPlatform.macOS || - TargetPlatform.fuchsia || - TargetPlatform.linux || - TargetPlatform.windows => false, - }; - final Color scrimColor = widget.scrimColor ?? DrawerTheme.of(context).scrimColor ?? @@ -757,8 +748,7 @@ class DrawerControllerState extends State with SingleTickerPro children: [ BlockSemantics( child: ExcludeSemantics( - // On Android, the back button is used to dismiss a modal. - excluding: platformHasBackButton, + excluding: !widget.drawerBarrierDismissible, child: GestureDetector( onTap: widget.drawerBarrierDismissible ? close : null, child: Semantics( diff --git a/packages/material_ui/pending_changelogs/drawer_barrier_semantics_android.yaml b/packages/material_ui/pending_changelogs/drawer_barrier_semantics_android.yaml new file mode 100644 index 00000000000..74930adc047 --- /dev/null +++ b/packages/material_ui/pending_changelogs/drawer_barrier_semantics_android.yaml @@ -0,0 +1,3 @@ +changelog: | + - Exposes modal barrier dismiss semantics for `Drawer` on Android to support Switch Access. +version: patch diff --git a/packages/material_ui/test/drawer_test.dart b/packages/material_ui/test/drawer_test.dart index 448c02294fe..41d8330ae31 100644 --- a/packages/material_ui/test/drawer_test.dart +++ b/packages/material_ui/test/drawer_test.dart @@ -130,37 +130,34 @@ void main() { expect(find.text('Archive'), findsNothing); }); - testWidgets( - 'Drawer dismiss barrier has label', - (WidgetTester tester) async { - final semantics = SemanticsTester(tester); - await tester.pumpWidget(const MaterialApp(home: Scaffold(drawer: Drawer()))); + testWidgets('Drawer dismiss barrier has label', (WidgetTester tester) async { + final semantics = SemanticsTester(tester); + await tester.pumpWidget(const MaterialApp(home: Scaffold(drawer: Drawer()))); - final ScaffoldState state = tester.firstState(find.byType(Scaffold)); - state.openDrawer(); + final ScaffoldState state = tester.firstState(find.byType(Scaffold)); + state.openDrawer(); - await tester.pump(); - await tester.pump(const Duration(seconds: 1)); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); - expect( - semantics, - includesNodeWith( - label: const DefaultMaterialLocalizations().modalBarrierDismissLabel, - actions: [SemanticsAction.tap], - ), - ); + expect( + semantics, + includesNodeWith( + label: const DefaultMaterialLocalizations().modalBarrierDismissLabel, + actions: [SemanticsAction.tap], + ), + ); - semantics.dispose(); - }, - variant: const TargetPlatformVariant({ - TargetPlatform.iOS, - TargetPlatform.macOS, - }), - ); + semantics.dispose(); + }, variant: TargetPlatformVariant.all()); - testWidgets('Drawer dismiss barrier has no label', (WidgetTester tester) async { + testWidgets('Drawer dismiss barrier has no label when drawerBarrierDismissible is false', ( + WidgetTester tester, + ) async { final semantics = SemanticsTester(tester); - await tester.pumpWidget(const MaterialApp(home: Scaffold(drawer: Drawer()))); + await tester.pumpWidget( + const MaterialApp(home: Scaffold(drawerBarrierDismissible: false, drawer: Drawer())), + ); final ScaffoldState state = tester.firstState(find.byType(Scaffold)); state.openDrawer(); @@ -179,7 +176,7 @@ void main() { ); semantics.dispose(); - }, variant: TargetPlatformVariant.only(TargetPlatform.android)); + }, variant: TargetPlatformVariant.all()); testWidgets('Drawer tap test', (WidgetTester tester) async { final scaffoldKey = GlobalKey(); @@ -1308,69 +1305,70 @@ void main() { expect(find.byType(Drawer), findsExactly(0)); }); - testWidgets( - 'Dismissible Drawer includes button in semantic tree', - (WidgetTester tester) async { - final semantics = SemanticsTester(tester); - final scaffoldKey = GlobalKey(); + testWidgets('Dismissible Drawer includes button in semantic tree', (WidgetTester tester) async { + final semantics = SemanticsTester(tester); + final scaffoldKey = GlobalKey(); - await tester.pumpWidget( - MaterialApp( - home: Builder( - builder: (BuildContext context) { - return Scaffold(key: scaffoldKey, drawer: const Drawer()); - }, - ), + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (BuildContext context) { + return Scaffold(key: scaffoldKey, drawer: const Drawer()); + }, ), - ); + ), + ); - // Open the drawer. - scaffoldKey.currentState!.openDrawer(); - await tester.pump(const Duration(milliseconds: 100)); + // Open the drawer. + scaffoldKey.currentState!.openDrawer(); + await tester.pump(const Duration(milliseconds: 100)); - expect(semantics, includesNodeWith(actions: [SemanticsAction.tap])); - expect(semantics, includesNodeWith(label: 'Dismiss')); + expect(semantics, includesNodeWith(actions: [SemanticsAction.tap])); + expect( + semantics, + includesNodeWith(label: const DefaultMaterialLocalizations().modalBarrierDismissLabel), + ); - semantics.dispose(); - }, - variant: const TargetPlatformVariant({ - TargetPlatform.iOS, - TargetPlatform.macOS, - }), - ); + semantics.dispose(); + }, variant: TargetPlatformVariant.all()); - testWidgets( - 'Dismissible Drawer barrier is hidden from semantics on Android (back button is used to dismiss)', - (WidgetTester tester) async { - final semantics = SemanticsTester(tester); - final scaffoldKey = GlobalKey(); + testWidgets('Non-dismissible Drawer barrier is hidden from semantics', ( + WidgetTester tester, + ) async { + final semantics = SemanticsTester(tester); + final scaffoldKey = GlobalKey(); - await tester.pumpWidget( - MaterialApp( - home: Builder( - builder: (BuildContext context) { - return Scaffold(key: scaffoldKey, drawer: const Drawer(), body: Container()); - }, - ), + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (BuildContext context) { + return Scaffold( + key: scaffoldKey, + drawerBarrierDismissible: false, + drawer: const Drawer(), + ); + }, ), - ); + ), + ); - // Open the drawer. - scaffoldKey.currentState!.openDrawer(); - await tester.pump(const Duration(milliseconds: 100)); + // Open the drawer. + scaffoldKey.currentState!.openDrawer(); + await tester.pump(const Duration(milliseconds: 100)); - expect( - semantics, - isNot( - includesNodeWith(actions: [SemanticsAction.tap, SemanticsAction.focus]), - ), - ); - expect(semantics, isNot(includesNodeWith(label: 'Dismiss'))); + expect( + semantics, + isNot( + includesNodeWith(actions: [SemanticsAction.tap, SemanticsAction.focus]), + ), + ); + expect( + semantics, + isNot(includesNodeWith(label: const DefaultMaterialLocalizations().modalBarrierDismissLabel)), + ); - semantics.dispose(); - }, - variant: TargetPlatformVariant.only(TargetPlatform.android), - ); + semantics.dispose(); + }, variant: TargetPlatformVariant.all()); testWidgets('Drawer can be configured as not dismissible', (WidgetTester tester) async { await tester.pumpWidget( @@ -1530,28 +1528,13 @@ void main() { // Test barrier semantics. final semantics = SemanticsTester(tester); - final expectBarrierExcluded = defaultTargetPlatform == TargetPlatform.android; - - if (expectBarrierExcluded) { - expect( - semantics, - isNot( - includesNodeWith( - label: localizations.modalBarrierDismissLabel, - actions: [SemanticsAction.tap, SemanticsAction.focus], - ), - ), - ); - } else { - expect( - semantics, - includesNodeWith( - label: localizations.modalBarrierDismissLabel, - actions: [SemanticsAction.tap], - ), - ); - } - + expect( + semantics, + includesNodeWith( + label: localizations.modalBarrierDismissLabel, + actions: [SemanticsAction.tap], + ), + ); semantics.dispose(); }