https://github.com/flutter/plugins/tree/master/packages
1、flutter create --template=plugin --platforms=android,ios -i objc -a java hahahaha
-
–template=plugin:创建插件包
-
–platforms=android,ios:指定平台
-
-i objc -a java:指定ios和安卓的语言
-
最后是工程名称
2、插件交互
- 使用
dependencies:
flutter:
sdk: flutter
hahahaha:
path: ../
import 'package:hahahaha/hahahaha.dart';
String platformVersion;
try {
platformVersion =
await Hahahaha.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
- MethodChannel(方法通道)上层调底层、底层监听上层
- 上层中
class Hahahaha {
static const MethodChannel _channel = MethodChannel('hahahaha');
static Future<String?> get platformVersion async {
// 上层调用invokeMethod方法触发底层setMethodCallHandler方法
final String? version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future<Map<String, String>> sendReq() async {
final Completer<Map<String, String>> completer = Completer();
// 上层调用invokeMethod方法触发底层setMethodCallHandler方法
_channel.invokeMethod('wx_sendReq');
// 底层调用invokeMethod方法触发上层setMethodCallHandler方法
_channel.setMethodCallHandler((call) async {
debugPrint("===授权结果===");
debugPrint("===method===${call.method}");
debugPrint("===arguments===${call.arguments}");
switch (call.method) {
case "onAuthResponse":
completer.complete({"state": call.arguments["state"], "code": call.arguments["code"]});
}
});
return completer.future;
}
}
- 底层中
/** HahahahaPlugin */
public class HahahahaPlugin implements FlutterPlugin, MethodCallHandler {
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "hahahaha");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
@implementation HahahahaPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"hahahaha"
binaryMessenger:[registrar messenger]];
HahahahaPlugin* instance = [[HahahahaPlugin alloc] init];
// 上层调用invokeMethod方法触发底层setMethodCallHandler方法
[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);
}
}
// 底层调用invokeMethod方法触发上层setMethodCallHandler事件
// [channel invokeMethod:@"onAuthResponse" arguments:result];
@end
- EventChannel(事件通道)底层调上层
1.上层中
//创建EventChannel设置监听
final StreamSubscription _eventSubscription = EventChannel('flutter_plugin_event')
.receiveBroadcastStream()
.listen((event) {},onError: (error) {
throw error;
}
)
2.底层中
//创建EventChannel名称跟上层监听保持一致,设置setStreamHandler,setStreamHandler会返回一个监听对象eventSink,eventSink来派发事件
//事件派发对象
private EventChannel.EventSink eventSink = null;
//事件派发流
private EventChannel.StreamHandler streamHandler = new EventChannel.StreamHandler(){
@Override
public void onListen(Objct o, EventChannel.EventSink sink){
eventSink = sink;
}
@Override
public void onCancel(Objct o) {
eventSink = null;
}
};
private FlutterPlugin(Registrar Registrar, MethodChannel channel) {
//初始化事件
EventChannel eventChannel = new EventChannel(Registrar.messenger(),name:"flutter_plugin_event");
eventChannel.setStreamHandler(streamHandler);
}
// 派发事件
if(eventSink != null) {
eventSink.success({'event':'demoEvent','value':'value is 10'});
}
#import "HahahahaPlugin.h"
#import "FlutterPluginEvent.h"
@implementation HahahahaPlugin
FlutterPluginEvent *pluginEvent;
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"hahahaha"
binaryMessenger:[registrar messenger]];
HahahahaPlugin* instance = [[HahahahaPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
pluginEvent = [[FlutterPluginEvent alloc] init];
pluginEvent.eventChannel = [FlutterEventChannel
eventChannelWithName:@"flutter_plugin_event"
binaryMessenger:[registrar messenger]];
[pluginEvent.eventChannel setStreamHandler:pluginEvent];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
}else if ([@"getNative" isEqualToString:call.method]) {
result(@"iOS getNative");
}else if ([@"sayHello" isEqualToString:call.method]) {
result(@{@"sayHello":call.arguments[@"message"],@"info":@"哈哈info"});
FlutterEventSink eventSink = pluginEvent.eventSink;
if(eventSink){
eventSink(@{@"event" : @"DemoEvent",@"value" : @"ios value is 10",});
}
} else {
result(FlutterMethodNotImplemented);
}
}
@end
//event dispatcher header file
#import <Flutter/Flutter.h>
@interface FlutterPluginEvent : NSObject<FlutterStreamHandler>
@property (nonatomic, strong) FlutterEventSink eventSink;
@property (nonatomic, strong) FlutterEventChannel* eventChannel;
@end
#import <Foundation/Foundation.h>
#import "FlutterPluginEvent.h"
#import <objc/runtime.h>
@implementation FlutterPluginEvent
@dynamic eventSink;
- (FlutterEventSink)eventSink
{
return objc_getAssociatedObject(self, _cmd);
}
- (void)setEventSink:(FlutterEventSink)eventSink
{
objc_setAssociatedObject(self, @selector(eventSink), eventSink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (FlutterEventChannel *)eventChannel
{
return objc_getAssociatedObject(self, _cmd);
}
- (void)setEventChannel:(FlutterEventChannel *)eventChannel
{
objc_setAssociatedObject(self, @selector(eventChannel), eventChannel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - FlutterStreamHandler methods
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
self.eventSink = nil;
return nil;
}
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
eventSink:(nonnull FlutterEventSink)sink {
self.eventSink = sink;
return nil;
}
@end
3、发布plugin到pub
-
检查命令
flutter packages pub publish --dry-run -
发布命令
flutter packages pub publish --server=https://pub.dartlang.org -
配置上网
export https_proxy=http://127.0.0.1:41091
export http_proxy=http://127.0.0.1:41091
set https_proxy=https://127.0.0.1:41091
set http_proxy=http://127.0.0.1:41091
网友评论