Giter Club home page Giter Club logo

flutter-native-bridge-example's Introduction

Flutter Native Bridge Example

这里描述的是flutter与原生工程的通信交互

我的做法是写了个文件覆盖了methodChannel的代理方法,将收发功能封装进了一个类SwiftFlutterBridge.swift、并将通信约定字符写在了宏定义文件里FlutterBridgeConst.swift

  • 必不可少的约定字符为以下三种(根据自己的项目需要去设定)
   static let MethodChannel = "com.example.flutter/method"
   
   static let EventChannel = "com.example.flutter/event"
   
   static let BasicMessageChannel = "com.example.flutter/basicMessage"
  • 在AppDelegate里注册通信
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       
       // 注册与Flutter的通信
       SwiftFlutterBridge.shared.load(methodChannel: FlutterBridgeConst.MethodChannel, eventChannel: FlutterBridgeConst.EventChannel, messageChannel: FlutterBridgeConst.BasicMessageChannel)
       
       self.window?.backgroundColor = .white
       self.window?.rootViewController = UINavigationController(rootViewController: RootViewController())
       self.window?.makeKeyAndVisible()
       
       return true
   }
  • 遵守协议代理及加载flutter页面 可以根据需要仅仅跳转或是直接加载
      SwiftFlutterBridge.shared.delegate = self

       DispatchQueue.main.async {
           self.addChild(SwiftFlutterBridge.shared.flutterViewController)
           self.view.addSubview(SwiftFlutterBridge.shared.flutterViewController.view)
           SwiftFlutterBridge.shared.flutterViewController.view.frame = self.view.bounds
           SwiftFlutterBridge.shared.flutterViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
       }
  • 原生向flutter发送信息
    func sendMessageToFlutter(message: Any) {
       if let messageChannel = basicMessageChannel {
           messageChannel.sendMessage(message)
       }
   }

Flutter这边的方法是使用工厂模式将EventChannelBasicMessageChannelMethodChannel写在了flutter_bridge_manager.dart文件里

  • 向原生发送信息
    // 这个字典紧紧是为了举个例子
    Map<String, dynamic> map = {"name": listTitleArray[index], "arguments": "test"};

    MethodChannelManager.instance.methodChannel
        .invokeMethod(listTitleArray[index], map);
  • 接收native发来的信息
    BasicMessageChannelManager.instance.basicMessageChannel.setMessageHandler((message) async {
      Map map = json.decode(message);
      print('basicMessageMethod:' + '$map');
    });

参考链接

flutter-native-bridge-example's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.