1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/foundation.dart' ; |
6 | import 'package:flutter/gestures.dart' ; |
7 | import 'package:flutter/material.dart' ; |
8 | import 'package:flutter/rendering.dart' ; |
9 | import 'package:flutter/services.dart' ; |
10 | |
11 | MethodChannel channel = const MethodChannel('android_views_integration' ); |
12 | |
13 | class AndroidPlatformView extends StatelessWidget { |
14 | /// Creates a platform view for Android, which is rendered as a |
15 | /// native view. |
16 | /// `viewType` identifies the type of Android view to create. |
17 | const AndroidPlatformView({ |
18 | super.key, |
19 | this.onPlatformViewCreated, |
20 | this.useHybridComposition = false, |
21 | required this.viewType, |
22 | }); |
23 | |
24 | /// The unique identifier for the view type to be embedded by this widget. |
25 | /// |
26 | /// A PlatformViewFactory for this type must have been registered. |
27 | final String viewType; |
28 | |
29 | /// Callback to invoke after the platform view has been created. |
30 | /// |
31 | /// May be null. |
32 | final PlatformViewCreatedCallback? onPlatformViewCreated; |
33 | |
34 | // Use hybrid composition. |
35 | final bool useHybridComposition; |
36 | |
37 | @override |
38 | Widget build(BuildContext context) { |
39 | return PlatformViewLink( |
40 | viewType: viewType, |
41 | surfaceFactory: |
42 | (BuildContext context, PlatformViewController controller) { |
43 | return AndroidViewSurface( |
44 | controller: controller as AndroidViewController, |
45 | gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{}, |
46 | hitTestBehavior: PlatformViewHitTestBehavior.opaque, |
47 | ); |
48 | }, |
49 | onCreatePlatformView: (PlatformViewCreationParams params) { |
50 | print('useHybridComposition= $useHybridComposition' ); |
51 | late AndroidViewController controller; |
52 | if (useHybridComposition) { |
53 | controller = PlatformViewsService.initExpensiveAndroidView( |
54 | id: params.id, |
55 | viewType: params.viewType, |
56 | layoutDirection: TextDirection.ltr, |
57 | ); |
58 | } else { |
59 | controller = PlatformViewsService.initSurfaceAndroidView( |
60 | id: params.id, |
61 | viewType: params.viewType, |
62 | layoutDirection: TextDirection.ltr, |
63 | ); |
64 | } |
65 | if (onPlatformViewCreated != null) { |
66 | controller.addOnPlatformViewCreatedListener(onPlatformViewCreated!); |
67 | } |
68 | return controller |
69 | ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) |
70 | ..create(); |
71 | }, |
72 | ); |
73 | } |
74 | } |
75 | |