swift 在Flutter中托管iosView时无法传递参数

hwamh0ep  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(103)

bounty将在3天后过期。回答此问题可获得+500的声望奖励。Pritish希望吸引更多人关注此问题。

我正在尝试托管一个iOS UIImageView,以便在Flutter应用中显示QR代码。我能够正确显示QR代码。现在,我想从flutteriOS 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(),
          );
  }
}

如您所见,我将creationParamsflutter端传递到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
    }
}

我试图在FLNativeViewinit块中打印,但总是得到args为空

hxzsmxv2

hxzsmxv21#

添加此方法:

public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
          return FlutterStandardMessageCodec.sharedInstance()
    }

像这样在你的FLNativeViewFactory里面

class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger

    init(messenger: FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }

public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
          return FlutterStandardMessageCodec.sharedInstance()
    }

    func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return FLNativeView(
            frame: frame,
            viewIdentifier: viewId,
            arguments: args,
            binaryMessenger: messenger)
    }
}

相关问题