From d1b8527f6b312a365cd708d1f2a5c5de645548eb Mon Sep 17 00:00:00 2001 From: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com> Date: Tue, 4 Apr 2023 20:56:51 -0700 Subject: [PATCH] [platform_view]fix iOS platform view's focus node leakage (#124066) [platform_view]fix iOS platform view's focus node leakage --- .../lib/src/services/platform_views.dart | 1 + .../lib/src/widgets/platform_view.dart | 17 ++++++++--- .../test/widgets/platform_view_test.dart | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/services/platform_views.dart b/packages/flutter/lib/src/services/platform_views.dart index 3871db5f23..5c5c0a6ada 100644 --- a/packages/flutter/lib/src/services/platform_views.dart +++ b/packages/flutter/lib/src/services/platform_views.dart @@ -1378,6 +1378,7 @@ class UiKitViewController { Future dispose() async { _debugDisposed = true; await SystemChannels.platform_views.invokeMethod('dispose', id); + PlatformViewsService._instance._focusCallbacks.remove(id); } } diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index 9288ee14d6..55653d2af2 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -503,6 +503,8 @@ class _AndroidViewState extends State { @override void dispose() { _controller.dispose(); + _focusNode?.dispose(); + _focusNode = null; super.dispose(); } @@ -562,7 +564,9 @@ class _UiKitViewState extends State { UiKitViewController? _controller; TextDirection? _layoutDirection; bool _initialized = false; - late FocusNode _focusNode; + + @visibleForTesting + FocusNode? focusNode; static final Set> _emptyRecognizersSet = >{}; @@ -574,7 +578,7 @@ class _UiKitViewState extends State { return const SizedBox.expand(); } return Focus( - focusNode: _focusNode, + focusNode: focusNode, onFocusChange: (bool isFocused) => _onFocusChange(isFocused, controller), child: _UiKitPlatformView( controller: _controller!, @@ -634,6 +638,9 @@ class _UiKitViewState extends State { @override void dispose() { _controller?.dispose(); + _controller = null; + focusNode?.dispose(); + focusNode = null; super.dispose(); } @@ -646,7 +653,7 @@ class _UiKitViewState extends State { creationParams: widget.creationParams, creationParamsCodec: widget.creationParamsCodec, onFocus: () { - _focusNode.requestFocus(); + focusNode?.requestFocus(); } ); if (!mounted) { @@ -656,7 +663,7 @@ class _UiKitViewState extends State { widget.onPlatformViewCreated?.call(id); setState(() { _controller = controller; - _focusNode = FocusNode(debugLabel: 'UiKitView(id: $id)'); + focusNode = FocusNode(debugLabel: 'UiKitView(id: $id)'); }); } @@ -938,6 +945,8 @@ class _PlatformViewLinkState extends State { void dispose() { _controller?.dispose(); _controller = null; + _focusNode?.dispose(); + _focusNode = null; super.dispose(); } } diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index 604f067af3..1016909fea 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -2150,6 +2150,34 @@ void main() { expect(channelArguments['platformViewId'], currentViewId + 1); }); + testWidgets('FocusNode is disposed on UIView dispose', (WidgetTester tester) async { + final FakeIosPlatformViewsController viewsController = FakeIosPlatformViewsController(); + viewsController.registerViewType('webview'); + + await tester.pumpWidget( + const Center( + child: SizedBox( + width: 200.0, + height: 100.0, + child: UiKitView(viewType: 'webview', layoutDirection: TextDirection.ltr), + ), + ), + ); + // casting to dynamic is required since the state class is private. + // ignore: avoid_dynamic_calls, invalid_assignment + final FocusNode node = (tester.state(find.byType(UiKitView)) as dynamic).focusNode; + expect(() => ChangeNotifier.debugAssertNotDisposed(node), isNot(throwsAssertionError)); + await tester.pumpWidget( + const Center( + child: SizedBox( + width: 200.0, + height: 100.0, + ), + ), + ); + expect(() => ChangeNotifier.debugAssertNotDisposed(node), throwsAssertionError); + }); + testWidgets('UiKitView has correct semantics', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics(); final int currentViewId = platformViewsRegistry.getNextPlatformViewId();