Skip to content

Commit

Permalink
add WebviewScaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
lejard-h committed Jan 28, 2018
1 parent c223270 commit dba0ad0
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 393 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# 0.0.10
# 0.1.0

- iOS && Android:
- get cookies
- eval javascript
- user agent setting
- state change event
- embed in rectangle(not fullscreen)
- embed in rectangle or fullscreen if null
- hidden webview

- Android
- adding Activity in manifest is no mandatory when not using fullScreen
- adding Activity in manifest is not needed anymore

- Add `WebviewScaffold`

# 0.0.9

Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Your Company. All rights reserved.
// Copyright 2017 Hadrien Lejard. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
Expand All @@ -10,7 +10,7 @@
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Your Company nor the names of its
// * Neither the name of Hadrien Lejard nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
Expand Down
72 changes: 47 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ For help getting started with Flutter, view our online [documentation](http://fl

### How it works

#### Launch WebView Fullscreen (default)
#### Launch WebView Fullscreen with Flutter navigation

On Android, add the Activity to you `AndroidManifest.xml`:

```xml
<activity android:name="com.flutter_webview_plugin.WebviewActivity" android:parentActivityName=".MainActivity"/>
```dart
new MaterialApp(
routes: {
"/": (_) => new WebviewScaffold(
url: "https://www.google.com",
appBar: new AppBar(
title: new Text("Widget webview"),
),
)
},
);
```

***For Android, it will launch a new Activity inside the App with the Webview inside. Does not allow to integrate a Webview inside a Flutter Widget***

***For IOS, it will launch a new UIViewController inside the App with the UIWebView inside. Does not allow to integrate a Webview inside a Flutter Widget***

`FlutterWebviewPlugin` provide a singleton instance linked to one unique webview,
so you can take control of the webview from anywhere in the app

listen for events
```dart
final flutterWebviewPlugin = new FlutterWebviewPlugin();
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url);
flutterWebviewPlugin.onUrlChanged.listen((String url) {
});
```

#### Close launched WebView
#### Hidden WebView

```dart
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url);
....
// Close WebView.
// This will also emit the onDestroy event.
flutterWebviewPlugin.close();
flutterWebviewPlugin.launch(url, hidden: true);
```

#### Hidden webView
#### Close launched WebView

```dart
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url, hidden: true);
flutterWebviewPlugin.close();
```

#### Webview inside custom Rectangle
Expand All @@ -67,8 +67,30 @@ flutterWebviewPlugin.launch(url,

- `Stream<Null>` onDestroy
- `Stream<String>` onUrlChanged
- `Stream<Null>` onBackPressed
- `Stream<WebViewStateChanged>` onStateChanged
- `Stream<String>` onError

***Don't forget to dispose webview***
`flutterWebviewPlugin.dispose()`
`flutterWebviewPlugin.dispose()`

### Webview Functions

```dart
Future<Null> launch(String url,
{bool withJavascript: true,
bool clearCache: false,
bool clearCookies: false,
bool hidden: false,
bool enableAppScheme: true,
Rect rect: null,
String userAgent: null});
```
```dart
Future<String> evalJavascript(String code);
```
```dart
Future<Map<String, dynamic>> getCookies();
```
```dart
Future<Null> resize(Rect rect);
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.view.Display;
import android.widget.FrameLayout;

import java.util.Map;
Expand All @@ -17,16 +18,14 @@
* FlutterWebviewPlugin
*/
public class FlutterWebviewPlugin implements MethodCallHandler {
private final int WEBVIEW_ACTIVITY_CODE = 1;

private Activity activity;
private WebviewManager webViewManager;
public static MethodChannel channel;
static MethodChannel channel;
private static final String CHANNEL_NAME = "flutter_webview_plugin";

public static void registerWith(PluginRegistry.Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
FlutterWebviewPlugin instance = new FlutterWebviewPlugin((Activity) registrar.activity());
FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity());
channel.setMethodCallHandler(instance);
}

