美文网首页
Flutter插件开发

Flutter插件开发

作者: jackyshan | 来源:发表于2020-10-17 00:12 被阅读0次

    介绍

    Flutter插件是Flutter调用原生能力的一种解构方式。

    Flutter 会通过 MethodChannel 将数据传递给 iOS/ Android ,反之 iOS/ Android 也可以通过 MethodChannel 将数据传回给 Flutter。

    数据类型传递

    插件创建

    在Flutter工程根目录创建plugins目录,在plugins目录下执行下面命令:

    flutter create --org com.example --template=plugin --platforms=android,ios -i objc hello
    

    Flutter和Native传递数据

    Flutter->Native

    class MediaLink {
      static const MethodChannel _channel =
          const MethodChannel('media_link');
    
      static Future<String> get platformVersion async {
        final String version = await _channel.invokeMethod('getPlatformVersion');
        return version;
      }
    
      ///初始化video sdk
      static init() {
        _channel.invokeMethod('init');
      }
    
      ///设置uid
      static setUid(String uid) {
        _channel.invokeMethod('setUid', uid);
      }
    
      ///开启语音连接
      static startVoiceLink(arguments) {
        _channel.invokeMethod('startVoiceLink', arguments);
      }
      
      ///停止语音连接
      static stopVoiceLink() {
        _channel.invokeMethod('stopVoiceLink');
      }
    
      ///获取通话声音
      static getVoicePower() async {
        return _channel.invokeMethod('getVoicePower');
      }
    
      /// 发送消息
      static invoke(String name,[data]) {
        _channel.invokeMethod(name, data);
      }
    }
    

    Native->Flutter

    @implementation MediaLinkPlugin
    + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
        FlutterMethodChannel* channel = [FlutterMethodChannel
                                         methodChannelWithName:@"media_link"
                                         binaryMessenger:[registrar messenger]];
        MediaLinkPlugin* instance = [[MediaLinkPlugin alloc] init];
        [registrar addMethodCallDelegate:instance channel:channel];
    }
    
    - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
        if ([@"init" isEqualToString:call.method]) {
            IKLogI(@"#media# init");
        }
        else if ([@"setUid" isEqualToString:call.method]) {
            [VideoManager settingUserId:call.arguments];
            IKLogI(@"#media# setUid");
        }
        else if ([@"startVoiceLink" isEqualToString:call.method]) {
            IKLogI(@"#media# startVoiceLink");
            [[MediaVoiceLink sharedInstance] createVoiceCallLink:call.arguments];
        }
        else if ([@"stopVoiceLink" isEqualToString:call.method]) {
            IKLogI(@"#media# stopVoiceLink");
            [[MediaVoiceLink sharedInstance] stopVoiceLink];
        }
        else if ([@"getVoicePower" isEqualToString:call.method]) {
            IKLogI(@"#media_heart# invoke getVoicePower");
            result([[MediaVoiceLink sharedInstance] getVoicePower]);
        }
        else if ([@"useSpeaker" isEqualToString:call.method]) {
            [[MediaVoiceLink sharedInstance] speaker:[call.arguments boolValue]];
            IKLogI(@"#media# invoke %@ %@", call.method, call.arguments);
        }
        else if ([@"setMute" isEqualToString:call.method]) {
            [[MediaVoiceLink sharedInstance] mute:[call.arguments boolValue]];
            IKLogI(@"#media# invoke %@ %@", call.method, call.arguments);
        }
    }
    

    相关文章

      网友评论

          本文标题:Flutter插件开发

          本文链接:https://www.haomeiwen.com/subject/usugektx.html