美文网首页
Flutter学习三-知识小集

Flutter学习三-知识小集

作者: 乔克蜀黍 | 来源:发表于2020-11-16 00:02 被阅读0次

    常用方法

    1. flutter构造函数
      flutter构造函数的属性可以使可选的,当属性是可选时,可选属性放在花括号的里面,不可选属性放在花括号的外面。构造函数写法为:Class(this.~~,{this.~,this.~,this.~~})
    2. GestureDetector 手势类
    3. MediaQuery.of(context).size.width 屏幕宽
    4. Expanded Widget主轴上的可用空间

    flutter与原生的交互

    创建与原生有交互的flutter工程应选择Flutter Moule来创建flutter工程,原生工程应与flutter工程在同一目录下,在原生工程里通过cocoapods来下载安装与flutter工程交互的中间库

    1. 原生调用flutter

    在iOS里通过FlutterMethodChannel对象来向flutter工程发送和监听flutter页面发来的消息,FlutterMethodChannel对象既是消息的发送者又是消息的监听者。

    用法:

    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:@"one_page" binaryMessenger:self.flutterVC];
        [methodChannel invokeMethod:@"one" arguments:nil];
        [methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
            NSLog(@"Method:%@",call.method);
            NSLog(@"Arguments:%@",call.arguments);
            if ([call.method isEqualToString:@"exit"]) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }];
    

    方法methodChannelWithName : binaryMessenger :是初始化FlutterMethodChannel对象的方法,参数一:FlutterMethodChannel的名字(在flutter工程里用),参数二:全局的FlutterViewController对象。

    方法invokeMethod : arguments是向flutter页面发送消息的方法,参数一:方法名,参数二:参数。

    方法setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result)是监听flutter页面发送消息的方法,block回调参数call里包含flutter页面的发送的方法名call.method和发送的参数call.arguments.

    2. flutter调用原生

    final MethodChannel _oneChannel = MethodChannel('one_page')
    flutter通过一个名为one_page的MethodChannel对象 _oneChannel来向原生页面发送消息和接受原生页面发来的消息

    通过Future<T> invokeMethod<T>(String method, [ dynamic arguments ])方法来发送消息,参数一:方法名,参数二:参数。
    示例:_oneChannel.invokeMethod('exit','arg')

    通过setMethodCallHandler(Future<dynamic> handler(MethodCall call))方法来接受原生页面的消息,call.methodcall.arguments分别表示原生页面发送的方法名和参数
    示例:

    _oneChannel.setMethodCallHandler((MethodCall call){
          _pageIndex = call.method;
          setState(() {});
        });
    
    3.BasicMessageChannel

    BasicMessageChannel可用于flutter和原生页面传递字符串
    flutter页面监听来自iOS原生页面的消息:

    //flutter页面监听原生页面的消息
    final BasicMessageChannel _messageChannel = BasicMessageChannel('messageChannel',StandardMessageCodec());
    _messageChannel.setMessageHandler((message){
          print('收到来自iOS的:${message}');
        });
    
    //iOS原生页面发送消息
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        static int a = 0;
        [self.messageChannel sendMessage:[NSString stringWithFormat:@"%d",a++]];
    }
    

    iOS原生页面监听flutter页面发送的消息

    //监听MessageChannel的数据
        _messageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"messageChannel" binaryMessenger:self.flutterVC];
        [_messageChannel setMessageHandler:^(id  _Nullable message, FlutterReply  _Nonnull callback) {
            NSLog(@"%@",message);
        }];
    //flutter Textfield文字输入发生变化就会发送消息
    TextField(
                    onChanged: (String str){
                      _messageChannel.send(str);
                    },
                  )
    

    相关文章

      网友评论

          本文标题:Flutter学习三-知识小集

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