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.7k
Initial (Android only) implementation of a WebView widget. #752
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="io.flutter.plugins.webviewflutter"> | ||
| <uses-sdk android:minSdkVersion="20" android:targetSdkVersion="26"/> | ||
| </manifest> |
48 changes: 48 additions & 0 deletions
48
...bview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import static io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
| import static io.flutter.plugin.common.MethodChannel.Result; | ||
|
|
||
| import android.content.Context; | ||
| import android.view.View; | ||
| import android.webkit.WebView; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import io.flutter.plugin.platform.PlatformView; | ||
|
|
||
| public class FlutterWebView implements PlatformView, MethodCallHandler { | ||
| private final WebView webView; | ||
| private final MethodChannel methodChannel; | ||
|
|
||
| FlutterWebView(Context context, BinaryMessenger messenger, int id) { | ||
| webView = new WebView(context); | ||
| methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id); | ||
| methodChannel.setMethodCallHandler(this); | ||
| } | ||
|
|
||
| @Override | ||
| public View getView() { | ||
| return webView; | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall methodCall, Result result) { | ||
| switch (methodCall.method) { | ||
| case "loadUrl": | ||
| loadUrl(methodCall, result); | ||
| break; | ||
| default: | ||
| result.notImplemented(); | ||
| } | ||
| } | ||
|
|
||
| private void loadUrl(MethodCall methodCall, Result result) { | ||
| String url = (String) methodCall.arguments; | ||
| webView.loadUrl(url); | ||
| result.success(null); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispose() {} | ||
| } |
21 changes: 21 additions & 0 deletions
21
...bview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFactory.java
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import android.content.Context; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.StandardMessageCodec; | ||
| import io.flutter.plugin.platform.PlatformView; | ||
| import io.flutter.plugin.platform.PlatformViewFactory; | ||
|
|
||
| public class WebViewFactory extends PlatformViewFactory { | ||
| private final BinaryMessenger messenger; | ||
|
|
||
| public WebViewFactory(BinaryMessenger messenger) { | ||
| super(StandardMessageCodec.INSTANCE); | ||
| this.messenger = messenger; | ||
| } | ||
|
|
||
| @Override | ||
| public PlatformView create(Context context, int id, Object args) { | ||
| return new FlutterWebView(context, messenger, id); | ||
| } | ||
| } |
21 changes: 5 additions & 16 deletions
21
...flutter/android/src/main/java/io/flutter/plugins/webviewflutter/WebviewFlutterPlugin.java
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 |
|---|---|---|
| @@ -1,25 +1,14 @@ | ||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
| import io.flutter.plugin.common.MethodChannel.Result; | ||
| import io.flutter.plugin.common.PluginRegistry.Registrar; | ||
|
|
||
| /** WebviewFlutterPlugin */ | ||
| public class WebviewFlutterPlugin implements MethodCallHandler { | ||
| public class WebviewFlutterPlugin { | ||
| /** Plugin registration. */ | ||
| public static void registerWith(Registrar registrar) { | ||
| final MethodChannel channel = new MethodChannel(registrar.messenger(), "webview_flutter"); | ||
| channel.setMethodCallHandler(new WebviewFlutterPlugin()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, Result result) { | ||
| if (call.method.equals("getPlatformVersion")) { | ||
| result.success("Android " + android.os.Build.VERSION.RELEASE); | ||
| } else { | ||
| result.notImplemented(); | ||
| } | ||
| registrar | ||
| .platformViewRegistry() | ||
| .registerViewFactory( | ||
| "plugins.flutter.io/webview", new WebViewFactory(registrar.messenger())); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -4,13 +4,112 @@ | |
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/gestures.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| class WebviewFlutter { | ||
| static const MethodChannel _channel = MethodChannel('webview_flutter'); | ||
| typedef void WebViewCreatedCallback(WebViewController controller); | ||
|
|
||
| static Future<String> get platformVersion async { | ||
| final String version = await _channel.invokeMethod('getPlatformVersion'); | ||
| return version; | ||
| /// A web view widget for showing html content. | ||
| class WebView extends StatefulWidget { | ||
| /// Creates a new web view. | ||
| /// | ||
| /// The web view can be controlled using a `WebViewController` that is passed to the | ||
| /// `onWebViewCreated` callback once the web view is created. | ||
| /// | ||
| /// The `gestureRecognizers` parameter must not be null; | ||
| const WebView({ | ||
| Key key, | ||
| this.onWebViewCreated, | ||
| this.gestureRecognizers = const <OneSequenceGestureRecognizer>[], | ||
| }) : assert(gestureRecognizers != null), | ||
| super(key: key); | ||
|
|
||
| /// If not null invoked once the web view is created. | ||
| final WebViewCreatedCallback onWebViewCreated; | ||
|
|
||
| /// Which gestures should be consumed by the web view. | ||
| /// | ||
| /// It is possible for other gesture recognizers to be competing with the web view on pointer | ||
| /// events, e.g if the webview is inside a [ListView] the [ListView] will want to handle | ||
| /// vertical drags. The web view will claim gestures that are recognized by any of the | ||
| /// recognizers on this list. | ||
| /// | ||
| /// When this list is empty, the web view will only handle pointer events for gestures that | ||
| /// were not claimed by any other gesture recognizer. | ||
| final List<OneSequenceGestureRecognizer> gestureRecognizers; | ||
|
|
||
| @override | ||
| State<StatefulWidget> createState() => _WebViewState(); | ||
| } | ||
|
|
||
| class _WebViewState extends State<WebView> { | ||
| @override | ||
| Widget build(BuildContext context) { | ||
| if (defaultTargetPlatform == TargetPlatform.android) { | ||
| return new GestureDetector( | ||
| // We prevent text selection by intercepting long press event. | ||
| // This is a temporary workaround to prevent a native crash on a second | ||
| // text selection. | ||
| // TODO(amirh): remove this when the selection handles crash is resolved. | ||
| // https://github.com/flutter/flutter/issues/21239 | ||
| onLongPress: () {}, | ||
| child: AndroidView( | ||
| viewType: 'plugins.flutter.io/webview', | ||
| onPlatformViewCreated: _onPlatformViewCreated, | ||
| gestureRecognizers: widget.gestureRecognizers, | ||
| // 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, | ||
| ), | ||
| ); | ||
| } | ||
| return Text( | ||
| '$defaultTargetPlatform is not yet supported by the webview_flutter plugin'); | ||
| } | ||
|
|
||
| void _onPlatformViewCreated(int id) { | ||
| if (widget.onWebViewCreated == null) { | ||
| return; | ||
| } | ||
| widget.onWebViewCreated(new WebViewController._(id)); | ||
| } | ||
| } | ||
|
|
||
| /// Controls a [WebView]. | ||
| /// | ||
| /// A [WebViewController] instance can be obtained by setting the [WebView.onWebViewCreated] | ||
| /// callback for a [WebView] widget. | ||
| class WebViewController { | ||
| WebViewController._(int id) { | ||
| _channel = new MethodChannel( | ||
| 'plugins.flutter.io/webview_$id', const StandardMethodCodec()); | ||
| } | ||
|
|
||
| MethodChannel _channel; | ||
|
||
|
|
||
| /// Loads the specified URL. | ||
| /// | ||
| /// `url` must not be null. | ||
| /// | ||
| /// Throws an ArgumentError if `url` is not a valid URL string. | ||
| Future<void> loadUrl(String url) async { | ||
| assert(url != null); | ||
| _validateUrlString(url); | ||
| return _channel.invokeMethod('loadUrl', url); | ||
| } | ||
| } | ||
|
|
||
| // Throws an ArgumentError if url is not a valid url string. | ||
| void _validateUrlString(String url) { | ||
| try { | ||
| final Uri uri = Uri.parse(url); | ||
| if (uri.scheme.isEmpty) { | ||
| throw new ArgumentError('Missing scheme in URL string: "$url"'); | ||
| } | ||
| } on FormatException catch (e) { | ||
| throw new ArgumentError(e); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
nit:
StandardMethodCodec()is the default valueThere 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