1+ package com.shuyu.gsygithub.gsygithubappflutter.webivew
2+
3+ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
4+ import io.flutter.plugin.common.MethodChannel.Result
5+
6+ import android.content.Context
7+ import android.view.View
8+ import android.webkit.WebView
9+ import io.flutter.plugin.common.BinaryMessenger
10+ import io.flutter.plugin.common.MethodCall
11+ import io.flutter.plugin.common.MethodChannel
12+ import io.flutter.plugin.platform.PlatformView
13+
14+
15+
16+ /* *
17+ * Created by guoshuyu
18+ * Date: 2018-10-17
19+ */
20+
21+ class FlutterWebView (context : Context , messenger : BinaryMessenger , id : Int , params : Map <String , Any >) : PlatformView, MethodCallHandler {
22+
23+ private val webView: WebView
24+
25+ private val methodChannel: MethodChannel
26+
27+ init {
28+ webView = WebView (context)
29+ if (params.containsKey(" initialUrl" )) {
30+ val url = params[" initialUrl" ] as String
31+ webView.loadUrl(url)
32+ }
33+ applySettings(params[" settings" ] as Map <String , Any >)
34+ methodChannel = MethodChannel (messenger, " plugins.flutter.io/webview_$id " )
35+ methodChannel.setMethodCallHandler(this )
36+ }
37+
38+ override fun getView (): View {
39+ return webView
40+ }
41+
42+ override fun onMethodCall (methodCall : MethodCall , result : Result ) {
43+ when (methodCall.method) {
44+ " loadUrl" -> loadUrl(methodCall, result)
45+ " updateSettings" -> updateSettings(methodCall, result)
46+ else -> result.notImplemented()
47+ }
48+ }
49+
50+ private fun loadUrl (methodCall : MethodCall , result : Result ) {
51+ val url = methodCall.arguments as String
52+ webView.loadUrl(url)
53+ result.success(null )
54+ }
55+
56+ private fun updateSettings (methodCall : MethodCall , result : Result ) {
57+ applySettings(methodCall.arguments as Map <String , Any >)
58+ result.success(null )
59+ }
60+
61+ private fun applySettings (settings : Map <String , Any >) {
62+ for (key in settings.keys) {
63+ when (key) {
64+ " jsMode" -> updateJsMode(settings[key] as Int )
65+ else -> throw IllegalArgumentException (" Unknown WebView setting: $key " )
66+ }
67+ }
68+ }
69+
70+ private fun updateJsMode (mode : Int ) {
71+ when (mode) {
72+ 0 // disabled
73+ -> webView.settings.javaScriptEnabled = false
74+ 1 // unrestricted
75+ -> webView.settings.javaScriptEnabled = true
76+ else -> throw IllegalArgumentException (" Trying to set unknown Javascript mode: $mode " )
77+ }
78+ }
79+
80+ override fun dispose () {}
81+ }
0 commit comments