Flutter添加iOS原生的platformview,
遇到的问题:
//关于编码格式一定要和ios那边统一,并且和传递的参数类型匹配,
//如果Flutter传递的是json,map,编码必须是JSONMessageCodec,
//Flutter传递的是string,编码必须是Stringcodec,否则会出错,加载不了view
@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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。