Skip to content

Commit ed4d12b

Browse files
committed
add android view
1 parent 7b02a0e commit ed4d12b

File tree

14 files changed

+401
-58
lines changed

14 files changed

+401
-58
lines changed

android/app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ apply plugin: 'kotlin-android'
1616
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
1717

1818
android {
19-
compileSdkVersion 27
19+
compileSdkVersion 28
2020

2121
sourceSets {
2222
main.java.srcDirs += 'src/main/kotlin'
@@ -30,7 +30,7 @@ android {
3030
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
3131
applicationId "com.shuyu.gsygithub.gsygithubappflutter"
3232
minSdkVersion 16
33-
targetSdkVersion 27
33+
targetSdkVersion 28
3434
versionCode 14
3535
versionName "1.1.4"
3636
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -67,7 +67,7 @@ flutter {
6767
}
6868

6969
dependencies {
70-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
70+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7171
testImplementation 'junit:junit:4.12'
7272
androidTestImplementation 'com.android.support.test:runner:1.0.1'
7373
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

android/app/src/main/kotlin/com/shuyu/gsygithub/gsygithubappflutter/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import android.os.Bundle
44

55
import io.flutter.app.FlutterActivity
66
import io.flutter.plugins.GeneratedPluginRegistrant
7+
import com.shuyu.gsygithub.gsygithubappflutter.webivew.WebviewFlutterPlugin
78

89
class MainActivity(): FlutterActivity() {
910
override fun onCreate(savedInstanceState: Bundle?) {
1011
super.onCreate(savedInstanceState)
1112
GeneratedPluginRegistrant.registerWith(this)
1213
UpdateAlbumPlugin.register(this, flutterView)
14+
WebviewFlutterPlugin.registerWith(flutterView.pluginRegistry.registrarFor("com.shuyu.gsygithub.gsygithubappflutter"))
1315
}
1416
}

android/app/src/main/kotlin/com/shuyu/gsygithub/gsygithubappflutter/UpdateAlbumPlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ object UpdateAlbumPlugin {
1919
fun register(context: Context, messenger: BinaryMessenger) = MethodChannel(messenger, ChannelName).setMethodCallHandler { methodCall, result ->
2020
when (methodCall.method) {
2121
"updateAlbum" -> {
22-
val path: String = methodCall.argument("path")
23-
val name: String = methodCall.argument("name")
22+
val path: String? = methodCall.argument("path")
23+
val name: String? = methodCall.argument("name")
2424
try {
2525
MediaStore.Images.Media.insertImage(context.contentResolver, path, name, null)
2626
} catch (e: Exception) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.shuyu.gsygithub.gsygithubappflutter.webivew
2+
3+
/**
4+
* Created by guoshuyu
5+
* Date: 2018-10-17
6+
*/
7+
import android.content.Context
8+
import io.flutter.plugin.common.BinaryMessenger
9+
import io.flutter.plugin.common.StandardMessageCodec
10+
import io.flutter.plugin.platform.PlatformView
11+
import io.flutter.plugin.platform.PlatformViewFactory
12+
13+
class WebViewFactory(private val messenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
14+
15+
override fun create(context: Context, id: Int, args: Any): PlatformView {
16+
val params = args as Map<String, Any>
17+
return FlutterWebView(context, messenger, id, params)
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.shuyu.gsygithub.gsygithubappflutter.webivew
2+
3+
/**
4+
* Created by guoshuyu
5+
* Date: 2018-10-17
6+
*/
7+
import io.flutter.plugin.common.PluginRegistry.Registrar
8+
9+
/** WebviewFlutterPlugin */
10+
object WebviewFlutterPlugin {
11+
/** Plugin registration. */
12+
fun registerWith(registrar: Registrar) {
13+
registrar.platformViewRegistry().registerViewFactory("plugins.flutter.io/webview", WebViewFactory(registrar.messenger()))
14+
}
15+
}

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
buildscript {
2-
ext.kotlin_version = '1.1.51'
2+
ext.kotlin_version = '1.2.71'
33
repositories {
44
google()
55
jcenter()
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.0.1'
9+
classpath 'com.android.tools.build:gradle:3.2.0'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}

android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

lib/common/dao/ReposDao.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:io';
44

55
import 'package:dio/dio.dart';
66
import 'package:fluttertoast/fluttertoast.dart';
7-
import 'package:get_version/get_version.dart';
87
import 'package:gsy_github_app_flutter/common/ab/provider/repos/ReadHistoryDbProvider.dart';
98
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryCommitsDbProvider.dart';
109
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryDetailDbProvider.dart';
@@ -32,6 +31,7 @@ import 'package:gsy_github_app_flutter/common/net/trending/GithubTrending.dart';
3231
import 'package:gsy_github_app_flutter/common/redux/TrendRedux.dart';
3332
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
3433
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
34+
import 'package:package_info/package_info.dart';
3535
import 'package:pub_semver/pub_semver.dart';
3636
import 'package:redux/redux.dart';
3737

@@ -645,7 +645,10 @@ class ReposDao {
645645
if (Config.DEBUG) {
646646
print("versionName " + versionName);
647647
}
648-
var appVersion = await GetVersion.projectVersion;
648+
649+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
650+
var appVersion = packageInfo.version;
651+
649652
if (Config.DEBUG) {
650653
print("appVersion " + appVersion);
651654
}

lib/page/HomePage.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:gsy_github_app_flutter/page/TrendPage.dart';
1111
import 'package:gsy_github_app_flutter/widget/GSYTabBarWidget.dart';
1212
import 'package:gsy_github_app_flutter/widget/GSYTitleBar.dart';
1313
import 'package:gsy_github_app_flutter/widget/HomeDrawer.dart';
14+
import 'package:gsy_github_app_flutter/widget/webview/WebView.dart';
1415

1516
/**
1617
* 主页
@@ -53,6 +54,7 @@ class HomePage extends StatelessWidget {
5354
_renderTab(GSYICons.MAIN_DT, CommonUtils.getLocale(context).home_dynamic),
5455
_renderTab(GSYICons.MAIN_QS, CommonUtils.getLocale(context).home_trend),
5556
_renderTab(GSYICons.MAIN_MY, CommonUtils.getLocale(context).home_my),
57+
_renderTab(GSYICons.MAIN_MY, CommonUtils.getLocale(context).home_my),
5658
];
5759
return WillPopScope(
5860
onWillPop: () {
@@ -66,6 +68,9 @@ class HomePage extends StatelessWidget {
6668
new DynamicPage(),
6769
new TrendPage(),
6870
new MyPage(),
71+
new WebView(initialUrl: 'https://www.baidu.com',
72+
javaScriptMode: JavaScriptMode.unrestricted,
73+
),
6974
],
7075
backgroundColor: GSYColors.primarySwatch,
7176
indicatorColor: Color(GSYColors.white),

0 commit comments

Comments
 (0)