Skip to content

Commit f445329

Browse files
committed
implement on url changed
1 parent 1b339ca commit f445329

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

android/src/main/java/com/flutter_webview_plugin/WebviewActivity.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.flutter_webview_plugin;
22

33
import android.app.Activity;
4+
import android.graphics.Bitmap;
5+
import android.os.Build;
46
import android.os.Bundle;
57
import android.webkit.CookieManager;
68
import android.webkit.ValueCallback;
79
import android.webkit.WebView;
810
import android.webkit.WebViewClient;
911

12+
import java.util.HashMap;
13+
import java.util.Map;
14+
1015
/**
1116
* Created by lejard_h on 23/04/2017.
1217
*/
@@ -41,12 +46,16 @@ protected WebView initWebview() {
4146

4247
protected void clearCookies() {
4348
if (getIntent().getBooleanExtra(CLEAR_COOKIES_KEY, false)) {
44-
CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
45-
@Override
46-
public void onReceiveValue(Boolean aBoolean) {
47-
48-
}
49-
});
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
50+
CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
51+
@Override
52+
public void onReceiveValue(Boolean aBoolean) {
53+
54+
}
55+
});
56+
} else {
57+
CookieManager.getInstance().removeAllCookie();
58+
}
5059
}
5160
}
5261

@@ -58,7 +67,7 @@ protected void clearCache() {
5867
}
5968

6069
protected WebViewClient setWebViewClient() {
61-
WebViewClient webViewClient = new WebViewClient();
70+
WebViewClient webViewClient = new BrowserClient();
6271
webView.setWebViewClient(webViewClient);
6372
return webViewClient;
6473
}
@@ -83,4 +92,19 @@ public void onBackPressed() {
8392
FlutterWebviewPlugin.channel.invokeMethod("onBackPressed", null);
8493
super.onBackPressed();
8594
}
95+
96+
97+
private class BrowserClient extends WebViewClient {
98+
private BrowserClient() {
99+
super();
100+
}
101+
102+
@Override
103+
public void onPageStarted(WebView view, String url, Bitmap favicon) {
104+
super.onPageStarted(view, url, favicon);
105+
Map<String, Object> data = new HashMap<>();
106+
data.put("url", url);
107+
FlutterWebviewPlugin.channel.invokeMethod("onUrlChanged", data);
108+
}
109+
}
86110
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.yourcompany.flutter_webview_plugin_example;
22

33
import android.os.Bundle;
4+
45
import io.flutter.app.FlutterActivity;
5-
import io.flutter.plugins.PluginRegistry;
6+
import io.flutter.plugins.GeneratedPluginRegistrant;
67

78
public class MainActivity extends FlutterActivity {
8-
PluginRegistry pluginRegistry;
9-
109
@Override
1110
protected void onCreate(Bundle savedInstanceState) {
1211
super.onCreate(savedInstanceState);
13-
pluginRegistry = new PluginRegistry();
14-
pluginRegistry.registerAll(this);
12+
GeneratedPluginRegistrant.registerWith(this);
1513
}
16-
}
14+
}

example/lib/main.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ class _MyHomePageState extends State<MyHomePage> {
3737
// On destroy stream
3838
StreamSubscription _onDestroy;
3939

40-
TextEditingController _ctrl = new TextEditingController(text: "https://flutter.io");
40+
// On urlChanged stream
41+
StreamSubscription<String> _onUrlChanged;
42+
43+
TextEditingController _ctrl =
44+
new TextEditingController(text: "https://flutter.io");
4145
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
4246

47+
final _history = [];
48+
4349
@override
4450
initState() {
4551
super.initState();
@@ -48,7 +54,17 @@ class _MyHomePageState extends State<MyHomePage> {
4854
_onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
4955
if (mounted) {
5056
// Actions like show a info toast.
51-
_scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
57+
_scaffoldKey.currentState
58+
.showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
59+
}
60+
});
61+
62+
// Add a listener to on url changed
63+
_onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
64+
if (mounted) {
65+
setState(() {
66+
_history.add(url);
67+
});
5268
}
5369
});
5470
}
@@ -57,6 +73,7 @@ class _MyHomePageState extends State<MyHomePage> {
5773
void dispose() {
5874
// Every listener should be canceled, the same should be done with this stream.
5975
_onDestroy?.cancel();
76+
_onUrlChanged?.cancel();
6077

6178
super.dispose();
6279
}
@@ -78,7 +95,8 @@ class _MyHomePageState extends State<MyHomePage> {
7895
new RaisedButton(
7996
onPressed: _onPressed,
8097
child: new Text("Open Webview"),
81-
)
98+
),
99+
new Text(_history.join(", "))
82100
],
83101
),
84102
);

lib/flutter_webview_plugin.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ const _kChannel = 'flutter_webview_plugin';
88
/// Have to be instanciate after `runApp` called.
99
class FlutterWebviewPlugin {
1010
final MethodChannel _channel = const MethodChannel(_kChannel);
11-
StreamController<Null> _onDestroy = new StreamController.broadcast();
12-
StreamController<Null> _onBackPressed = new StreamController.broadcast();
11+
final StreamController<Null> _onDestroy = new StreamController.broadcast();
12+
final StreamController<Null> _onBackPressed =
13+
new StreamController.broadcast();
14+
final StreamController<String> _onUrlChanged =
15+
new StreamController.broadcast();
1316

1417
static FlutterWebviewPlugin _instance;
1518
FlutterWebviewPlugin._() {
@@ -30,6 +33,9 @@ class FlutterWebviewPlugin {
3033
case "onBackPressed":
3134
_onBackPressed.add(null);
3235
break;
36+
case "onUrlChanged":
37+
_onUrlChanged.add(call.arguments["url"]);
38+
break;
3339
}
3440
}
3541

@@ -63,4 +69,8 @@ class FlutterWebviewPlugin {
6369
/// Close the Webview
6470
/// Will trigger the [onDestroy] event
6571
Future<Null> close() => _channel.invokeMethod("close");
72+
73+
/// Listening url changed
74+
///
75+
Stream<String> get onUrlChanged => _onUrlChanged.stream;
6676
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors:
44
- Hadrien Lejard <[email protected]>
55
- Toufik Zitouni <[email protected]>
66
homepage: https://github.com/dart-flitter/flutter_webview_plugin
7-
version: 0.0.5
7+
version: 0.0.7
88

99
flutter:
1010
plugin:

0 commit comments

Comments
 (0)