准备工作
环境:macOS
所使用的Flutter版本:2.0.5
软件:AndroidStudio | VSCode | Xcode
开始
或使用终端
键入:flutter create --flutter_native_cache_image=plugin
一路next就行了。
MethodChannel:
要实现Flutter与iOS和Android的交互就是通过这个MethodChannel
。MethodChannel
就是我们的信使,负责dart和原生代码通信。flutter_device_gyro_provider
是MethodChannel
的名字,Flutter通过一个具体的名字能才够在对应平台上找到对应的MethodChannel
,从而实现Flutter与平台的交互。同样地,我们在对应的平台上也要注册名为flutter_device_gyro_provider
的MethodChannel
先来看一下这三个路径下的文件
Android:android/src/main/kotlin/com/renren/flutter_device_gyro_provider/FlutterDeviceGyroProviderPlugin.kt
iOS:ios/Classes/SwiftFlutterDeviceGyroProviderPlugin.swift
Flutter:lib/flutter_device_gyro_provider.dart
flutter_device_gyro_provider.dart
class FlutterDeviceGyroProvider {
static const MethodChannel _channel =
const MethodChannel('flutter_device_gyro_provider');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
SwiftFlutterDeviceGyroProviderPlugin.swift
import Flutter
import UIKit
public class SwiftFlutterDeviceGyroProviderPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_device_gyro_provider", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterDeviceGyroProviderPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}
FlutterDeviceGyroProviderPlugin.kt
package com.renren.flutter_device_gyro_provider
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
/** FlutterDeviceGyroProviderPlugin */
class FlutterDeviceGyroProviderPlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_device_gyro_provider")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
MethodChannel的定义
static const MethodChannel _channel = const MethodChannel('flutter_device_gyro_provider');
flutter通过一个具体的名字能才够在对应平台上找到对应的MethodChannel,从而实现flutter与平台的交互
以上基本过程Flutter已在创建时自动生成;
原生调用Flutter
Flutter调用原生
iOS:
插件Plugins上传:
把整体插件项目上传github或者gitLab,记录对应的github |gitlab 地址。
配置 pubspec.yaml
文件
name: flutter_device_gyro_provider#插件的名称,改动的时候需要注意(尽量为当初所建的项目名称)
description: 陀螺仪 # 插件的简介
version: 0.0.1 # 当前插件版本,每一次发布都不能和以前版本号一样
author: Ming <418589912@qq.com> # 作者信息,会显示到 https://pub.flutter-io.cn 中
homepage: https://github.com/LittleJamie/flutter_device_gyro_provider#项目的git地址
执行下面指令进行发布
flutter packages pub publish --server=https://pub.dartlang.org
Package has 1 warning.. Do you want to publish flutter_device_gyro_provider 0.0.1 (y/N)? y
输入y
遇到问题:
LICENSE file `LICENSE` contains generic TODO.
pub finished with exit code 1
解决方法:
文件路径:flutter_device_gyro_provider/LICENSE
添加:
Author 替换为作者名字
The MIT License (MIT)
Copyright (c) 2021 Author
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
成功上传
Uploading...
Successfully uploaded package.
ming@bogon flutter_device_gyro_provider %