Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter with Platform

najeira
July 14, 2020

Flutter with Platform

najeira

July 14, 2020
Tweet

More Decks by najeira

Other Decks in Technology

Transcript

  1. Android public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler { @Override

    public void configureFlutterEngine(@NonNull Flutte super.configureFlutterEngine(flutterEngine); MethodChannel channel = new MethodChannel( flutterEngine.getDartExecutor().getBinaryMesse "my channel"); channel.setMethodCallHandler(this); } 8 8 Flutter with platform Flutter with platform
  2. Android public void onMethodCall(MethodCall call, MethodChannel.Result result) { switch (call.method)

    { case "hello": result.success("Android!"); break; default: result.notImplemented(); break; } } 9 9 Flutter with platform Flutter with platform
  3. iOS @objc class AppDelegate: FlutterAppDelegate { override func application(...) ->

    Bool { let fvc : FlutterViewController = window?.rootViewController as! FlutterViewCont let channel = FlutterMethodChannel( name: "my channel", binaryMessenger: fvc.binaryMessenger) channel.setMethodCallHandler(self.handle) 10 10 Flutter with platform Flutter with platform
  4. iOS func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) -> Void

    { switch call.method { case "hello": result("iOS!") default: result(FlutterMethodNotImplemented) } } 11 11 Flutter with platform Flutter with platform
  5. Android MainActivity: private MethodChannel.Result mResult; public void onMethodCall(MethodCall call, MethodChannel.Result

    result) { switch (call.method) { case "text": mResult = result; Intent intent = new Intent(this, MyActivity.class); startActivityForResult( intent, REQUEST_CODE); 16 16 Flutter with platform Flutter with platform
  6. Android MainActivity: protected void onActivityResult(int requestCode, int if (requestCode ==

    REQUEST_CODE) { if (resultCode == RESULT_OK) { mResult.success(data.getStringExtra("data")); } else { mResult.success("cancel"); } mResult = null; } else { super.onActivityResult(requestCode, resultCode, } } 18 18 Flutter with platform Flutter with platform
  7. iOS AppDelegate: var flutterResult: FlutterResult? func handle(_ call: FlutterMethodCall, result:

    @escaping FlutterResult) -> Void { switch call.method { case "text": flutterResult = result let vc = MyViewController() vc.delegate = self window?.rootViewController?.present( vc, animated: true, completion: nil) 21 21 Flutter with platform Flutter with platform
  8. iOS MyViewController: delegate?.textDidEdit(editText.text) self.dismiss(animated: true, completion: nil) AppDelegate: func textDidEdit(_

    text: String) { flutterResult?(text) flutterResult = nil } 22 22 Flutter with platform Flutter with platform
  9. iOS rootをUINavigationControllerに差し替え override func application(...) -> Bool { let fvc

    : FlutterViewController = window?.rootViewController as! FlutterViewContro let nav = UINavigationController.init( rootViewController: fvc) nav.isNavigationBarHidden = true window.rootViewController = nav flutterViewController = fvc 23 23 Flutter with platform Flutter with platform
  10. iOS var flutterViewController: FlutterViewController? override func registrar(forPlugin: String) -> Flutte

    return (self.flutterViewController?.registrar(forP } override func hasPlugin(_ pluginKey: String) -> Bool return (self.flutterViewController?.hasPlugin(plug } override func valuePublished(byPlugin pluginKey: Str return (self.flutterViewController?.valuePublished } 24 24 Flutter with platform Flutter with platform
  11. まとめ Flutter⾃⾝も Activity / ViewController なので 別のプラットフォームの画⾯と⾏き来できる Texture, Platform View

    を使って Flutterの画⾯内にインラインに プラットフォームUIを表⽰できる 37 37 Flutter with platform Flutter with platform