bounty将在3天后过期。回答此问题可获得+500的声望奖励。Pritish希望吸引更多人关注此问题。
我正在尝试托管一个iOS UIImageView
,以便在Flutter
应用中显示QR代码。我能够正确显示QR代码。现在,我想从flutter
向iOS native side
传递额外的参数
下面是我的flutter
代码
class CealQrView extends StatelessWidget {
const CealQrView({required this.text, super.key});
final String text;
@override
Widget build(BuildContext context) {
// Pass parameters to the platform side.
final Map<String, dynamic> creationParams = <String, dynamic>{};
creationParams["text"] = text;
return UiKitView(
viewType: cealQrView,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
}
如您所见,我将creationParams
从flutter
端传递到native iOS
端
下面是我的iOS代码
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)
}
}
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
debugPrint("Arguments ")
debugPrint(args)
debugPrint(messenger)
if let args = args as? Dictionary<String, Any>,
let textValue = args["text"] as? String {
// please check the "as" above - wasn't able to test
// handle the method
debugPrint(textValue) } else {
debugPrint("Else")
}
debugPrint("\(viewId)")
createNativeView(view: _view)
}
func view() -> UIView {
return _view
}
func createNativeView(view _view: UIView){
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0,width: 100,height: 100)
imageView.image = generateQRCode(from: "Hello")
_view.addSubview(imageView)
}
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output)
}
}
return nil
}
}
我试图在FLNativeView
的init
块中打印,但总是得到args
为空
1条答案
按热度按时间hxzsmxv21#
添加此方法:
像这样在你的
FLNativeViewFactory
里面