flutter的UIKitView和platformview之iOS
作者:互联网
flutter添加iOS原生的platformview,
遇到的问题:
//关于编码格式一定要和ios那边统一,并且和传递的参数类型匹配,
//如果flutter传递的是json,map,编码必须是JSONMessageCodec,
//flutter传递的是string,编码必须是Stringcodec,否则会出错,加载不了view
flutter代码:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title),),
body: Container(
width:300,
height:300,
child: UiKitView(
viewType: "iosview",
onPlatformViewCreated:onPlatformViewCreated,
creationParams:{"a":"1"},
//关于编码格式一定要和ios那边统一,并且和传递的参数类型匹配,
//如果flutter传递的是json,map,编码必须是JSONMessageCodec,
//flutter传递的是string,编码必须是Stringcodec,否则会出错,加载不了view
creationParamsCodec:JSONMessageCodec()
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Future<void> onPlatformViewCreated(int id) async {
print("**********ok***********id="+id.toString());
}
iOS代码:
1,创建viewplugin,实现FlutterPlugin协议,在GeneratedPluginRegistrant类中注册插件。
@implementation GeneratedPluginRegistrant
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[ViewPlugin registerWithRegistrar:[registry registrarForPlugin:@"ViewPlugin"]];
}
@end
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface ViewPlugin : NSObject<FlutterPlugin>
@end
NS_ASSUME_NONNULL_END
#import "ViewPlugin.h"
#import "ViewFactory.h"
@implementation ViewPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar
{
ViewFactory *fc = [[ViewFactory alloc] initWith:registrar];
[registrar registerViewFactory:fc withId:@"iosview"];
}
@end
2,创建ViewFactory类实现FlutterPlatformViewFactory协议,
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface ViewFactory : NSObject<FlutterPlatformViewFactory>
- (instancetype)initWith:(NSObject<FlutterPluginRegistrar>*)registrar;
@end
NS_ASSUME_NONNULL_END
#import "ViewFactory.h"
#import "IOSView.h"
@interface ViewFactory()
{
id<FlutterPluginRegistrar> _registrar;
}
@end
@implementation ViewFactory
- (instancetype)initWith:(NSObject<FlutterPluginRegistrar>*)registrar
{
self = [super init];
if (self) {
_registrar = registrar;
}
return self;
}
//实现协议方法,flutter根据widget自动计算出iOS的frame,
//自动生成的viewId和flutter那边对应,flutter传的参数args
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
{
NSLog(@"====---f--==%@",NSStringFromCGRect(frame));
NSLog(@"====-----==%d",viewId);
NSLog(@"=======%@",args);
IOSView *view = [[IOSView alloc] init];
[view createWithFrame:frame viewIdentifier:viewId arguments:args];
return view;
}
//关于编码格式一定要和flutter那边统一,并且和传递的参数类型匹配,
//如果flutter传递的是json,map,编码必须是JSONMessageCodec,
//flutter传递的是string,编码必须是Stringcodec,否则会出错,加载不了view
- (NSObject<FlutterMessageCodec>*)createArgsCodec
{
return [[FlutterJSONMessageCodec alloc] init];
}
@end
3,创建IOSView实现协议,在这个类中创建iOS的view,并返回给flutter
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface IOSView : NSObject<FlutterPlatformView>
- (void)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args;
@end
NS_ASSUME_NONNULL_END
#import "IOSView.h"
@interface IOSView()
{
//这是要添加到flutter上的view,
UIView *_view;
}
@end
@implementation IOSView
- (void)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args
{
_view = [[UIView alloc] initWithFrame:frame];
[_view setBackgroundColor:[UIColor redColor]];
}
//实现协议方法,返回ios的view给flutter
- (UIView*)view
{
return _view;
}
@end
标签:end,iOS,UIKitView,registrar,NSObject,import,flutter,view 来源: https://blog.csdn.net/sinat_34245894/article/details/112707903