From a9b8796a602c5e66ac1e76c25d168a65a95f8c06 Mon Sep 17 00:00:00 2001 From: Stefano Rodriguez Date: Thu, 1 Nov 2018 17:45:13 +0100 Subject: [PATCH] Use maybePop in place of pop in ModalBarrier dismiss interaction (#22023) * Use maybePop in place of pop in ModalBarrier --- .../lib/src/widgets/modal_barrier.dart | 2 +- .../test/widgets/modal_barrier_test.dart | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index fca9f7cc8e..17750b2301 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -83,7 +83,7 @@ class ModalBarrier extends StatelessWidget { child: GestureDetector( onTapDown: (TapDownDetails details) { if (dismissible) - Navigator.pop(context); + Navigator.maybePop(context); }, behavior: HitTestBehavior.opaque, child: Semantics( diff --git a/packages/flutter/test/widgets/modal_barrier_test.dart b/packages/flutter/test/widgets/modal_barrier_test.dart index e51052fed7..725043cb4b 100644 --- a/packages/flutter/test/widgets/modal_barrier_test.dart +++ b/packages/flutter/test/widgets/modal_barrier_test.dart @@ -85,6 +85,86 @@ void main() { reason: 'The route should have been dismissed by tapping the barrier.'); }); + testWidgets('ModalBarrier does not pop the Navigator with a WillPopScope that returns false', (WidgetTester tester) async { + bool willPopCalled = false; + final Map routes = { + '/': (BuildContext context) => FirstWidget(), + '/modal': (BuildContext context) => Stack( + children: [ + SecondWidget(), + WillPopScope( + child: const SizedBox(), + onWillPop: () async { + willPopCalled = true; + return false; + }, + ), + ],), + }; + + await tester.pumpWidget(MaterialApp(routes: routes)); + + // Initially the barrier is not visible + expect(find.byKey(const ValueKey('barrier')), findsNothing); + + // Tapping on X routes to the barrier + await tester.tap(find.text('X')); + await tester.pump(); // begin transition + await tester.pump(const Duration(seconds: 1)); // end transition + + expect(willPopCalled, isFalse); + + // Tap on the barrier to attempt to dismiss it + await tester.tap(find.byKey(const ValueKey('barrier'))); + await tester.pump(); // begin transition + await tester.pump(const Duration(seconds: 1)); // end transition + + expect(find.byKey(const ValueKey('barrier')), findsOneWidget, + reason: 'The route should still be present if the pop is vetoed.'); + + expect(willPopCalled, isTrue); + }); + + testWidgets('ModalBarrier pops the Navigator with a WillPopScope that returns true', (WidgetTester tester) async { + bool willPopCalled = false; + final Map routes = { + '/': (BuildContext context) => FirstWidget(), + '/modal': (BuildContext context) => Stack( + children: [ + SecondWidget(), + WillPopScope( + child: const SizedBox(), + onWillPop: () async { + willPopCalled = true; + return true; + }, + ), + ],), + }; + + await tester.pumpWidget(MaterialApp(routes: routes)); + + // Initially the barrier is not visible + expect(find.byKey(const ValueKey('barrier')), findsNothing); + + // Tapping on X routes to the barrier + await tester.tap(find.text('X')); + await tester.pump(); // begin transition + await tester.pump(const Duration(seconds: 1)); // end transition + + expect(willPopCalled, isFalse); + + // Tap on the barrier to attempt to dismiss it + await tester.tap(find.byKey(const ValueKey('barrier'))); + await tester.pump(); // begin transition + await tester.pump(const Duration(seconds: 1)); // end transition + + expect(find.byKey(const ValueKey('barrier')), findsNothing, + reason: 'The route should not be present if the pop is permitted.'); + + expect(willPopCalled, isTrue); + }); + testWidgets('Undismissible ModalBarrier hidden in semantic tree', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); await tester.pumpWidget(const ModalBarrier(dismissible: false));