一鬓照、新建plugin
由于1、2 方式未生成平臺(tái)對(duì)應(yīng)文件询件,我一般使用第三種命令行方式創(chuàng)建
1. 通過(guò)IntelliJ
(1) Create New Project 或者 點(diǎn)擊 File>New>Project…带污;
(2) 在左側(cè)菜單選擇 Flutter, 然后點(diǎn)擊 Next;
(3) 輸入 Project name 和 Project location专钉,Project type 選擇 "Plugin";
(4)最后點(diǎn)擊 Finish累铅。
2. 通過(guò)Android Studio
(1) Start a new flutter project -> flutter plugin ,然后點(diǎn)擊 Next跃须;
(2) 選擇語(yǔ)言: 不勾選iOS 為OC,安卓為Java
上述兩種方法創(chuàng)建過(guò)后需要手動(dòng)在插件 pubspec.yaml 文件中 設(shè)置每個(gè)平臺(tái)對(duì)應(yīng)的類(lèi)名等信息娃兽,文檔描述說(shuō)在插件目錄下執(zhí)行 flutter packages get
后會(huì)生成對(duì)應(yīng)的平臺(tái)對(duì)應(yīng)目錄(如ios菇民、android),不知道為什么未生效投储,如果有人知道該怎么做歡迎聯(lián)系我
3. 通過(guò)命令行
具體參數(shù)設(shè)置可參考flutter 文檔
例:
flutter create --org com.example.nativeapi --template=plugin --platforms=android,ios -i objc native_api
該命令會(huì)在插件下自動(dòng)創(chuàng)建好ios 及 android 目錄
二第练、增加方法
例如想要增加一個(gè)方法獲取字符串長(zhǎng)度
1. dart (native_api/lib/native_api.dart)
默認(rèn)創(chuàng)建的文件如下
class Nativeapi {
static const MethodChannel _channel =
const MethodChannel('nativeapi');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
新增一個(gè)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)
創(chuàng)建后自動(dòng)生成的代碼如下
#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
對(duì)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(@"字符串長(zhǎng)度為 3");
}else {
result(FlutterMethodNotImplemented);
}
}
@end
三、在其他flutter工程中使用本地plugin
新建plugins 文件玛荞,將剛才的plugin拷貝進(jìn)去
修改工程的pubspec文件
在工程中引入
import 'package:native_api/native_api.dart'
使用
var futureValue = await NativeApi.countOfString("abc");
結(jié)果:
想要讓更多人用可以使用命令發(fā)布插件
$flutter packages pub publish --dry-run
$flutter packages pub publish