Skip to content

Commit 5c38125

Browse files
Wait for non-empty layout in platform view placeholder (#112402)
1 parent 4862a84 commit 5c38125

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

packages/flutter/lib/src/widgets/platform_view.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,10 +877,10 @@ class _PlatformViewLinkState extends State<PlatformViewLink> {
877877
return const SizedBox.expand();
878878
}
879879
if (!_platformViewCreated) {
880-
// Depending on the implementation, the initial size can be used to size
881-
// the platform view.
880+
// Depending on the implementation, the first non-empty size can be used
881+
// to size the platform view.
882882
return _PlatformViewPlaceHolder(onLayout: (Size size) {
883-
if (controller.awaitingCreation) {
883+
if (controller.awaitingCreation && !size.isEmpty) {
884884
controller.create(size: size);
885885
}
886886
});

packages/flutter/test/services/fake_platform_views.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ class FakeAndroidViewController implements AndroidViewController {
133133
@override
134134
Future<void> create({Size? size}) async {
135135
assert(!_createCalledSuccessfully);
136+
if (requiresSize && size != null) {
137+
assert(!size.isEmpty);
138+
}
136139
_createCalledSuccessfully = size != null || !requiresSize;
137140
}
138141

packages/flutter/test/widgets/platform_view_test.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,48 @@ void main() {
25022502
},
25032503
);
25042504

2505+
testWidgets(
2506+
'PlatformViewLink widget should not trigger creation with an empty size',
2507+
(WidgetTester tester) async {
2508+
late PlatformViewController controller;
2509+
2510+
final Widget widget = Center(child: SizedBox(
2511+
height: 0,
2512+
child: PlatformViewLink(
2513+
viewType: 'webview',
2514+
onCreatePlatformView: (PlatformViewCreationParams params) {
2515+
controller = FakeAndroidViewController(params.id, requiresSize: true);
2516+
controller.create();
2517+
// This test should be simulating one of the texture-based display
2518+
// modes, where `create` is a no-op when not provided a size, and
2519+
// creation is triggered via a later call to setSize, or to `create`
2520+
// with a size.
2521+
expect(controller.awaitingCreation, true);
2522+
return controller;
2523+
},
2524+
surfaceFactory: (BuildContext context, PlatformViewController controller) {
2525+
return PlatformViewSurface(
2526+
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
2527+
controller: controller,
2528+
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
2529+
);
2530+
},
2531+
)
2532+
));
2533+
2534+
await tester.pumpWidget(widget);
2535+
2536+
expect(
2537+
tester.allWidgets.map((Widget widget) => widget.runtimeType.toString()).toList(),
2538+
equals(<String>['Center', 'SizedBox', 'PlatformViewLink', '_PlatformViewPlaceHolder']),
2539+
);
2540+
2541+
// 'create' should not have been called by PlatformViewLink, since its
2542+
// size is empty.
2543+
expect(controller.awaitingCreation, true);
2544+
},
2545+
);
2546+
25052547
testWidgets(
25062548
'PlatformViewLink calls create when needed for Android texture display modes',
25072549
(WidgetTester tester) async {
@@ -2541,6 +2583,9 @@ void main() {
25412583
equals(<String>['PlatformViewLink', '_PlatformViewPlaceHolder']),
25422584
);
25432585

2586+
// Layout should have triggered a create call. Simulate the callback
2587+
// that the real controller would make after creation.
2588+
expect(controller.awaitingCreation, false);
25442589
onPlatformViewCreatedCallBack(createdPlatformViewId);
25452590

25462591
await tester.pump();
@@ -2551,7 +2596,6 @@ void main() {
25512596
);
25522597

25532598
expect(createdPlatformViewId, currentViewId + 1);
2554-
expect(controller.awaitingCreation, false);
25552599
},
25562600
);
25572601

0 commit comments

Comments
 (0)