From 4d38f94e27d4c7691d8f4eb6cc9b068db666642d Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Wed, 3 May 2023 17:05:16 +0800 Subject: [PATCH] `ModalBarrier`'s gesture recognizer does not call `invokeCallback` (#125386) Close #125385 - please see explanations there :) If you think this PR looks OK, I will polish it (e.g. copy-and-paste-and-modify the tests shown in #125385 into here and make CI pass) --- .../lib/src/widgets/modal_barrier.dart | 4 +++- .../test/widgets/modal_barrier_test.dart | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index d38822a944..f831c2089d 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -394,7 +394,9 @@ class _AnyTapGestureRecognizer extends BaseTapGestureRecognizer { @protected @override void handleTapUp({PointerDownEvent? down, PointerUpEvent? up}) { - onAnyTapUp?.call(); + if (onAnyTapUp != null) { + invokeCallback('onAnyTapUp', onAnyTapUp!); + } } @protected diff --git a/packages/flutter/test/widgets/modal_barrier_test.dart b/packages/flutter/test/widgets/modal_barrier_test.dart index 08b8c08c2b..3d8ea7cc27 100644 --- a/packages/flutter/test/widgets/modal_barrier_test.dart +++ b/packages/flutter/test/widgets/modal_barrier_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart' show PointerDeviceKind, kSecondaryButton; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -399,6 +400,29 @@ void main() { expect(dismissCallbackCalled, true); }); + testWidgets('when onDismiss throws, should have correct context', (WidgetTester tester) async { + final FlutterExceptionHandler? handler = FlutterError.onError; + FlutterErrorDetails? error; + FlutterError.onError = (FlutterErrorDetails details) { + error = details; + }; + + final UniqueKey barrierKey = UniqueKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: ModalBarrier( + key: barrierKey, + onDismiss: () => throw Exception('deliberate'), + ), + ), + )); + await tester.tap(find.byKey(barrierKey)); + await tester.pump(); + + expect(error?.context.toString(), contains('handling a gesture')); + FlutterError.onError = handler; + }); + testWidgets('will not pop when given an onDismiss callback', (WidgetTester tester) async { final Map routes = { '/': (BuildContext context) => const FirstWidget(),