使用平臺檢視在 Flutter 應用中託管原生 iOS 檢視
平臺檢視允許你在 Flutter 應用中嵌入原生檢視,這樣你就可以從 Dart 應用轉換、裁剪和調整原生檢視的不透明度。
這使您能夠直接在 Flutter 應用中使用 Android 和 iOS SDK 中的原生 Google Maps。
iOS 僅使用混合組合,這意味著原生 UIView 會被新增到檢視層級結構中。
要在 iOS 上建立平臺檢視,請按照以下說明操作
在 Dart 側
#在 Dart 端,建立一個 Widget 並新增 build 實現,如下面的步驟所示。
在 Dart 小部件檔案中,進行類似於 native_view_example.dart 中所示的更改
新增以下匯入:
dartimport 'package:flutter/foundation.dart'; import 'package:flutter/services.dart';實現
build()方法dartWidget build(BuildContext context) { // This is used in the platform side to register the view. const String viewType = '<platform-view-type>'; // Pass parameters to the platform side. final Map<String, dynamic> creationParams = <String, dynamic>{}; return UiKitView( viewType: viewType, layoutDirection: TextDirection.ltr, creationParams: creationParams, creationParamsCodec: const StandardMessageCodec(), ); }
有關更多資訊,請參閱 API 文件:UIKitView。
在平臺側
#在平臺端,使用 Swift 或 Objective-C
實現工廠和平臺檢視。FLNativeViewFactory 建立平臺檢視,平臺檢視提供對 UIView 的引用。例如,FLNativeView.swift
import Flutter
import UIKit
class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
private var messenger: FlutterBinaryMessenger
init(messenger: FlutterBinaryMessenger) {
self.messenger = messenger
super.init()
}
func create(
withFrame frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?
) -> FlutterPlatformView {
return FLNativeView(
frame: frame,
viewIdentifier: viewId,
arguments: args,
binaryMessenger: messenger)
}
/// Implementing this method is only necessary when the `arguments` in `createWithFrame` is not `nil`.
public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
return FlutterStandardMessageCodec.sharedInstance()
}
}
class FLNativeView: NSObject, FlutterPlatformView {
private var _view: UIView
init(
frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?,
binaryMessenger messenger: FlutterBinaryMessenger?
) {
_view = UIView()
super.init()
// iOS views can be created here
createNativeView(view: _view)
}
func view() -> UIView {
return _view
}
func createNativeView(view _view: UIView){
_view.backgroundColor = UIColor.blue
let nativeLabel = UILabel()
nativeLabel.text = "Native text from iOS"
nativeLabel.textColor = UIColor.white
nativeLabel.textAlignment = .center
nativeLabel.frame = CGRect(x: 0, y: 0, width: 180, height: 48.0)
_view.addSubview(nativeLabel)
}
}最後,註冊平臺檢視。這可以在應用程式或外掛中完成。
對於應用程式註冊,請修改應用程式的 AppDelegate.swift
import Flutter
import UIKit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
guard let pluginRegistrar = self.registrar(forPlugin: "plugin-name") else { return false }
let factory = FLNativeViewFactory(messenger: pluginRegistrar.messenger())
pluginRegistrar.register(
factory,
withId: "<platform-view-type>")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}對於外掛註冊,請修改外掛的主檔案(例如,FLPlugin.swift)
import Flutter
import UIKit
class FLPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let factory = FLNativeViewFactory(messenger: registrar.messenger())
registrar.register(factory, withId: "<platform-view-type>")
}
}在 Objective-C 中,添加工廠和平臺檢視的標頭檔案。例如,如 FLNativeView.h 中所示
#import <flutter flutter.h="">
@interface FLNativeViewFactory : NSObject <flutterplatformviewfactory>
- (instancetype)initWithMessenger:(NSObject<flutterbinarymessenger>*)messenger;
@end
@interface FLNativeView : NSObject <flutterplatformview>
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<flutterbinarymessenger>*)messenger;
- (UIView*)view;
@end實現工廠和平臺檢視。FLNativeViewFactory 建立平臺檢視,平臺檢視提供對 UIView 的引用。例如,FLNativeView.m
#import "FLNativeView.h"
@implementation FLNativeViewFactory {
NSObject<flutterbinarymessenger>* _messenger;
}
- (instancetype)initWithMessenger:(NSObject<flutterbinarymessenger>*)messenger {
self = [super init];
if (self) {
_messenger = messenger;
}
return self;
}
- (NSObject<flutterplatformview>*)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args {
return [[FLNativeView alloc] initWithFrame:frame
viewIdentifier:viewId
arguments:args
binaryMessenger:_messenger];
}
/// Implementing this method is only necessary when the `arguments` in `createWithFrame` is not `nil`.
- (NSObject<fluttermessagecodec>*)createArgsCodec {
return [FlutterStandardMessageCodec sharedInstance];
}
@end
@implementation FLNativeView {
UIView *_view;
}
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<flutterbinarymessenger>*)messenger {
if (self = [super init]) {
_view = [[UIView alloc] init];
}
return self;
}
- (UIView*)view {
return _view;
}
@end最後,註冊平臺檢視。這可以在應用程式或外掛中完成。
對於應用程式註冊,請修改應用程式的 AppDelegate.m
#import "AppDelegate.h"
#import "FLNativeView.h"
#import "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
NSObject<flutterpluginregistrar>* registrar =
[self registrarForPlugin:@"plugin-name"];
FLNativeViewFactory* factory =
[[FLNativeViewFactory alloc] initWithMessenger:registrar.messenger];
[[self registrarForPlugin:@"<plugin-name>"] registerViewFactory:factory
withId:@"<platform-view-type>"];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end對於外掛註冊,請修改主外掛檔案(例如,FLPlugin.m)
#import <flutter flutter.h="">
#import "FLNativeView.h"
@interface FLPlugin : NSObject<flutterplugin>
@end
@implementation FLPlugin
+ (void)registerWithRegistrar:(NSObject<flutterpluginregistrar>*)registrar {
FLNativeViewFactory* factory =
[[FLNativeViewFactory alloc] initWithMessenger:registrar.messenger];
[registrar registerViewFactory:factory withId:@"<platform-view-type>"];
}
@end有關更多資訊,請參閱 API 文件
整合起來
#在 Dart 中實現 build() 方法時,您可以使用 defaultTargetPlatform 來檢測平臺,並決定使用哪個小部件
Widget build(BuildContext context) {
// This is used in the platform side to register the view.
const String viewType = '<platform-view-type>';
// Pass parameters to the platform side.
final Map<String, dynamic> creationParams = <String, dynamic>{};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
// return widget on Android.
case TargetPlatform.iOS:
// return widget on iOS.
case TargetPlatform.macOS:
// return widget on macOS.
default:
throw UnsupportedError('Unsupported platform view');
}
}效能
#Flutter 中的平臺檢視會帶來效能上的權衡。
對於複雜情況,有一些技術可以用來緩解效能問題。
例如,您可以在 Dart 中進行動畫時使用佔位符紋理。換句話說,如果平臺檢視渲染時動畫緩慢,則考慮擷取原生檢視的螢幕截圖並將其渲染為紋理。
組合限制
#在組合 iOS 平臺檢視時存在一些限制。
- 不支援
ShaderMask和ColorFiltered小部件。 - 支援
BackdropFilter小部件,但在如何使用它方面存在一些限制。有關更多詳細資訊,請檢視 iOS 平臺檢視背景濾鏡模糊設計文件。