Browse Source

Android plugin 编写基本的textview demo

ykrank 5 years ago
parent
commit
967477ae1c

+ 12
- 17
android/src/main/kotlin/com/shuangyubang/mofun_flutter_plugin_video/MofunFlutterPluginVideoPlugin.kt View File

@@ -1,25 +1,20 @@
1 1
 package com.shuangyubang.mofun_flutter_plugin_video
2 2
 
3
-import io.flutter.plugin.common.MethodCall
4 3
 import io.flutter.plugin.common.MethodChannel
5
-import io.flutter.plugin.common.MethodChannel.MethodCallHandler
6
-import io.flutter.plugin.common.MethodChannel.Result
7 4
 import io.flutter.plugin.common.PluginRegistry.Registrar
8 5
 
9
-class MofunFlutterPluginVideoPlugin: MethodCallHandler {
10
-  companion object {
11
-    @JvmStatic
12
-    fun registerWith(registrar: Registrar) {
13
-      val channel = MethodChannel(registrar.messenger(), "mofun_flutter_plugin_video")
14
-      channel.setMethodCallHandler(MofunFlutterPluginVideoPlugin())
15
-    }
16
-  }
17 6
 
18
-  override fun onMethodCall(call: MethodCall, result: Result) {
19
-    if (call.method == "getPlatformVersion") {
20
-      result.success("Android ${android.os.Build.VERSION.RELEASE}")
21
-    } else {
22
-      result.notImplemented()
7
+class MofunFlutterPluginVideoPlugin {
8
+    companion object {
9
+        @JvmStatic
10
+        fun registerWith(registrar: Registrar) {
11
+            val channel = MethodChannel(registrar.messenger(), "mofun_flutter_plugin_video")
12
+            channel.setMethodCallHandler(TestPlugin())
13
+
14
+            registrar.platformViewRegistry()
15
+                    .registerViewFactory(Channel_Mofun_Video_Info,
16
+                            MofunVideoViewFactory(registrar.messenger()))
17
+        }
23 18
     }
24
-  }
25 19
 }
20
+

+ 55
- 0
android/src/main/kotlin/com/shuangyubang/mofun_flutter_plugin_video/MofunVideoView.kt View File

@@ -0,0 +1,55 @@
1
+package com.shuangyubang.mofun_flutter_plugin_video
2
+
3
+import android.content.Context
4
+import android.graphics.Color
5
+import android.view.View
6
+import android.widget.TextView
7
+import io.flutter.plugin.common.BinaryMessenger
8
+import io.flutter.plugin.common.MethodCall
9
+import io.flutter.plugin.common.MethodChannel
10
+import io.flutter.plugin.common.StandardMessageCodec
11
+import io.flutter.plugin.platform.PlatformView
12
+import io.flutter.plugin.platform.PlatformViewFactory
13
+
14
+
15
+const val Channel_Mofun_Video_Info = "plugins.shuangyunbang.mofun/mofunvideoview"
16
+
17
+class MofunVideoView(context: Context, messenger: BinaryMessenger, id: Int) : PlatformView, MethodChannel.MethodCallHandler {
18
+
19
+    private val textView: TextView = TextView(context).also {
20
+        it.setBackgroundColor(Color.BLUE)
21
+    }
22
+    private val methodChannel: MethodChannel = MethodChannel(messenger, Channel_Mofun_Video_Info + "_$id")
23
+            .also { it.setMethodCallHandler(this@MofunVideoView) }
24
+
25
+
26
+    override fun getView(): View {
27
+        return textView
28
+    }
29
+
30
+    override fun dispose() {
31
+
32
+    }
33
+
34
+    override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
35
+        when (methodCall.method) {
36
+            "setText" -> setText(methodCall, result)
37
+            else -> result.notImplemented()
38
+        }
39
+    }
40
+
41
+    private fun setText(methodCall: MethodCall, result:MethodChannel.Result) {
42
+        val text = methodCall.arguments as String
43
+        textView.text = text
44
+        result.success(null)
45
+    }
46
+
47
+}
48
+
49
+class MofunVideoViewFactory(val messenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
50
+
51
+    override fun create(context: Context, id: Int, o: Any?): PlatformView {
52
+        return MofunVideoView(context, messenger, id)
53
+    }
54
+
55
+}

+ 15
- 0
android/src/main/kotlin/com/shuangyubang/mofun_flutter_plugin_video/TestPlugin.kt View File

@@ -0,0 +1,15 @@
1
+package com.shuangyubang.mofun_flutter_plugin_video
2
+
3
+import io.flutter.plugin.common.MethodCall
4
+import io.flutter.plugin.common.MethodChannel
5
+
6
+class TestPlugin : MethodChannel.MethodCallHandler {
7
+
8
+    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
9
+        if (call.method == "getPlatformVersion") {
10
+            result.success("Android ${android.os.Build.VERSION.RELEASE}")
11
+        } else {
12
+            result.notImplemented()
13
+        }
14
+    }
15
+}

