1、创建工程
flutter create --org com.kinsomy --template=plugin -i swift -a kotlin hello
- --org选项:指定您的组织
- --template选项:1、package 开发Dart包 2、plugin 创建插件包
- -i选项:iOS开发语言swift或者oc
- -a选项:Android开发语言java或者kotlin
目录介绍:
- lib/hello.dart:
插件包的Dart API. - android/src/main/java/com/yourcompany/hello/HelloPlugin.java:
插件包API的Android实现. - ios/Classes/HelloPlugin.m:
插件包API的ios实现. - example/:
一个依赖于该插件的Flutter应用程序,来说明如何使用它
2、Android Studio编辑Android插件包
首先终端运行
cd hello/example
flutter build apk
结构展示
Running Gradle task 'assembleRelease'...
Removed unused resources: Binary resource data reduced from 46KB to 36KB: Removed 20%
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done 207.4s (!)
✓ Built build/app/outputs/flutter-apk/app-release.apk (15.0MB).
localhost:example lc$
打开Android Studio,选择Open an existing Android Studio Project
打开项目
package com.example.lchelper;
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;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Vibrator;
import java.util.HashMap;
import java.util.Map;
/** LchelperPlugin */
public class LchelperPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "lchelper");
channel.setMethodCallHandler(this);
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "lchelper");
channel.setMethodCallHandler(new LchelperPlugin());
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
}
else if (call.method.equals("PackageInfo")){
//获取设备信息
Map<String, String> map = new HashMap<>();
map.put("systemVersion", android.os.Build.VERSION.RELEASE);
map.put("deviceType", android.os.Build.MODEL);
map.put("deviceName", Build.DEVICE);
result.success(map);
}
else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
3、编辑iOS插件包
首先终端操作
cd hello/example
flutter build ios --no-codesign
编辑第三方https://blog.csdn.net/weixin_34162401/article/details/91390081
4、插件发布到pub
在发布之前,确保pubspec.yaml,、README.md以及CHANGELOG.md文件的内容都正确填写完毕。可以通过dry-run
命令来看准备是否就绪
。
flutter packages pub publish --dry-run
检查无误后,可以执行下面的命令,发布到Pub上。
flutter packages pub publish