[Android] Remove overlay when platform views are removed from screen. (#162908)

When there are no more platform views, make sure the overlay layer is
hidden. When a platform view is added again, show the overlay. The
show/hide allows us to avoid continually recreating and destroying the
overlay surface + swapchain when a platform view slides in and out of
frame.

To further reduce memory usage, we could do a delayed de-allocation of
the overlay layer (say after 50 frames of no platform view, destroy it).
But I'm leaving this to a follow up.
This commit is contained in:
Jonah Williams
2025-02-10 18:29:07 -08:00
committed by GitHub
parent 2d39a739ea
commit 7cd9e0f640
14 changed files with 191 additions and 32 deletions

View File

@@ -12,6 +12,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_driver/driver_extension.dart';
import '../platform_view/_shared.dart';
import '../src/allow_list_devices.dart';
void main() async {
@@ -27,32 +28,11 @@ void main() async {
// Run on full screen.
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
runApp(const MainApp());
}
final class MainApp extends StatelessWidget {
const MainApp({super.key});
// This should appear as the yellow line over a blue box. The
// green box should not be visible unless the platform view has not loaded yet.
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
SizedBox(width: 190, height: 190, child: ColoredBox(color: Colors.green)),
SizedBox(
width: 200,
height: 200,
child: _HybridCompositionAndroidPlatformView(viewType: 'box_platform_view'),
),
SizedBox(width: 800, height: 25, child: ColoredBox(color: Colors.yellow)),
],
),
);
}
runApp(
const MainApp(
platformView: _HybridCompositionAndroidPlatformView(viewType: 'box_platform_view'),
),
);
}
final class _HybridCompositionAndroidPlatformView extends StatelessWidget {
@@ -60,7 +40,6 @@ final class _HybridCompositionAndroidPlatformView extends StatelessWidget {
final String viewType;
// TODO(jonahwilliams): swap this out with new platform view APIs.
@override
Widget build(BuildContext context) {
return PlatformViewLink(

View File

@@ -4,18 +4,50 @@
import 'package:flutter/material.dart';
final class MainApp extends StatelessWidget {
// This should appear as the yellow line over a blue box. The
// green box should not be visible unless the platform view has not loaded yet.
final class MainApp extends StatefulWidget {
const MainApp({super.key, required this.platformView});
final Widget platformView;
@override
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
bool showPlatformView = true;
void _togglePlatformView() {
setState(() {
showPlatformView = !showPlatformView;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
platformView,
Center(child: Container(width: 100, height: 100, color: Colors.red)),
TextButton(
key: const ValueKey<String>('AddOverlay'),
onPressed: _togglePlatformView,
child: const SizedBox(width: 190, height: 190, child: ColoredBox(color: Colors.green)),
),
if (showPlatformView) ...<Widget>[
SizedBox(width: 200, height: 200, child: widget.platformView),
TextButton(
key: const ValueKey<String>('RemoveOverlay'),
onPressed: _togglePlatformView,
child: const SizedBox(
width: 800,
height: 25,
child: ColoredBox(color: Colors.yellow),
),
),
],
],
),
);

View File

@@ -41,6 +41,8 @@ void main() async {
});
tearDownAll(() async {
await flutterDriver.tap(find.byValueKey('AddOverlay'));
await nativeDriver.close();
await flutterDriver.close();
});
@@ -72,4 +74,16 @@ void main() async {
matchesGoldenFile('$goldenPrefix.platform_view_portait_rotated_back.png'),
);
}, timeout: Timeout.none);
// Note: this doesn't reset the app so if additional test cases are added
// make sure to press the button again.
test('should remove overlay when platform view is removed', () async {
await flutterDriver.tap(find.byValueKey('RemoveOverlay'));
await Future<void>.delayed(const Duration(seconds: 1));
await expectLater(
nativeDriver.screenshot(),
matchesGoldenFile('$goldenPrefix.removed_overlay.png'),
);
}, timeout: Timeout.none);
}

View File

@@ -45,6 +45,8 @@ void main() async {
});
tearDownAll(() async {
await flutterDriver.tap(find.byValueKey('AddOverlay'));
await nativeDriver.close();
await flutterDriver.close();
});
@@ -69,4 +71,14 @@ void main() async {
matchesGoldenFile('$goldenPrefix.blue_orange_gradient_portait_rotated_back.png'),
);
}, timeout: Timeout.none);
test('should hide overlay layer', () async {
await flutterDriver.tap(find.byValueKey('RemoveOverlay'));
await Future<void>.delayed(const Duration(seconds: 1));
await expectLater(
nativeDriver.screenshot(),
matchesGoldenFile('$goldenPrefix.hide_overlay.png'),
);
}, timeout: Timeout.none);
}

View File

@@ -56,6 +56,8 @@ void main() async {
});
tearDownAll(() async {
await flutterDriver.tap(find.byValueKey('AddOverlay'));
await nativeDriver.close();
await flutterDriver.close();
});
@@ -86,4 +88,14 @@ void main() async {
),
);
}, timeout: Timeout.none);
test('should hide overlay layer', () async {
await flutterDriver.tap(find.byValueKey('RemoveOverlay'));
await Future<void>.delayed(const Duration(seconds: 1));
await expectLater(
nativeDriver.screenshot(),
matchesGoldenFile('$goldenPrefix.hide_overlay.png'),
);
}, timeout: Timeout.none);
}