diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index ee4ec55275..efe9a62aa8 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -933,6 +933,7 @@ class _CupertinoModalPopupRoute extends PopupRoute { required this.barrierColor, required this.barrierLabel, required this.builder, + bool? barrierDismissible, bool? semanticsDismissible, required ImageFilter? filter, RouteSettings? settings, @@ -940,10 +941,14 @@ class _CupertinoModalPopupRoute extends PopupRoute { filter: filter, settings: settings, ) { + _barrierDismissible = barrierDismissible; _semanticsDismissible = semanticsDismissible; } final WidgetBuilder builder; + + bool? _barrierDismissible; + bool? _semanticsDismissible; @override @@ -953,7 +958,7 @@ class _CupertinoModalPopupRoute extends PopupRoute { final Color? barrierColor; @override - bool get barrierDismissible => true; + bool get barrierDismissible => _barrierDismissible ?? true; @override bool get semanticsDismissible => _semanticsDismissible ?? false; @@ -1012,6 +1017,13 @@ class _CupertinoModalPopupRoute extends PopupRoute { /// It is only used when the method is called. Its corresponding widget can be /// safely removed from the tree before the popup is closed. /// +/// The `barrierColor` argument determines the [Color] of the barrier underneath +/// the popup. When unspecified, the barrier color defaults to a light opacity +/// black scrim based on iOS's dialog screens. +/// +/// The `barrierDismissible` argument determines whether clicking outside the +/// popup results in dismissal. It is `true` by default. +/// /// The `useRootNavigator` argument is used to determine whether to push the /// popup to the [Navigator] furthest from or nearest to the given `context`. It /// is `false` by default. @@ -1038,13 +1050,16 @@ Future showCupertinoModalPopup({ required BuildContext context, required WidgetBuilder builder, ImageFilter? filter, + Color barrierColor = _kModalBarrierColor, + bool barrierDismissible = true, bool useRootNavigator = true, bool? semanticsDismissible, }) { assert(useRootNavigator != null); return Navigator.of(context, rootNavigator: useRootNavigator)!.push( _CupertinoModalPopupRoute( - barrierColor: CupertinoDynamicColor.resolve(_kModalBarrierColor, context), + barrierColor: CupertinoDynamicColor.resolve(barrierColor, context), + barrierDismissible: barrierDismissible, barrierLabel: 'Dismiss', builder: builder, filter: filter, diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index a2a2df3233..a86346d3d7 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -1375,6 +1375,137 @@ void main() { debugDefaultTargetPlatformOverride = null; }); + testWidgets('showCupertinoModalPopup transparent barrier color is transparent', (WidgetTester tester) async { + const Color _kTransparentColor = Color(0x00000000); + + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Builder(builder: (BuildContext context) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const SizedBox(), + barrierColor: _kTransparentColor, + ); + }, + child: const Text('tap'), + ); + }), + ), + )); + + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(ModalBarrier).last).color, null); + }); + + testWidgets('showCupertinoModalPopup null barrier color must be default gray barrier color', (WidgetTester tester) async { + // Barrier color for a Cupertino modal barrier. + // Extracted from https://developer.apple.com/design/resources/. + const Color kModalBarrierColor = CupertinoDynamicColor.withBrightness( + color: Color(0x33000000), + darkColor: Color(0x7A000000), + ); + + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Builder(builder: (BuildContext context) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const SizedBox(), + ); + }, + child: const Text('tap'), + ); + }), + ), + )); + + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(ModalBarrier).last).color, kModalBarrierColor); + }); + + testWidgets('showCupertinoModalPopup custom barrier color', (WidgetTester tester) async { + const Color customColor = Color(0x11223344); + + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Builder(builder: (BuildContext context) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const SizedBox(), + barrierColor: customColor); + }, + child: const Text('tap'), + ); + }), + ), + )); + + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(ModalBarrier).last).color, customColor); + }); + + testWidgets('showCupertinoModalPopup barrier dismissible', (WidgetTester tester) async { + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Builder(builder: (BuildContext context) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const Text('Visible'), + barrierDismissible: true); + }, + child: const Text('tap'), + ); + }), + ), + )); + + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + await tester.tapAt(tester.getTopLeft(find.ancestor(of: find.text('tap'), matching: find.byType(CupertinoPageScaffold)))); + await tester.pumpAndSettle(); + + expect(find.text('Visible'), findsNothing); + }); + + testWidgets('showCupertinoModalPopup barrier not dismissible', (WidgetTester tester) async { + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Builder(builder: (BuildContext context) { + return GestureDetector( + onTap: () async { + await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) => const Text('Visible'), + barrierDismissible: false); + }, + child: const Text('tap'), + ); + }), + ), + )); + + await tester.tap(find.text('tap')); + await tester.pumpAndSettle(); + await tester.tapAt(tester.getTopLeft(find.ancestor(of: find.text('tap'), matching: find.byType(CupertinoPageScaffold)))); + await tester.pumpAndSettle(); + + expect(find.text('Visible'), findsOneWidget); + }); + testWidgets('CupertinoPage works', (WidgetTester tester) async { final LocalKey pageKey = UniqueKey(); final TransitionDetector detector = TransitionDetector();