+ 2
- 2
example/android/build.gradle View File

@@ -1,12 +1,12 @@
1 1
 buildscript {
2
-    ext.kotlin_version = '1.2.71'
2
+    ext.kotlin_version = '1.3.21'
3 3
     repositories {
4 4
         google()
5 5
         jcenter()
6 6
     }
7 7
 
8 8
     dependencies {
9
-        classpath 'com.android.tools.build:gradle:3.2.1'
9
+        classpath 'com.android.tools.build:gradle:3.4.0'
10 10
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11 11
     }
12 12
 }

+ 2
- 2
example/android/gradle/wrapper/gradle-wrapper.properties View File

@@ -1,6 +1,6 @@
1
-#Fri Jun 23 08:50:38 CEST 2017
1
+#Tue Apr 30 15:37:07 CST 2019
2 2
 distributionBase=GRADLE_USER_HOME
3 3
 distributionPath=wrapper/dists
4 4
 zipStoreBase=GRADLE_USER_HOME
5 5
 zipStorePath=wrapper/dists
6
-distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
6
+distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-all.zip

+ 1
- 0
example/ios/Flutter/Debug.xcconfig View File

@@ -1 +1,2 @@
1
+#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
1 2
 #include "Generated.xcconfig"

+ 1
- 0
example/ios/Flutter/Release.xcconfig View File

@@ -1 +1,2 @@
1
+#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
1 2
 #include "Generated.xcconfig"

+ 71
- 0
example/ios/Podfile View File

@@ -0,0 +1,71 @@
1
+# Uncomment this line to define a global platform for your project
2
+# platform :ios, '9.0'
3
+
4
+# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
5
+ENV['COCOAPODS_DISABLE_STATS'] = 'true'
6
+
7
+project 'Runner', {
8
+  'Debug' => :debug,
9
+  'Profile' => :release,
10
+  'Release' => :release,
11
+}
12
+
13
+def parse_KV_file(file, separator='=')
14
+  file_abs_path = File.expand_path(file)
15
+  if !File.exists? file_abs_path
16
+    return [];
17
+  end
18
+  pods_ary = []
19
+  skip_line_start_symbols = ["#", "/"]
20
+  File.foreach(file_abs_path) { |line|
21
+      next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
22
+      plugin = line.split(pattern=separator)
23
+      if plugin.length == 2
24
+        podname = plugin[0].strip()
25
+        path = plugin[1].strip()
26
+        podpath = File.expand_path("#{path}", file_abs_path)
27
+        pods_ary.push({:name => podname, :path => podpath});
28
+      else
29
+        puts "Invalid plugin specification: #{line}"
30
+      end
31
+  }
32
+  return pods_ary
33
+end
34
+
35
+target 'Runner' do
36
+  use_frameworks!
37
+
38
+  # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
39
+  # referring to absolute paths on developers' machines.
40
+  system('rm -rf .symlinks')
41
+  system('mkdir -p .symlinks/plugins')
42
+
43
+  # Flutter Pods
44
+  generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig')
45
+  if generated_xcode_build_settings.empty?
46
+    puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first."
47
+  end
48
+  generated_xcode_build_settings.map { |p|
49
+    if p[:name] == 'FLUTTER_FRAMEWORK_DIR'
50
+      symlink = File.join('.symlinks', 'flutter')
51
+      File.symlink(File.dirname(p[:path]), symlink)
52
+      pod 'Flutter', :path => File.join(symlink, File.basename(p[:path]))
53
+    end
54
+  }
55
+
56
+  # Plugin Pods
57
+  plugin_pods = parse_KV_file('../.flutter-plugins')
58
+  plugin_pods.map { |p|
59
+    symlink = File.join('.symlinks', 'plugins', p[:name])
60
+    File.symlink(p[:path], symlink)
61
+    pod p[:name], :path => File.join(symlink, 'ios')
62
+  }
63
+end
64
+
65
+post_install do |installer|
66
+  installer.pods_project.targets.each do |target|
67
+    target.build_configurations.each do |config|
68
+      config.build_settings['ENABLE_BITCODE'] = 'NO'
69
+    end
70
+  end
71
+end

