Skip to content

Commit 6928314

Browse files
stuartmorgan-gitsjustkevingodofredoc
authored
Wait for non-empty layout in platform view placeholder (flutter#112402) (flutter#113040)
Co-authored-by: Kevin Chisholm <kevinjchisholm@google.com> Co-authored-by: godofredoc <godofredoc@google.com>
1 parent d9111f6 commit 6928314

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
@@ -869,10 +869,10 @@ class _PlatformViewLinkState extends State<PlatformViewLink> {
869869
return const SizedBox.expand();
870870
}
871871
if (!_platformViewCreated) {
872-
// Depending on the implementation, the initial size can be used to size
873-
// the platform view.
872+
// Depending on the implementation, the first non-empty size can be used
873+
// to size the platform view.
874874
return _PlatformViewPlaceHolder(onLayout: (Size size) {
875-
if (controller.awaitingCreation) {
875+
if (controller.awaitingCreation && !size.isEmpty) {
876876
controller.create(size: size);
877877
}
878878
});

packages/flutter/test/services/fake_platform_views.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class FakeAndroidViewController implements AndroidViewController {
124124
@override
125125
Future<void> create({Size? size}) async {
126126
assert(!_createCalledSuccessfully);
127+
if (requiresSize && size != null) {
128+
assert(!size.isEmpty);
129+
}
127130
_createCalledSuccessfully = size != null || !requiresSize;
128131
}
129132

packages/flutter/test/widgets/platform_view_test.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,48 @@ void main() {
24552455
},
24562456
);
24572457

2458+
testWidgets(
2459+
'PlatformViewLink widget should not trigger creation with an empty size',
2460+
(WidgetTester tester) async {
2461+
late PlatformViewController controller;
2462+
2463+
final Widget widget = Center(child: SizedBox(
2464+
height: 0,
2465+
child: PlatformViewLink(
2466+
viewType: 'webview',
2467+
onCreatePlatformView: (PlatformViewCreationParams params) {
2468+
controller = FakeAndroidViewController(params.id, requiresSize: true);
2469+
controller.create();
2470+
// This test should be simulating one of the texture-based display
2471+
// modes, where `create` is a no-op when not provided a size, and
2472+
// creation is triggered via a later call to setSize, or to `create`
2473+
// with a size.
2474+
expect(controller.awaitingCreation, true);
2475+
return controller;
2476+
},
2477+
surfaceFactory: (BuildContext context, PlatformViewController controller) {
2478+
return PlatformViewSurface(
2479+
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
2480+
controller: controller,
2481+
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
2482+
);
2483+
},
2484+
)
2485+
));
2486+
2487+
await tester.pumpWidget(widget);
2488+
2489+
expect(
2490+
tester.allWidgets.map((Widget widget) => widget.runtimeType.toString()).toList(),
2491+
equals(<String>['Center', 'SizedBox', 'PlatformViewLink', '_PlatformViewPlaceHolder']),
2492+
);
2493+
2494+
// 'create' should not have been called by PlatformViewLink, since its
2495+
// size is empty.
2496+
expect(controller.awaitingCreation, true);
2497+
},
2498+
);
2499+
24582500
testWidgets(
24592501
'PlatformViewLink calls create when needed for Android texture display modes',
24602502
(WidgetTester tester) async {
@@ -2494,6 +2536,9 @@ void main() {
24942536
equals(<String>['PlatformViewLink', '_PlatformViewPlaceHolder']),
24952537
);
24962538

2539+
// Layout should have triggered a create call. Simulate the callback
2540+
// that the real controller would make after creation.
2541+
expect(controller.awaitingCreation, false);
24972542
onPlatformViewCreatedCallBack(createdPlatformViewId);
24982543

24992544
await tester.pump();
@@ -2504,7 +2549,6 @@ void main() {
25042549
);
25052550

25062551
expect(createdPlatformViewId, currentViewId + 1);
2507-
expect(controller.awaitingCreation, false);
25082552
},
25092553
);
25102554

0 commit comments

Comments
 (0)