Reland: "Use texture layer when displaying an Android view" (#100990)

This commit is contained in:
Emmanuel Garcia
2022-03-29 19:40:17 -07:00
committed by GitHub
parent f07f6fef9a
commit 032205eaca
12 changed files with 342 additions and 263 deletions

View File

@@ -16,7 +16,8 @@ found in the LICENSE file. -->
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:windowSoftInputMode="adjustResize"
android:exported="true">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
@@ -28,9 +29,5 @@ found in the LICENSE file. -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<!-- Hybrid composition -->
<meta-data
android:name="io.flutter.embedded_views_preview"
android:value="true" />
</application>
</manifest>

View File

@@ -17,6 +17,7 @@ class AndroidPlatformView extends StatelessWidget {
const AndroidPlatformView({
Key? key,
this.onPlatformViewCreated,
this.useHybridComposition = false,
required this.viewType,
}) : assert(viewType != null),
super(key: key);
@@ -31,6 +32,9 @@ class AndroidPlatformView extends StatelessWidget {
/// May be null.
final PlatformViewCreatedCallback? onPlatformViewCreated;
// Use hybrid composition.
final bool useHybridComposition;
@override
Widget build(BuildContext context) {
return PlatformViewLink(
@@ -44,17 +48,27 @@ class AndroidPlatformView extends StatelessWidget {
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
final AndroidViewController controller =
PlatformViewsService.initSurfaceAndroidView(
print('useHybridComposition=$useHybridComposition');
late AndroidViewController controller;
if (useHybridComposition) {
controller = PlatformViewsService.initExpensiveAndroidView(
id: params.id,
viewType: params.viewType,
layoutDirection: TextDirection.ltr,
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated);
);
} else {
controller = PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: params.viewType,
layoutDirection: TextDirection.ltr,
);
}
if (onPlatformViewCreated != null) {
controller.addOnPlatformViewCreatedListener(onPlatformViewCreated!);
}
return controller..create();
return controller
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
);
}

View File

@@ -39,6 +39,7 @@ class NestedViewEventBodyState extends State<NestedViewEventBody> {
int? id;
int nestedViewClickCount = 0;
bool showPlatformView = true;
bool useHybridComposition = false;
@override
Widget build(BuildContext context) {
@@ -55,39 +56,63 @@ class NestedViewEventBodyState extends State<NestedViewEventBody> {
key: const ValueKey<String>('PlatformView'),
viewType: 'simple_view',
onPlatformViewCreated: onPlatformViewCreated,
useHybridComposition: useHybridComposition,
) : null,
),
if (_lastTestStatus != _LastTestStatus.pending) _statusWidget(),
if (viewChannel != null) ... <Widget>[
ElevatedButton(
key: const ValueKey<String>('ShowAlertDialog'),
onPressed: onShowAlertDialogPressed,
child: const Text('SHOW ALERT DIALOG'),
),
ElevatedButton(
key: const ValueKey<String>('TogglePlatformView'),
onPressed: onTogglePlatformView,
child: const Text('TOGGLE PLATFORM VIEW'),
Row(
children: <Widget>[
Expanded(
child: ElevatedButton(
key: const ValueKey<String>('ShowAlertDialog'),
onPressed: onShowAlertDialogPressed,
child: const Text('SHOW ALERT DIALOG'),
),
),
Expanded(
child: ElevatedButton(
key: const ValueKey<String>('TogglePlatformView'),
onPressed: onTogglePlatformView,
child: const Text('TOGGLE PLATFORM VIEW'),
),
),
],
),
Row(
children: <Widget>[
ElevatedButton(
key: const ValueKey<String>('AddChildView'),
onPressed: onChildViewPressed,
child: const Text('ADD CHILD VIEW'),
),
ElevatedButton(
key: const ValueKey<String>('TapChildView'),
onPressed: onTapChildViewPressed,
child: const Text('TAP CHILD VIEW'),
),
if (nestedViewClickCount > 0)
Text(
'Click count: $nestedViewClickCount',
key: const ValueKey<String>('NestedViewClickCount'),
Expanded(
child: ElevatedButton(
key: const ValueKey<String>('ToggleHybridComposition'),
child: const Text('TOGGLE HC'),
onPressed: () {
setState(() {
useHybridComposition = !useHybridComposition;
});
},
),
),
Expanded(
child: ElevatedButton(
key: const ValueKey<String>('AddChildView'),
onPressed: onChildViewPressed,
child: const Text('ADD CHILD VIEW'),
),
),
Expanded(
child: ElevatedButton(
key: const ValueKey<String>('TapChildView'),
onPressed: onTapChildViewPressed,
child: const Text('TAP CHILD VIEW'),
),
),
],
),
if (nestedViewClickCount > 0)
Text(
'Click count: $nestedViewClickCount',
key: const ValueKey<String>('NestedViewClickCount'),
),
],
],
),

View File

@@ -59,10 +59,58 @@ Future<void> main() async {
}, timeout: Timeout.none);
});
group('Flutter surface switch', () {
group('Flutter surface without hybrid composition', () {
setUpAll(() async {
final SerializableFinder wmListTile = find.byValueKey('NestedViewEventTile');
await driver.tap(wmListTile);
await driver.tap(find.byValueKey('NestedViewEventTile'));
});
tearDownAll(() async {
await driver.waitFor(find.pageBack());
await driver.tap(find.pageBack());
});
test('Uses FlutterSurfaceView when Android view is on the screen', () async {
await driver.waitFor(find.byValueKey('PlatformView'));
expect(
await driver.requestData('hierarchy'),
'|-FlutterView\n'
' |-FlutterSurfaceView\n' // Flutter UI
' |-ViewGroup\n' // Platform View
' |-ViewGroup\n'
);
// Hide platform view.
final SerializableFinder togglePlatformView = find.byValueKey('TogglePlatformView');
await driver.tap(togglePlatformView);
await driver.waitForAbsent(find.byValueKey('PlatformView'));
expect(
await driver.requestData('hierarchy'),
'|-FlutterView\n'
' |-FlutterSurfaceView\n' // Just the Flutter UI
);
// Show platform view again.
await driver.tap(togglePlatformView);
await driver.waitFor(find.byValueKey('PlatformView'));
expect(
await driver.requestData('hierarchy'),
'|-FlutterView\n'
' |-FlutterSurfaceView\n' // Flutter UI
' |-ViewGroup\n' // Platform View
' |-ViewGroup\n'
);
}, timeout: Timeout.none);
});
group('Flutter surface with hybrid composition', () {
setUpAll(() async {
await driver.tap(find.byValueKey('NestedViewEventTile'));
await driver.tap(find.byValueKey('ToggleHybridComposition'));
await driver.tap(find.byValueKey('TogglePlatformView'));
await driver.tap(find.byValueKey('TogglePlatformView'));
});
tearDownAll(() async {