+ 20
- 3
example/lib/main.dart View File

@@ -1,8 +1,9 @@
1
-import 'package:flutter/material.dart';
2 1
 import 'dart:async';
3 2
 
3
+import 'package:flutter/material.dart';
4 4
 import 'package:flutter/services.dart';
5 5
 import 'package:mofun_flutter_plugin_video/mofun_flutter_plugin_video.dart';
6
+import 'package:mofun_flutter_plugin_video/mofun_video_view.dart';
6 7
 
7 8
 void main() => runApp(MyApp());
8 9
 
@@ -47,10 +48,26 @@ class _MyAppState extends State<MyApp> {
47 48
         appBar: AppBar(
48 49
           title: const Text('Plugin example app'),
49 50
         ),
50
-        body: Center(
51
-          child: Text('Running on: $_platformVersion\n'),
51
+        body: Column(
52
+          children: <Widget>[
53
+            Container(
54
+              alignment: Alignment.center,
55
+              height: 48,
56
+              child: Text('Running on: $_platformVersion'),
57
+            ),
58
+            Container(
59
+              height: 100.0,
60
+              child: MofunVideoView(
61
+                onMofunVideoViewCreated: _onMofunVideoViewCreated,
62
+              ),
63
+            ),
64
+          ],
52 65
         ),
53 66
       ),
54 67
     );
55 68
   }
56 69
 }
70
+
71
+void _onMofunVideoViewCreated(MofunVideoViewController controller) {
72
+  controller.setText('Hello from Android!');
73
+}

+ 5
- 3
lib/mofun_video_view.dart View File

@@ -6,6 +6,8 @@ import 'package:flutter/widgets.dart';
6 6
 
7 7
 typedef void MofunVideoViewCreatedCallback(MofunVideoViewController controller);
8 8
 
9
+const _CHANNEL = 'plugins.shuangyunbang.mofun/mofunvideoview';
10
+
9 11
 class MofunVideoView extends StatefulWidget {
10 12
   final MofunVideoViewCreatedCallback onMofunVideoViewCreated;
11 13
 
@@ -26,12 +28,12 @@ class _MofunVideoViewState extends State<MofunVideoView> {
26 28
   Widget build(BuildContext context) {
27 29
     if (defaultTargetPlatform == TargetPlatform.android) {
28 30
       return AndroidView(
29
-        viewType: 'plugins.shuangyunbang.mofun/mofunvideoview',
31
+        viewType: _CHANNEL,
30 32
         onPlatformViewCreated: _onPlatformViewCreated,
31 33
       );
32 34
     }else if (defaultTargetPlatform == TargetPlatform.iOS) {
33 35
       return UiKitView(
34
-        viewType: 'plugins.shuangyunbang.mofun/mofunvideoview',
36
+        viewType: _CHANNEL,
35 37
         onPlatformViewCreated: _onPlatformViewCreated,
36 38
       );
37 39
     }
@@ -49,7 +51,7 @@ class _MofunVideoViewState extends State<MofunVideoView> {
49 51
 
50 52
 class MofunVideoViewController {
51 53
   MofunVideoViewController._(int id)
52
-      : _channel = new MethodChannel('plugins.shuangyunbang.mofun/mofunvideoview_$id');
54
+      : _channel = new MethodChannel('${_CHANNEL}_$id');
53 55
 
54 56
   final MethodChannel _channel;
55 57