Expand All @@ -46,47 +45,61 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "eval":
eval(call, result);
break;
case "resize":
resize(call, result);
break;
default:
result.notImplemented();
break;
}
}

private void openUrl(MethodCall call, MethodChannel.Result result) {
if ((boolean) call.argument("fullScreen") && !(boolean) call.argument("hidden")) {
Intent intent = new Intent(activity, WebviewActivity.class);
intent.putExtra(WebviewActivity.URL_KEY, (String) call.argument("url"));
intent.putExtra(WebviewActivity.WITH_JAVASCRIPT_KEY, (boolean) call.argument("withJavascript"));
intent.putExtra(WebviewActivity.CLEAR_CACHE_KEY, (boolean) call.argument("clearCache"));
intent.putExtra(WebviewActivity.CLEAR_COOKIES_KEY, (boolean) call.argument("clearCookies"));
activity.startActivityForResult(intent, WEBVIEW_ACTIVITY_CODE);
} else {
if (webViewManager == null) {
webViewManager = new WebviewManager(activity);
}

Map<String, Number> rc = call.argument("rect");
if (rc != null) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
dp2px(activity, rc.get("width").intValue()), dp2px(activity, rc.get("height").intValue()));
params.setMargins(dp2px(activity, rc.get("left").intValue()), dp2px(activity, rc.get("top").intValue()),
0, 0);
activity.addContentView(webViewManager.webView, params);
} else if (!(boolean) call.argument("hidden")) {
activity.setContentView(webViewManager.webView);
}

webViewManager.openUrl((boolean) call.argument("withJavascript"),
(boolean) call.argument("clearCache"),
(boolean) call.argument("hidden"),
(boolean) call.argument("clearCookies"),
(String) call.argument("userAgent"),
(String) call.argument("url")
);
boolean hidden = call.argument("hidden");
String url = call.argument("url");
String userAgent = call.argument("userAgent");
boolean withJavascript = call.argument("withJavascript");
boolean clearCache = call.argument("clearCache");
boolean clearCookies = call.argument("clearCookies");

if (webViewManager == null) {
webViewManager = new WebviewManager(activity);
}

FrameLayout.LayoutParams params = buildLayoutParams(call);

activity.addContentView(webViewManager.webView, params);

webViewManager.openUrl(withJavascript,
clearCache,
hidden,
clearCookies,
userAgent,
url
);
result.success(null);
}

private FrameLayout.LayoutParams buildLayoutParams(MethodCall call) {
Map<String, Number> rc = call.argument("rect");
FrameLayout.LayoutParams params;
if (rc != null) {
params = new FrameLayout.LayoutParams(
dp2px(activity, rc.get("width").intValue()), dp2px(activity, rc.get("height").intValue()));
params.setMargins(dp2px(activity, rc.get("left").intValue()), dp2px(activity, rc.get("top").intValue()),
0, 0);
} else {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
params = new FrameLayout.LayoutParams(width, height);
}

return params;
}

private void close(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.close(call, result);
Expand All @@ -100,7 +113,15 @@ private void eval(MethodCall call, final MethodChannel.Result result) {
}
}

private static int dp2px(Context context, float dp) {
private void resize(MethodCall call, final MethodChannel.Result result) {
if (webViewManager != null) {
FrameLayout.LayoutParams params = buildLayoutParams(call);
webViewManager.resize(params);
}
result.success(null);
}

private int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -89,4 +90,8 @@ public void onReceiveValue(String value) {
}
});
}

void resize(FrameLayout.LayoutParams params) {
webView.setLayoutParams(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
*/
public final class GeneratedPluginRegistrant {
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
FlutterWebviewPlugin.registerWith(registry.registrarFor("com.flutter_webview_plugin.FlutterWebviewPlugin"));
}

private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = GeneratedPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}
Loading

0 comments on commit dba0ad0

Please sign in to comment.