This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
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
Merged
bparrishMines
merged 12 commits into
flutter:master
from
bparrishMines:webview_entrypoint
Sep 21, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8f8ad8
Add surface view
bparrishMines 6a5927d
Add test for scrolling hybrid view
bparrishMines 8e90cc3
Create a custom platform for android surface view
bparrishMines da2a0b9
Formatting and changelog
bparrishMines bdd7f2e
Merge branch 'master' of github.com:flutter/plugins into webview_entr…
bparrishMines 1cef7c0
Merge branch 'master' of github.com:flutter/plugins into webview_entr…
bparrishMines 24533ed
Merge branch 'master' of github.com:flutter/plugins into webview_entr…
bparrishMines 29d2b88
test reformat and better documentation
bparrishMines 7c30a7e
slight doc change
bparrishMines 6185138
Merge branch 'master' of github.com:flutter/plugins into webview_entr…
bparrishMines c82be0a
update docs/changelog and hopefully fix ios
bparrishMines 28faa1a
fix ios test
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
/// | ||
/// This implementation uses hybrid composition to render the [WebView] on | ||
/// 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/wiki/Hybrid-Composition for 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; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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?