diff --git a/examples/flutter_gallery/lib/demo/cupertino/cupertino_picker_demo.dart b/examples/flutter_gallery/lib/demo/cupertino/cupertino_picker_demo.dart index 9ce07f69ce..f45819e94d 100644 --- a/examples/flutter_gallery/lib/demo/cupertino/cupertino_picker_demo.dart +++ b/examples/flutter_gallery/lib/demo/cupertino/cupertino_picker_demo.dart @@ -108,6 +108,7 @@ class _CupertinoPickerDemoState extends State { onTap: () async { await showCupertinoModalPopup( context: context, + semanticsDismissible: true, builder: (BuildContext context) { return _BottomPicker( child: CupertinoPicker( @@ -144,6 +145,7 @@ class _CupertinoPickerDemoState extends State { onTap: () { showCupertinoModalPopup( context: context, + semanticsDismissible: true, builder: (BuildContext context) { return _BottomPicker( child: CupertinoTimerPicker( @@ -176,6 +178,7 @@ class _CupertinoPickerDemoState extends State { onTap: () { showCupertinoModalPopup( context: context, + semanticsDismissible: true, builder: (BuildContext context) { return _BottomPicker( child: CupertinoDatePicker( @@ -207,6 +210,7 @@ class _CupertinoPickerDemoState extends State { onTap: () { showCupertinoModalPopup( context: context, + semanticsDismissible: true, builder: (BuildContext context) { return _BottomPicker( child: CupertinoDatePicker( @@ -238,6 +242,7 @@ class _CupertinoPickerDemoState extends State { onTap: () { showCupertinoModalPopup( context: context, + semanticsDismissible: true, builder: (BuildContext context) { return _BottomPicker( child: CupertinoDatePicker( diff --git a/packages/flutter/lib/src/cupertino/picker.dart b/packages/flutter/lib/src/cupertino/picker.dart index f20f796f0d..2ab858d8ab 100644 --- a/packages/flutter/lib/src/cupertino/picker.dart +++ b/packages/flutter/lib/src/cupertino/picker.dart @@ -35,7 +35,8 @@ const double _kOverAndUnderCenterOpacity = 0.447; /// that child the initially selected child. /// /// Can be used with [showCupertinoModalPopup] to display the picker modally at the -/// bottom of the screen. +/// bottom of the screen. When calling [showCupertinoModalPopup], be sure to set +/// `semanticsDismissible` to true to enable dismissing the modal via semantics. /// /// Sizes itself to its parent. All children are sized to the same size based /// on [itemExtent]. diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index 61e88f6b0e..8ecbaeca3b 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -792,14 +792,18 @@ class _CupertinoModalPopupRoute extends PopupRoute { this.barrierColor, this.barrierLabel, this.builder, + bool semanticsDismissible, ImageFilter filter, RouteSettings settings, }) : super( filter: filter, settings: settings, - ); + ) { + _semanticsDismissible = semanticsDismissible; + } final WidgetBuilder builder; + bool _semanticsDismissible; @override final String barrierLabel; @@ -811,7 +815,7 @@ class _CupertinoModalPopupRoute extends PopupRoute { bool get barrierDismissible => true; @override - bool get semanticsDismissible => false; + bool get semanticsDismissible => _semanticsDismissible ?? false; @override Duration get transitionDuration => _kModalPopupTransitionDuration; @@ -871,6 +875,9 @@ class _CupertinoModalPopupRoute extends PopupRoute { /// popup to the [Navigator] furthest from or nearest to the given `context`. It /// is `false` by default. /// +/// The `semanticsDismissble` argument is used to determine whether the +/// semantics of the modal barrier are included in the semantics tree. +/// /// The `builder` argument typically builds a [CupertinoActionSheet] widget. /// Content below the widget is dimmed with a [ModalBarrier]. The widget built /// by the `builder` does not share a context with the location that @@ -891,6 +898,7 @@ Future showCupertinoModalPopup({ @required WidgetBuilder builder, ImageFilter filter, bool useRootNavigator = true, + bool semanticsDismissible, }) { assert(useRootNavigator != null); return Navigator.of(context, rootNavigator: useRootNavigator).push( @@ -899,6 +907,7 @@ Future showCupertinoModalPopup({ barrierLabel: 'Dismiss', builder: builder, filter: filter, + semanticsDismissible: semanticsDismissible, ), ); } diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index ef2a58e2fa..cf1c6be16f 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -3,11 +3,14 @@ // found in the LICENSE file. import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; +import '../widgets/semantics_tester.dart'; + void main() { MockNavigatorObserver navigatorObserver; @@ -1052,6 +1055,75 @@ void main() { expect(rootObserver.dialogCount, 0); expect(nestedObserver.dialogCount, 1); }); + + testWidgets('showCupertinoModalPopup does not allow for semantics dismiss by default', (WidgetTester tester) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final SemanticsTester semantics = SemanticsTester(tester); + await tester.pumpWidget(CupertinoApp( + home: Navigator( + onGenerateRoute: (RouteSettings settings) { + return PageRouteBuilder( + pageBuilder: (BuildContext context, Animation _, Animation __) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const SizedBox(), + ); + }, + child: const Text('tap'), + ); + }, + ); + }, + ), + )); + + // Push the route. + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + + expect(semantics, isNot(includesNodeWith( + actions: [SemanticsAction.tap], + label: 'Dismiss', + ))); + debugDefaultTargetPlatformOverride = null; + }); + + testWidgets('showCupertinoModalPopup allows for semantics dismiss when set', (WidgetTester tester) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final SemanticsTester semantics = SemanticsTester(tester); + await tester.pumpWidget(CupertinoApp( + home: Navigator( + onGenerateRoute: (RouteSettings settings) { + return PageRouteBuilder( + pageBuilder: (BuildContext context, Animation _, Animation __) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + semanticsDismissible: true, + builder: (BuildContext context) => const SizedBox(), + ); + }, + child: const Text('tap'), + ); + }, + ); + }, + ), + )); + + // Push the route. + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + + expect(semantics, includesNodeWith( + actions: [SemanticsAction.tap], + label: 'Dismiss', + )); + debugDefaultTargetPlatformOverride = null; + }); } class MockNavigatorObserver extends Mock implements NavigatorObserver {}