一、新建plugin
由于1、2 方式未生成平台对应文件,我一般使用第三种命令行方式创建
1. 通过IntelliJ
截屏2020-10-29 17.23.24.png(1) Create New Project 或者 点击 File>New>Project…;
(2) 在左侧菜单选择 Flutter, 然后点击 Next;
(3) 输入 Project name 和 Project location,Project type 选择 "Plugin";
(4)最后点击 Finish。
2. 通过Android Studio
截屏2020-10-29 19.31.28.png(1) Start a new flutter project -> flutter plugin ,然后点击 Next;
截屏2020-10-29 19.32.25.png(2) 选择语言: 不勾选iOS 为OC,安卓为Java
上述两种方法创建过后需要手动在插件 pubspec.yaml 文件中 设置每个平台对应的类名等信息,文档描述说在插件目录下执行 flutter packages get
后会生成对应的平台对应目录(如ios、android),不知道为什么未生效,如果有人知道该怎么做欢迎联系我
3. 通过命令行
具体参数设置可参考flutter 文档
例:
flutter create --org com.example.nativeapi --template=plugin --platforms=android,ios -i objc native_api
该命令会在插件下自动创建好ios 及 android 目录
截屏2020-10-29 20.16.08.png
二、增加方法
例如想要增加一个方法获取字符串长度
1. dart (native_api/lib/native_api.dart)
默认创建的文件如下
class Nativeapi {
static const MethodChannel _channel =
const MethodChannel('nativeapi');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
新增一个countOfString
class NativeApi {
static const MethodChannel _channel = const MethodChannel('native_api');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future<String> countOfString(str) async {
final String count = await _channel.invokeMethod('countOfString', str);
return count;
}
}
2. ios (native_api/ios/Classes/NativeApiPlugin.m)
创建后自动生成的代码如下
#import "NativeApiPlugin.h"
@implementation NativeApiPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"native_api"
binaryMessenger:[registrar messenger]];
NativeApiPlugin* instance = [[NativeApiPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else {
result(FlutterMethodNotImplemented);
}
}
@end
对countOfString 方法做具体处理,改完后文件代码为
#import "NativeApiPlugin.h"
@implementation NativeApiPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"native_api"
binaryMessenger:[registrar messenger]];
NativeApiPlugin* instance = [[NativeApiPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
}else if ([@"countOfString" isEqualToString:call.method]) {
NSString *urlParam = call.arguments;
result(@"字符串长度为 3");
}else {
result(FlutterMethodNotImplemented);
}
}
@end
三、在其他flutter工程中使用本地plugin
新建plugins 文件,将刚才的plugin拷贝进去
截屏2020-10-29 21.22.53.png修改工程的pubspec文件
截屏2020-10-29 21.24.46.png在工程中引入
import 'package:native_api/native_api.dart'
使用
var futureValue = await NativeApi.countOfString("abc");
结果:
截屏2020-10-29 21.34.27.png想要让更多人用可以使用命令发布插件
$flutter packages pub publish --dry-run
$flutter packages pub publish
网友评论