【 Flutter 】Flutter を 基礎 から 学習 ( ライブラリ ) part208 プラットフォームアクセスとPluginパッケージ

基礎 から 学ぶ Flutter 」という書籍で  学習 したことを ブログでアウトプットしていこうと思います。今回は ライブラリ ( part208 )です。

前回

【 Flutter 】Flutter を 基礎 から 学習 ( ライブラリ ) part207 プラットフォームアクセスとPluginパッケージ

引き続き、ライブラリについて学びます。

プラットフォームアクセスとPluginパッケージ

プラットフォームへのアクセス方法

前回はDart側の実装を行いました。

次はiOS側の実装です。

今度はSwiftという言語を使用して実装をしていくようです。

私は普段Windowsを使っています。
Swift言語ってWindowsでコンパイルできたんでしたかね?

実装するファイルはおそらく...\flutter_app\ios\Runner\AppDelegate.swiftと思われます。

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  var channel: FlutterMethodChannel!

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    // ①-② Dart側の①で指定した文字列
    channel = FlutterMethodChanel(name: "sample/toPlatformScreen", binaryMessenger: controllere as FlutterBinaryMessenger)

    channel.setMethodCallHandler({
      (call: FlutterMethodCall, result: FlutterResult) -> Void in
      switch call.method {
      // ②-② Dart側の②で指定した文字列
      case "toPlatformScreen":
        // ③-② Dart側の③で指定した引数
        let argument = call.arguments as! Dictionary<String, String>
        let label = arguments["label_from_dart"]!
        self.toPlatformScreen(label)
        // ④-② 成功した場合委に値を渡す値
        result("Platform Screen was displayed!!")
        break
      default:
        result(FlutterMethodNotImplemented)
      }
    })
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  private func toPlatformScreen(_ label: String) {
    let controller: FlutterViewController =
      window?.rootViewController as! FlutterViewController
    let storyboard: UIStoryboard
      = UIStoryboard(nae: "Next", bundle: nil)
    let nextController =
      storyboard.instantiateInitialViewController() as! NextController
    controller.present(nextController,
                       animated: true,
                       completion: {() in nextController.label.text = label
     })
  }
}

とりあえず実装しましたがAndroid Studioは特に反応なし・・・。

最後に

今回も前回同様に学習するだけで終わりそうな予感です😧

今日はここまで!