diff --git a/packages/flutter/lib/src/services/platform_views.dart b/packages/flutter/lib/src/services/platform_views.dart index 7e8b4a93ab..6b8ca3193c 100644 --- a/packages/flutter/lib/src/services/platform_views.dart +++ b/packages/flutter/lib/src/services/platform_views.dart @@ -583,6 +583,14 @@ class AndroidViewController { }); } + /// Clears the focus from the Android View if it is focused. + Future clearFocus() { + if (_state != _AndroidViewState.created) { + return null; + } + return SystemChannels.platform_views.invokeMethod('clearFocus', id); + } + static int _getAndroidDirection(TextDirection direction) { assert(direction != null); switch (direction) { diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index ad92a27a73..3392a6551b 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -306,6 +306,7 @@ class _AndroidViewState extends State { Widget build(BuildContext context) { return Focus( focusNode: _focusNode, + onFocusChange: _onFocusChange, child: _AndroidPlatformView( controller: _controller, hitTestBehavior: widget.hitTestBehavior, @@ -384,6 +385,40 @@ class _AndroidViewState extends State { _controller.addOnPlatformViewCreatedListener(widget.onPlatformViewCreated); } } + + void _onFocusChange(bool isFocused) { + if (!_controller.isCreated) { + return; + } + if (!isFocused) { + _controller.clearFocus().catchError((dynamic e) { + if (e is MissingPluginException) { + // We land the framework part of Android platform views keyboard + // support before the engine part. There will be a commit range where + // clearFocus isn't implemented in the engine. When that happens we + // just swallow the error here. Once the engine part is rolled to the + // framework I'll remove this. + // TODO(amirh): remove this once the engine's clearFocus is rolled. + return; + } + }); + return; + } + SystemChannels.textInput.invokeMethod( + 'TextInput.setPlatformViewClient', + _id, + ).catchError((dynamic e) { + if (e is MissingPluginException) { + // We land the framework part of Android platform views keyboard + // support before the engine part. There will be a commit range where + // setPlatformViewClient isn't implemented in the engine. When that + // happens we just swallow the error here. Once the engine part is + // rolled to the framework I'll remove this. + // TODO(amirh): remove this once the engine's clearFocus is rolled. + return; + } + }); + } } class _UiKitViewState extends State { diff --git a/packages/flutter/test/services/fake_platform_views.dart b/packages/flutter/test/services/fake_platform_views.dart index b066348ed4..bc49eae142 100644 --- a/packages/flutter/test/services/fake_platform_views.dart +++ b/packages/flutter/test/services/fake_platform_views.dart @@ -28,6 +28,8 @@ class FakeAndroidPlatformViewsController { Completer createCompleter; + int lastClearedFocusViewId; + void registerViewType(String viewType) { _registeredViewTypes.add(viewType); } @@ -50,6 +52,8 @@ class FakeAndroidPlatformViewsController { return _touch(call); case 'setDirection': return _setDirection(call); + case 'clearFocus': + return _clearFocus(call); } return Future.sync(() => null); } @@ -154,6 +158,19 @@ class FakeAndroidPlatformViewsController { return Future.sync(() => null); } + + Future _clearFocus(MethodCall call) { + final int id = call.arguments; + + if (!_views.containsKey(id)) + throw PlatformException( + code: 'error', + message: 'Trying to clear the focus on a platform view with unknown id: $id', + ); + + lastClearedFocusViewId = id; + return Future.sync(() => null); + } } class FakeIosPlatformViewsController { diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index 79eaf42013..31da4f615c 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -908,6 +908,100 @@ void main() { expect(containerFocusNode.hasFocus, isFalse); expect(androidViewFocusNode.hasFocus, isTrue); }); + + testWidgets('AndroidView sets a platform view text input client when focused', (WidgetTester tester) async { + final int currentViewId = platformViewsRegistry.getNextPlatformViewId(); + final FakeAndroidPlatformViewsController viewsController = FakeAndroidPlatformViewsController(); + viewsController.registerViewType('webview'); + + viewsController.createCompleter = Completer(); + + final GlobalKey containerKey = GlobalKey(); + await tester.pumpWidget( + Center( + child: Column( + children: [ + const SizedBox( + width: 200.0, + height: 100.0, + child: AndroidView(viewType: 'webview', layoutDirection: TextDirection.ltr), + ), + Focus( + debugLabel: 'container', + child: Container(key: containerKey), + ), + ], + ), + ), + ); + + viewsController.createCompleter.complete(); + + + final Element containerElement = tester.element(find.byKey(containerKey)); + final FocusNode containerFocusNode = Focus.of(containerElement); + + containerFocusNode.requestFocus(); + await tester.pump(); + + int lastPlatformViewTextClient; + SystemChannels.textInput.setMockMethodCallHandler((MethodCall call) { + if (call.method == 'TextInput.setPlatformViewClient') { + lastPlatformViewTextClient = call.arguments; + } + return null; + }); + + viewsController.invokeViewFocused(currentViewId + 1); + await tester.pump(); + + expect(lastPlatformViewTextClient, currentViewId + 1); + }); + + testWidgets('AndroidView clears platform focus when unfocused', (WidgetTester tester) async { + final int currentViewId = platformViewsRegistry.getNextPlatformViewId(); + final FakeAndroidPlatformViewsController viewsController = FakeAndroidPlatformViewsController(); + viewsController.registerViewType('webview'); + + viewsController.createCompleter = Completer(); + + final GlobalKey containerKey = GlobalKey(); + await tester.pumpWidget( + Center( + child: Column( + children: [ + const SizedBox( + width: 200.0, + height: 100.0, + child: AndroidView(viewType: 'webview', layoutDirection: TextDirection.ltr), + ), + Focus( + debugLabel: 'container', + child: Container(key: containerKey), + ), + ], + ), + ), + ); + + viewsController.createCompleter.complete(); + + final Element containerElement = tester.element(find.byKey(containerKey)); + final FocusNode containerFocusNode = Focus.of(containerElement); + + containerFocusNode.requestFocus(); + await tester.pump(); + + viewsController.invokeViewFocused(currentViewId + 1); + await tester.pump(); + + viewsController.lastClearedFocusViewId = null; + + containerFocusNode.requestFocus(); + await tester.pump(); + + expect(viewsController.lastClearedFocusViewId, currentViewId + 1); + }); }); group('UiKitView', () {