Commit b043b435 authored by pichillilorenzo's avatar pichillilorenzo

v0.5.5

parent 53e9ecbf
This diff is collapsed.
## 0.5.5
- added `getUrl` method for the `InAppWebViewController` class
- added `getTitle` method for the `InAppWebViewController` class
- added `getProgress` method for the `InAppWebViewController` class
- added `getFavicon` method for the `InAppWebViewController` class
- added `onScrollChanged` event for the `InAppWebViewController` and `InAppBrowser` class
- added `onBrowserCreated` event for the `InAppBrowser` class
- added `openData` method for the `InAppBrowser` class
- added `initialData` property for the `InAppWebView` widget
## 0.5.4
- added `WebHistory` and `WebHistoryItem` class
- added `getCopyBackForwardList`, `goBackOrForward`, `canGoBackOrForward` and `goTo` methods for `InAppWebView` and `InAppBrowser`
- added `getCopyBackForwardList`, `goBackOrForward`, `canGoBackOrForward` and `goTo` methods for the `InAppWebViewController` class
## 0.5.3
......@@ -10,8 +21,8 @@
## 0.5.2
- fixed some missing `result.success()` on Android and iOS
- added `postUrl()` method for `InAppWebView` and `InAppBrowser`
- added `loadData()` method for `InAppWebView` and `InAppBrowser`
- added `postUrl()` method for the `InAppWebViewController` class
- added `loadData()` method for the `InAppWebViewController` class
## 0.5.1
......
......@@ -40,6 +40,8 @@ Classes:
- [InAppLocalhostServer](#inapplocalhostserver-class): This class allows you to create a simple server on `http://localhost:[port]/`. The default `port` value is `8080`.
- [CookieManager](#cookiemanager-class): Manages the cookies used by WebView instances. **NOTE for iOS**: available from iOS 11.0+.
See the online [docs](https://pub.dartlang.org/documentation/flutter_inappbrowser/latest/) to get the full documentation.
### `InAppWebView` class
Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree.
......@@ -193,6 +195,9 @@ Initial url that will be loaded.
#### InAppWebView.initialFile
Initial asset file that will be loaded. See `InAppWebView.loadFile()` for explanation.
#### InAppWebView.initialData
Initial `InAppWebViewInitialData` that will be loaded.
#### InAppWebView.initialHeaders
Initial headers that will be used.
......@@ -302,6 +307,49 @@ InAppWebView(
}
```
Event `onScrollChanged` fires when the `InAppWebView` scrolls.
`x` represents the current horizontal scroll origin in pixels.
`y` represents the current vertical scroll origin in pixels.
```dart
InAppWebView(
initialUrl: "https://flutter.io/",
onScrollChanged: (InAppWebViewController controller, int x, int y) {}
}
```
#### Future\<String\> InAppWebViewController.getUrl
Gets the URL for the current page.
This is not always the same as the URL passed to `InAppWebView.onLoadStarted` because although the load for that URL has begun, the current page may not have changed.
```dart
inAppWebViewController.getUrl();
```
#### Future\<String\> InAppWebViewController.getTitle
Gets the title for the current page.
```dart
inAppWebViewController.getTitle();
```
#### Future\<int\> InAppWebViewController.getProgress
Gets the progress for the current page. The progress value is between 0 and 100.
```dart
inAppWebViewController.getProgress();
```
#### Future\<List\<int\>\> InAppWebViewController.getFavicon
Gets the favicon for the current page.
```dart
inAppWebViewController.getFavicon();
```
#### Future\<void\> InAppWebViewController.loadUrl
Loads the given `url` with optional `headers` specified as a map from name to value.
......@@ -744,7 +792,7 @@ inAppBrowser.open({String url = "about:blank", Map<String, String> headers = con
#### Future\<void\> InAppBrowser.openFile
Opens the giver `assetFilePath` file in a new `InAppBrowser` instance. The other arguments are the same of `InAppBrowser.open()`.
Opens the given `assetFilePath` file in a new `InAppBrowser` instance. The other arguments are the same of `InAppBrowser.open()`.
To be able to load your local files (assets, js, css, etc.), you need to add them in the `assets` section of the `pubspec.yaml` file, otherwise they cannot be found!
......@@ -778,6 +826,16 @@ inAppBrowser.openFile("assets/index.html");
inAppBrowser.openFile(String assetFilePath, {Map<String, String> headers = const {}, Map<String, dynamic> options = const {}});
```
#### static Future\<void\> InAppBrowser.openData
Opens a new `InAppBrowser` instance with `data` as a content, using `baseUrl` as the base URL for it.
The `mimeType` parameter specifies the format of the data.
The `encoding` parameter specifies the encoding of the data.
```dart
InAppBrowser.openData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank", Map<String, dynamic> options = const {}});
```
#### static Future\<void\> InAppBrowser.openWithSystemBrowser
This is a static method that opens an `url` in the system browser. You wont be able to use the `InAppBrowser` methods here!
......@@ -841,6 +899,14 @@ inAppBrowser.isOpened();
#### Events
Event `onBrowserCreated` fires when the `InAppBrowser` is created.
```dart
@override
void onBrowserCreated() {
}
```
Event `onLoadStart` fires when the `InAppBrowser` starts to load an `url`.
```dart
@override
......@@ -911,6 +977,17 @@ Event `onLoadResource` fires when the `InAppBrowser` webview loads a resource.
}
```
Event `onScrollChanged` fires when the `InAppBrowser` webview scrolls.
`x` represents the current horizontal scroll origin in pixels.
`y` represents the current vertical scroll origin in pixels.
```dart
@override
void onScrollChanged(int x, int y) {
}
```
### `ChromeSafariBrowser` class
[Chrome Custom Tabs](https://developer.android.com/reference/android/support/customtabs/package-summary) on Android / [SFSafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) on iOS.
......
......@@ -40,12 +40,19 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
String initialUrl = (String) params.get("initialUrl");
String initialFile = (String) params.get("initialFile");
Map<String, String> initialData = (Map<String, String>) params.get("initialData");
Map<String, String> initialHeaders = (Map<String, String>) params.get("initialHeaders");
HashMap<String, Object> initialOptions = (HashMap<String, Object>) params.get("initialOptions");
InAppWebViewOptions options = new InAppWebViewOptions();
options.parse(initialOptions);
webView = new InAppWebView(context, this, id, options);
webView.prepare();
channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappwebview_" + id);
channel.setMethodCallHandler(this);
if (initialFile != null) {
try {
initialUrl = Util.getUrlAsset(registrar, initialFile);
......@@ -56,12 +63,14 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
}
}
webView = new InAppWebView(context, this, id, options);
webView.prepare();
channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappwebview_" + id);
channel.setMethodCallHandler(this);
if (initialData != null) {
String data = initialData.get("data");
String mimeType = initialData.get("mimeType");
String encoding = initialData.get("encoding");
String baseUrl = initialData.get("baseUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, null);
}
else
webView.loadUrl(initialUrl, initialHeaders);
}
......@@ -76,6 +85,15 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
String jsWrapper;
String urlFile;
switch (call.method) {
case "getUrl":
result.success((webView != null) ? webView.getUrl() : null);
break;
case "getTitle":
result.success((webView != null) ? webView.getTitle() : null);
break;
case "getProgress":
result.success((webView != null) ? webView.getProgress() : null);
break;
case "loadUrl":
if (webView != null)
webView.loadUrl(call.argument("url").toString(), (Map<String, String>) call.argument("headers"), result);
......
......@@ -52,7 +52,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
Bundle b = getIntent().getExtras();
uuid = b.getString("uuid");
String url = b.getString("url");
HashMap<String, Object> optionsMap = (HashMap<String, Object>) b.getSerializable("options");
options = new InAppBrowserOptions();
......@@ -62,16 +62,29 @@ public class InAppBrowserActivity extends AppCompatActivity {
webViewOptions.parse(optionsMap);
webView.options = webViewOptions;
headers = (HashMap<String, String>) b.getSerializable("headers");
InAppBrowserFlutterPlugin.webViewActivities.put(uuid, this);
actionBar = getSupportActionBar();
prepareView();
Boolean isData = b.getBoolean("isData");
if (!isData) {
headers = (HashMap<String, String>) b.getSerializable("headers");
String url = b.getString("url");
webView.loadUrl(url, headers);
//webView.loadData("<!DOCTYPE assets> <assets lang=\"en\"> <head> <meta charset=\"UTF-8\"> <title>Document</title> </head> <body> ciao <img src=\"https://via.placeholder.com/350x150\" /> <img src=\"./images/test\" alt=\"not found\" /></body> </assets>", "text/assets", "utf8");
}
else {
String data = b.getString("data");
String mimeType = b.getString("mimeType");
String encoding = b.getString("encoding");
String baseUrl = b.getString("baseUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, null);
}
Map<String, Object> obj = new HashMap<>();
obj.put("uuid", uuid);
InAppBrowserFlutterPlugin.channel.invokeMethod("onBrowserCreated", obj);
}
......@@ -164,6 +177,24 @@ public class InAppBrowserActivity extends AppCompatActivity {
return true;
}
public String getUrl() {
if (webView != null)
return webView.getUrl();
return null;
}
public String getWebViewTitle() {
if (webView != null)
return webView.getTitle();
return null;
}
public Integer getProgress() {
if (webView != null)
return webView.getProgress();
return null;
}
public void loadUrl(String url, MethodChannel.Result result) {
if (webView != null) {
webView.loadUrl(url, result);
......
......@@ -97,6 +97,8 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
switch (call.method) {
case "open":
boolean isData = (boolean) call.argument("isData");
if (!isData) {
final String url_final = call.argument("url").toString();
final boolean useChromeSafariBrowser = (boolean) call.argument("useChromeSafariBrowser");
......@@ -162,6 +164,30 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
}
}
});
}
else {
this.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
HashMap<String, Object> options = (HashMap<String, Object>) call.argument("options");
String data = call.argument("data").toString();
String mimeType = call.argument("mimeType").toString();
String encoding = call.argument("encoding").toString();
String baseUrl = call.argument("baseUrl").toString();
openData(uuid, options, data, mimeType, encoding, baseUrl);
result.success(true);
}
});
}
break;
case "getUrl":
result.success(getUrl(uuid));
break;
case "getTitle":
result.success(getTitle(uuid));
break;
case "getProgress":
result.success(getProgress(uuid));
break;
case "loadUrl":
loadUrl(uuid, call.argument("url").toString(), (Map<String, String>) call.argument("headers"), result);
......@@ -370,7 +396,7 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
Intent intent = null;
Bundle extras = new Bundle();
extras.putString("url", url);
extras.putBoolean("isData", false);
extras.putString("uuid", uuid);
extras.putSerializable("options", options);
extras.putSerializable("headers", (Serializable) headers);
......@@ -408,6 +434,43 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
result.error(LOG_TAG, "No WebView fallback declared.", null);
}
public void openData(String uuid, HashMap<String, Object> options, String data, String mimeType, String encoding, String baseUrl) {
Intent intent = new Intent(activity, InAppBrowserActivity.class);
Bundle extras = new Bundle();
extras.putBoolean("isData", true);
extras.putString("uuid", uuid);
extras.putSerializable("options", options);
extras.putString("data", data);
extras.putString("mimeType", mimeType);
extras.putString("encoding", encoding);
extras.putString("baseUrl", baseUrl);
intent.putExtras(extras);
activity.startActivity(intent);
}
private String getUrl(String uuid) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
return inAppBrowserActivity.getUrl();
return null;
}
private String getTitle(String uuid) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
return inAppBrowserActivity.getWebViewTitle();
return null;
}
private Integer getProgress(String uuid) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
return inAppBrowserActivity.getProgress();
return null;
}
public void loadUrl(String uuid, String url, Map<String, String> headers, Result result) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
......
......@@ -18,6 +18,7 @@ import android.webkit.WebView;
import com.pichillilorenzo.flutter_inappbrowser.FlutterWebView;
import com.pichillilorenzo.flutter_inappbrowser.InAppBrowserActivity;
import com.pichillilorenzo.flutter_inappbrowser.InAppBrowserFlutterPlugin;
import com.pichillilorenzo.flutter_inappbrowser.JavaScriptBridgeInterface;
import com.pichillilorenzo.flutter_inappbrowser.Util;
......@@ -431,4 +432,26 @@ public class InAppWebView extends WebView {
return result;
}
@Override
protected void onScrollChanged (int l,
int t,
int oldl,
int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float scale = getResources().getDisplayMetrics().density;
int x = (int) (l/scale);
int y = (int) (t/scale);
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("x", x);
obj.put("y", y);
getChannel().invokeMethod("onScrollChanged", obj);
}
private MethodChannel getChannel() {
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.channel : flutterWebView.channel;
}
}
......@@ -4,6 +4,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
class MyInAppBrowser extends InAppBrowser {
@override
Future onBrowserCreated() async {
print("\n\nBrowser Ready!\n\n");
}
@override
Future onLoadStart(String url) async {
print("\n\nStarted $url\n\n");
......@@ -16,6 +21,7 @@ class MyInAppBrowser extends InAppBrowser {
Future onLoadStop(String url) async {
print("\n\nStopped $url\n\n");
// print(base64.encode(await this.webViewController.getFavicon()));
// WebHistory history = await this.webViewController.getCopyBackForwardList();
// print(history.list.length);
// print(history.currentIndex);
......@@ -23,6 +29,7 @@ class MyInAppBrowser extends InAppBrowser {
// for(WebHistoryItem item in history.list) {
// print(item.title);
// }
//
// print(await this.webViewController.canGoBackOrForward(1));
// if (await this.webViewController.canGoBackOrForward(-2)) {
......@@ -132,6 +139,11 @@ class MyInAppBrowser extends InAppBrowser {
// this.webViewController.injectStyleFile("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css");
}
@override
Future onScrollChanged(int x, int y) async {
// print(x.toString() + " " + y.toString());
}
@override
void onLoadError(String url, int code, String message) {
print("\n\nCan't load $url.. Error: $message\n\n");
......@@ -308,11 +320,13 @@ class _MyAppState extends State<MyApp> {
// await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", domain: "flutter.io", expiresDate: 1540838864611);
// await CookieManager.setCookie("https://flutter.io/", "my_cookie", "cookieValue", domain: "flutter.io", expiresDate: 1540838864611);
// await inAppBrowserFallback.openData("<html><head><title>Data example</title></head><body><p>This is a \"p\" tag</p></body></html>", options: {});
await inAppBrowserFallback.open(url: "https://flutter.io/", options: {
//"useOnLoadResource": true,
//"hidden": true,
//"toolbarTopFixedTitle": "Fixed title",
"useShouldOverrideUrlLoading": true
"useShouldOverrideUrlLoading": true,
//"hideUrlBar": true,
//"toolbarTop": false,
//"toolbarBottom": false
......@@ -382,6 +396,7 @@ class _MyAppState extends State<MyApp> {
// ),
// child: InAppWebView(
// initialUrl: "https://flutter.io/",
// //initialData: InAppWebViewInitialData("<html><head><title>Data example</title></head><body><p>This is a \"p\" tag</p></body></html>"),
// initialHeaders: {
//
// },
......
......@@ -20,6 +20,7 @@
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/build" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/ios/Flutter/flutter_assets/packages" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/flutter_assets/packages" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
......
......@@ -137,7 +137,7 @@ class InAppWebView_IBWrapper: InAppWebView {
}
}
class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, UITextFieldDelegate, WKScriptMessageHandler {
class InAppBrowserWebViewController: UIViewController, UIScrollViewDelegate, WKUIDelegate, WKNavigationDelegate, UITextFieldDelegate, WKScriptMessageHandler {
@IBOutlet var webView: InAppWebView_IBWrapper!
@IBOutlet var closeButton: UIButton!
......@@ -161,6 +161,10 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
var browserOptions: InAppBrowserOptions?
var webViewOptions: InAppWebViewOptions?
var initHeaders: [String: String]?
var initData: String?
var initMimeType: String?
var initEncoding: String?
var initBaseUrl: String?
var isHidden = false
var uuid: String = ""
var WKNavigationMap: [String: [String: Any]] = [:]
......@@ -186,6 +190,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
webView.uiDelegate = self
webView.navigationDelegate = self
webView.scrollView.delegate = self
urlField.delegate = self
urlField.text = self.currentURL?.absoluteString
......@@ -211,8 +216,14 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
spinner.isHidden = false
spinner.stopAnimating()
if self.initData == nil {
loadUrl(url: self.currentURL!, headers: self.initHeaders)
}
else {
loadData(data: initData!, mimeType: initMimeType!, encoding: initEncoding!, baseUrl: initBaseUrl!)
}
navigationDelegate?.onBrowserCreated(uuid: uuid, webView: webView)
}
// Prevent crashes on closing windows
......@@ -369,10 +380,11 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
currentURL = url
updateUrlTextField(url: (currentURL?.absoluteString)!)
if headers != nil {
if let mutableRequest = (request as NSURLRequest).mutableCopy() as? NSMutableURLRequest {
for (key, value) in headers! {
request.setValue(value, forHTTPHeaderField: key)
mutableRequest.setValue(value, forHTTPHeaderField: key)
}
request = mutableRequest as URLRequest
}
webView.load(request)
......@@ -582,6 +594,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
currentURL = url
updateUrlTextField(url: (url.absoluteString))
}
}
......@@ -693,6 +706,14 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if navigationDelegate != nil {
let x = Int(scrollView.contentOffset.x / scrollView.contentScaleFactor)
let y = Int(scrollView.contentOffset.y / scrollView.contentScaleFactor)
navigationDelegate?.onScrollChanged(uuid: self.uuid, webView: webView, x: x, y: y)
}
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name.starts(with: "console") {
var messageLevel = "LOG"
......@@ -960,4 +981,5 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
return result;
}
}
......@@ -10,6 +10,8 @@ import WebKit
public class InAppWebView: WKWebView {
var historyOffset = 0
public func goBackOrForward(steps: Int) {
if canGoBackOrForward(steps: steps) {
if (steps > 0) {
......
This diff is collapsed.
......@@ -127,6 +127,10 @@ class InAppBrowser {
Future<dynamic> _handleMethod(MethodCall call) async {
switch(call.method) {
case "onBrowserCreated":
this._isOpened = true;
onBrowserCreated();
break;
case "onExit":
this._isOpened = false;
onExit();
......@@ -199,12 +203,12 @@ class InAppBrowser {
args.putIfAbsent('options', () => options);
args.putIfAbsent('openWithSystemBrowser', () => false);
args.putIfAbsent('isLocalFile', () => false);
args.putIfAbsent('isData', () => false);
args.putIfAbsent('useChromeSafariBrowser', () => false);
await _ChannelManager.channel.invokeMethod('open', args);
this._isOpened = true;
}
///Opens the giver [assetFilePath] file in a new [InAppBrowser] instance. The other arguments are the same of [InAppBrowser.open()].
///Opens the given [assetFilePath] file in a new [InAppBrowser] instance. The other arguments are the same of [InAppBrowser.open()].
///
///To be able to load your local files (assets, js, css, etc.), you need to add them in the `assets` section of the `pubspec.yaml` file, otherwise they cannot be found!
///
......@@ -243,9 +247,28 @@ class InAppBrowser {
args.putIfAbsent('options', () => options);
args.putIfAbsent('openWithSystemBrowser', () => false);
args.putIfAbsent('isLocalFile', () => true);
args.putIfAbsent('isData', () => false);
args.putIfAbsent('useChromeSafariBrowser', () => false);
await _ChannelManager.channel.invokeMethod('open', args);
}
///Opens a new [InAppBrowser] instance with [data] as a content, using [baseUrl] as the base URL for it.
///The [mimeType] parameter specifies the format of the data.
///The [encoding] parameter specifies the encoding of the data.
Future<void> openData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank", Map<String, dynamic> options = const {}}) async {
assert(data != null);
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('uuid', () => uuid);
args.putIfAbsent('options', () => options);
args.putIfAbsent('data', () => data);
args.putIfAbsent('mimeType', () => mimeType);
args.putIfAbsent('encoding', () => encoding);
args.putIfAbsent('baseUrl', () => baseUrl);
args.putIfAbsent('openWithSystemBrowser', () => false);
args.putIfAbsent('isLocalFile', () => false);
args.putIfAbsent('isData', () => true);
args.putIfAbsent('useChromeSafariBrowser', () => false);
await _ChannelManager.channel.invokeMethod('open', args);
this._isOpened = true;
}
///This is a static method that opens an [url] in the system browser. You wont be able to use the [InAppBrowser] methods here!
......@@ -256,6 +279,7 @@ class InAppBrowser {
args.putIfAbsent('url', () => url);
args.putIfAbsent('headers', () => {});
args.putIfAbsent('isLocalFile', () => false);
args.putIfAbsent('isData', () => false);
args.putIfAbsent('openWithSystemBrowser', () => true);
args.putIfAbsent('useChromeSafariBrowser', () => false);
return await _ChannelManager.channel.invokeMethod('open', args);
......@@ -319,6 +343,11 @@ class InAppBrowser {
return this._isOpened;
}
///Event fires when the [InAppBrowser] is created.
void onBrowserCreated() {
}
///Event fires when the [InAppBrowser] starts to load an [url].
void onLoadStart(String url) {
......@@ -365,6 +394,13 @@ class InAppBrowser {
}
///Event fires when the [InAppBrowser] webview scrolls.
///[x] represents the current horizontal scroll origin in pixels.
///[y] represents the current vertical scroll origin in pixels.
void onScrollChanged(int x, int y) {
}
void _throwIsAlreadyOpened({String message = ''}) {
if (this.isOpened()) {
throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is already opened.']);
......@@ -496,6 +532,28 @@ typedef void onWebViewProgressChangedCallback(InAppWebViewController controller,
typedef void onWebViewConsoleMessageCallback(InAppWebViewController controller, ConsoleMessage consoleMessage);
typedef void shouldOverrideUrlLoadingCallback(InAppWebViewController controller, String url);
typedef void onWebViewLoadResourceCallback(InAppWebViewController controller, WebResourceResponse response, WebResourceRequest request);
typedef void onWebViewScrollChangedCallback(InAppWebViewController controller, int x, int y);
///Initial [data] as a content for an [InAppWebView] instance, using [baseUrl] as the base URL for it.
///The [mimeType] property specifies the format of the data.
///The [encoding] property specifies the encoding of the data.
class InAppWebViewInitialData {
String data;
String mimeType;
String encoding;
String baseUrl;
InAppWebViewInitialData(this.data, {this.mimeType = "text/html", this.encoding = "utf8", this.baseUrl = "about:blank"});
Map<String, String> toMap() {
return {
"data": data,
"mimeType": mimeType,
"encoding": encoding,
"baseUrl": baseUrl
};
}
}
///InAppWebView Widget class.
///
......@@ -563,10 +621,17 @@ class InAppWebView extends StatefulWidget {
///**NOTE only for iOS**: In some cases, the [response.data] of a [response] with `text/assets` encoding could be empty.
final onWebViewLoadResourceCallback onLoadResource;
///Event fires when the [InAppWebView] scrolls.
///[x] represents the current horizontal scroll origin in pixels.
///[y] represents the current vertical scroll origin in pixels.
final onWebViewScrollChangedCallback onScrollChanged;
///Initial url that will be loaded.
final String initialUrl;
///Initial asset file that will be loaded. See [InAppWebView.loadFile()] for explanation.
final String initialFile;
///Initial [InAppWebViewInitialData] that will be loaded.
final InAppWebViewInitialData initialData;
///Initial headers that will be used.
final Map<String, String> initialHeaders;
///Initial options that will be used.
......@@ -577,6 +642,7 @@ class InAppWebView extends StatefulWidget {
Key key,
this.initialUrl = "about:blank",
this.initialFile,
this.initialData,
this.initialHeaders = const {},
this.initialOptions = const {},
this.onWebViewCreated,
......@@ -587,6 +653,7 @@ class InAppWebView extends StatefulWidget {
this.onProgressChanged,
this.shouldOverrideUrlLoading,
this.onLoadResource,
this.onScrollChanged,
this.gestureRecognizers,
}) : super(key: key);
......@@ -620,6 +687,7 @@ class _InAppWebViewState extends State<InAppWebView> {
creationParams: <String, dynamic>{
'initialUrl': widget.initialUrl,
'initialFile': widget.initialFile,
'initialData': widget.initialData?.toMap(),
'initialHeaders': widget.initialHeaders,
'initialOptions': widget.initialOptions
},
......@@ -754,6 +822,14 @@ class InAppWebViewController {
else
_inAppBrowser.onConsoleMessage(ConsoleMessage(sourceURL, lineNumber, message, messageLevel));
break;
case "onScrollChanged":
int x = call.arguments["x"];
int y = call.arguments["y"];
if (_widget != null)
_widget.onScrollChanged(this, x, y);
else
_inAppBrowser.onScrollChanged(x, y);
break;
case "onCallJsHandler":
String handlerName = call.arguments["handlerName"];
List<dynamic> args = jsonDecode(call.arguments["args"]);
......@@ -768,6 +844,57 @@ class InAppWebViewController {
}
}
///Gets the URL for the current page.
///This is not always the same as the URL passed to [InAppWebView.onLoadStarted] because although the load for that URL has begun, the current page may not have changed.
Future<String> getUrl() async {
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
return await _channel.invokeMethod('getUrl', args);
}
///Gets the title for the current page.
Future<String> getTitle() async {
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
return await _channel.invokeMethod('getTitle', args);
}
///Gets the progress for the current page. The progress value is between 0 and 100.
Future<int> getProgress() async {
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
return await _channel.invokeMethod('getProgress', args);
}
///Gets the favicon for the current page.
Future<List<int>> getFavicon() async {
var completer = new Completer<List<int>>();
var faviconData = new List<int>();
HttpClient client = new HttpClient();
var url = Uri.parse(await getUrl());
// solution found here: https://stackoverflow.com/a/15750809/4637638
var faviconUrl = Uri.parse("https://plus.google.com/_/favicon?domain_url=" + url.scheme + "://" + url.host);
client.getUrl(faviconUrl).then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) {
response.listen((List<int> data) {
faviconData = data;
}, onDone: () => completer.complete(faviconData));
});
return completer.future;
}
///Loads the given [url] with optional [headers] specified as a map from name to value.
Future<void> loadUrl(String url, {Map<String, String> headers = const {}}) async {
assert(url != null && url.isNotEmpty);
......
name: flutter_inappbrowser
description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window. (inspired by the popular cordova-plugin-inappbrowser).
version: 0.5.4
version: 0.5.5
author: Lorenzo Pichilli <pichillilorenzo@gmail.com>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment