flutter学习笔记二(MethodChannel)

本地访问-MethodChannel

  1. 本地访问是指跨平台的解决方案,有访问本地android或者ios资源的能力,主要是MethodChannel和MessageChannel两种,第一种是调用方法,第二种是传递信息。通信的双方是Flutter和本地操作系统或者应用,而且方法的调用和消息的方法可以从任何一方发起,类似远程过程调用。
  • Flutter是一个比较独立的环境,要想访问本地的资源,Flutter提供了Channel机制,类似Cilent-Server模式或者RPC
  • 通过Channel的名称打通Channel,隧道
  • Flutter和本地是对称的,请求可以从任何一发发起,这篇文章主要是记录从Flutter给本地发送请求

  • 首先我们创建Flutte工程,ide随你自己喜欢,修改UI代码如下:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  static const platformMethodChannel = const MethodChannel("com.xyjy.flutterchannel/test");
  num nativeMessage = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Future<Null> getData() async{
    num _message;
      try{
        final num result = await platformMethodChannel.invokeMethod("getTime");
        _message = result;
      }
      on PlatformException catch(e){
        print(e.message);
      }
      setState(() {
        nativeMessage = _message;
      });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: new Center(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              "$nativeMessage",
            ),
            new Text(
              '$nativeMessage',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: ()=>getData(),
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
  • 很简单我只是将_counter显示的替换成了自己定义的nativeMessage,接着往下
  1. 首先我们要引入service包和async包。
  import 'package:flutter/services.dart';
  import 'dart:async';
  1. 声明一个static const变量platformMethodChannel ,赋值MethodChannel类型的对象。
 class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  static const platformMethodChannel = const MethodChannel("com.xyjy.flutterchannel/test");
  num nativeMessage = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
  1. 在上面我们要注意,给MethodChannel一个名字com.xyjy.flutterchannel/test,这个名字和本地(这里代表android部分)那边的要一样
    我们创建了MethodChannel,我们准备一个用来发送请求的函数。我们创建一个叫getData的异步函数
    await platformMethodChannel.invokeMethod('getTime') 是调用本地的getTime方法,并且这个方法是异步的,await表示阻塞执行。
    nativeMessage = _message; 表示通知Flutter状态改变,刷新界面。
 Future<Null> getData() async{
    // num是数字类型
    num _message;
      try{
        final num result = await platformMethodChannel.invokeMethod("getTime");
        _message = result;
      }
      on PlatformException catch(e){
        print(e.message);
      }
      setState(() {
        nativeMessage = _message;
      });
  }
  1. 修改build里面的显示,将_counter 替换为nativeMessage ,并将floatingActionButton的onTap事件替换为我们定义好的getData方法
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: new Center(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              "$nativeMessage",
            ),
            new Text(
              '$nativeMessage',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: ()=>getData(),
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  1. 接下来本地方法如何编写,打开MainActivity
 public class MainActivity extends FlutterActivity {
  private static final  String CHANNAL = "com.xyjy.flutterchannel/test";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    new MethodChannel(getFlutterView() , CHANNAL).setMethodCallHandler(new MethodChannel.MethodCallHandler()
    {
      @Override
      public void onMethodCall(MethodCall methodCall, MethodChannel.Result result)
      {
          if (methodCall.method.equals("getTime"))
          {
              long time = getTime();
              result.success(time);
          }
          else
          {
            result.notImplemented();
          }
      }
    });
  }

  private long getTime()
  {
    return System.currentTimeMillis();
  }

}
  • CHANNAL 里面的字符串一定是与Flutter定义一致,在MethodChannel的中调用.setMethodCallHandler() 方法,需要一个MethodCallHandler 对象,是一个匿名内部类,有一个方法onMethodCall,在Flutter发送请求时,onMethodCall方法会执行, result.success(time);返回信息,这样就可以进行本地调用,点击悬浮按钮得到时间(此时的时间是通过本地返回的时间)


    QQ图片20180709174145.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *、assets 这里需要注意的是文件里的assets只要一个缩进即和flutter里的内容保持对齐,否则,会出问...
    大鹏鸟阅读 5,560评论 1 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,123评论 19 139
  • 本文主要介绍怎样在现有的 Android 项目中集成 flutter 开发界面,以及 flutter 与原生之间怎...
    PandaQ404阅读 6,293评论 1 10
  • 第一章:Java程序设计概述 Java和C++最大的不同在于Java采用的指针模型可以消除重写内存和损坏数据的可能...
    loneyzhou阅读 1,298评论 1 7
  • 提起“笔”,草草了了写了几行字再删除。这样的事情做过无数次,今天也不例外。 过了一会儿,又想起写点什么,既然有意,...
    有追求的大哥阅读 193评论 0 0