-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Add new entrypoint that uses hybrid composition on Android #2883
Changes from 10 commits
e8f8ad8
6a5927d
8e90cc3
da2a0b9
bdd7f2e
1cef7c0
24533ed
29d2b88
7c30a7e
6185138
c82be0a
28faa1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,14 @@ import 'dart:async'; | |
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'platform_interface.dart'; | ||
import 'src/webview_android.dart'; | ||
import 'src/webview_cupertino.dart'; | ||
import 'src/webview_method_channel.dart'; | ||
|
||
/// Optional callback invoked when a web view is first created. [controller] is | ||
/// the [WebViewController] for the created web view. | ||
|
@@ -64,6 +67,66 @@ enum NavigationDecision { | |
navigate, | ||
} | ||
|
||
/// Android [WebViewPlatform] that uses [AndroidViewSurface] to build the [WebView] widget. | ||
/// | ||
/// To use this, set [WebView.platform] to an instance of this class. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some guidance on when should one use it and what are the costs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. I added it below. @blasten What are your thoughts on the explanation below? |
||
/// | ||
/// This implementation uses Hybrid Composition to render the [WebView] on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hybrid Composition -> hybrid composition |
||
/// Android. It solves multiple issues related to accessibility and interaction | ||
/// with the [WebView] at the cost of some performance on Android versions below | ||
/// 10. See https://github.com/flutter/flutter/issues/61133 for more | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// information. | ||
class SurfaceAndroidWebView extends AndroidWebView { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the AndroidManifest change required? if so we should probably mention it here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is no longer required, so I removed it from the changelog and example |
||
@override | ||
Widget build({ | ||
BuildContext context, | ||
CreationParams creationParams, | ||
WebViewPlatformCreatedCallback onWebViewPlatformCreated, | ||
Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers, | ||
@required WebViewPlatformCallbacksHandler webViewPlatformCallbacksHandler, | ||
}) { | ||
assert(webViewPlatformCallbacksHandler != null); | ||
return PlatformViewLink( | ||
viewType: 'plugins.flutter.io/webview', | ||
surfaceFactory: ( | ||
BuildContext context, | ||
PlatformViewController controller, | ||
) { | ||
return AndroidViewSurface( | ||
controller: controller, | ||
gestureRecognizers: gestureRecognizers ?? | ||
const <Factory<OneSequenceGestureRecognizer>>{}, | ||
hitTestBehavior: PlatformViewHitTestBehavior.opaque, | ||
); | ||
}, | ||
onCreatePlatformView: (PlatformViewCreationParams params) { | ||
return PlatformViewsService.initSurfaceAndroidView( | ||
id: params.id, | ||
viewType: 'plugins.flutter.io/webview', | ||
// WebView content is not affected by the Android view's layout direction, | ||
// we explicitly set it here so that the widget doesn't require an ambient | ||
// directionality. | ||
layoutDirection: TextDirection.rtl, | ||
creationParams: MethodChannelWebViewPlatform.creationParamsToMap( | ||
creationParams, | ||
), | ||
creationParamsCodec: const StandardMessageCodec(), | ||
) | ||
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) | ||
..addOnPlatformViewCreatedListener((int id) { | ||
if (onWebViewPlatformCreated == null) { | ||
return; | ||
} | ||
onWebViewPlatformCreated( | ||
MethodChannelWebViewPlatform(id, webViewPlatformCallbacksHandler), | ||
); | ||
}) | ||
..create(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
/// Decides how to handle a specific navigation request. | ||
/// | ||
/// The returned [NavigationDecision] determines how the navigation described by | ||
|
@@ -445,9 +508,7 @@ WebSettings _clearUnchangedWebSettings( | |
|
||
Set<String> _extractChannelNames(Set<JavascriptChannel> channels) { | ||
final Set<String> channelNames = channels == null | ||
// TODO(iskakaushik): Remove this when collection literals makes it to stable. | ||
// ignore: prefer_collection_literals | ||
? Set<String>() | ||
? <String>{} | ||
: channels.map((JavascriptChannel channel) => channel.name).toSet(); | ||
return channelNames; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this (note the link to hybrid composition and example):
Add support for building
WebView
widget with Android hybrid views.To use this feature, set
WebView.platform
to an instance ofSurfaceAndroidWebView
. For example: