diff --git a/packages/flutter/lib/src/rendering/platform_view.dart b/packages/flutter/lib/src/rendering/platform_view.dart index 593fbc0db4..438b2c76b0 100644 --- a/packages/flutter/lib/src/rendering/platform_view.dart +++ b/packages/flutter/lib/src/rendering/platform_view.dart @@ -412,11 +412,17 @@ class _UiKitViewGestureRecognizer extends OneSequenceGestureRecognizer { @override void rejectGesture(int pointer) { - // Currently the engine rejects the gesture when the sequence is done. - // This doesn't work well with gesture recognizers that recognize after the sequence - // has ended. - // TODO(amirh): trigger an engine gesture reject here. - // https://github.com/flutter/flutter/issues/24076 + controller.rejectGesture().catchError((dynamic e) { + if (e is MissingPluginException) { + // We land the framework part of active gesture rejection before the engine part. + // There will be some commit range where we call rejectGesture from the framework and it + // isn't implemented in the engine, if that is the case we swallow the MissingPluginException. + // Once the engine support lands we will remove the enclosing catchError call. + // TODO(amirh): remove this once the engine supports active gesture rejection. + return; + } + throw e; + }); } void reset() { diff --git a/packages/flutter/lib/src/services/platform_views.dart b/packages/flutter/lib/src/services/platform_views.dart index 9da30c85bf..99985b8d33 100644 --- a/packages/flutter/lib/src/services/platform_views.dart +++ b/packages/flutter/lib/src/services/platform_views.dart @@ -630,6 +630,18 @@ class UiKitViewController { return SystemChannels.platform_views.invokeMethod('acceptGesture', args); } + /// Rejects an active gesture. + /// + /// When a touch sequence is happening on the embedded UIView all touch events are delayed. + /// Calling this method drops the buffered touch events and prevents any future touch events for + /// the pointers that are part of the active touch sequence from arriving to the embedded view. + Future rejectGesture() { + final Map args = { + 'id': id, + }; + return SystemChannels.platform_views.invokeMethod('rejectGesture', args); + } + /// Disposes the view. /// /// The [UiKitViewController] object is unusable after calling this. diff --git a/packages/flutter/test/services/fake_platform_views.dart b/packages/flutter/test/services/fake_platform_views.dart index 6a0dd12c3f..fcea5dbe9c 100644 --- a/packages/flutter/test/services/fake_platform_views.dart +++ b/packages/flutter/test/services/fake_platform_views.dart @@ -159,9 +159,12 @@ class FakeIosPlatformViewsController { // delayed until it completes. Completer creationDelay; - // Maps a view id to the number of gestures it accepted so fat. + // Maps a view id to the number of gestures it accepted so far. final Map gesturesAccepted = {}; + // Maps a view id to the number of gestures it rejected so far. + final Map gesturesRejected = {}; + void registerViewType(String viewType) { _registeredViewTypes.add(viewType); } @@ -174,6 +177,8 @@ class FakeIosPlatformViewsController { return _dispose(call); case 'acceptGesture': return _acceptGesture(call); + case 'rejectGesture': + return _rejectGesture(call); } return Future.sync(() => null); } @@ -202,6 +207,7 @@ class FakeIosPlatformViewsController { _views[id] = FakeUiKitView(id, viewType, creationParams); gesturesAccepted[id] = 0; + gesturesRejected[id] = 0; return Future.sync(() => null); } @@ -212,6 +218,13 @@ class FakeIosPlatformViewsController { return Future.sync(() => null); } + Future _rejectGesture(MethodCall call) async { + final Map args = call.arguments; + final int id = args['id']; + gesturesRejected[id] += 1; + return Future.sync(() => null); + } + Future _dispose(MethodCall call) { final int id = call.arguments; diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index 973b8dfad2..be06681108 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -1196,6 +1196,7 @@ void main() { expect(verticalDragAcceptedByParent, true); expect(viewsController.gesturesAccepted[currentViewId + 1], 0); + expect(viewsController.gesturesRejected[currentViewId + 1], 1); }); testWidgets('UiKitView gesture recognizers', (WidgetTester tester) async { @@ -1237,6 +1238,7 @@ void main() { expect(verticalDragAcceptedByParent, false); expect(viewsController.gesturesAccepted[currentViewId + 1], 1); + expect(viewsController.gesturesRejected[currentViewId + 1], 0); }); testWidgets('UiKitView can claim gesture after all pointers are up', (WidgetTester tester) async { @@ -1276,6 +1278,7 @@ void main() { expect(verticalDragAcceptedByParent, false); expect(viewsController.gesturesAccepted[currentViewId + 1], 1); + expect(viewsController.gesturesRejected[currentViewId + 1], 0); }); testWidgets('UiKitView rebuilt during gesture', (WidgetTester tester) async { @@ -1320,6 +1323,7 @@ void main() { await gesture.up(); expect(viewsController.gesturesAccepted[currentViewId + 1], 1); + expect(viewsController.gesturesRejected[currentViewId + 1], 0); }); testWidgets('UiKitView with eager gesture recognizer', (WidgetTester tester) async { @@ -1359,6 +1363,7 @@ void main() { // the Android view). Here we assert that with the eager recognizer in the gesture team the // pointer down event is immediately dispatched. expect(viewsController.gesturesAccepted[currentViewId + 1], 1); + expect(viewsController.gesturesRejected[currentViewId + 1], 0); }); testWidgets('AndroidView rebuilt with same gestureRecognizers', (WidgetTester tester) async {