當你來自 iOS 開發背景時,你可能需要尋找能提供與 Apple 系統函式庫相同功能的 Flutter 套件(plugin)。這可能包括存取裝置硬體或與特定框架(如 HealthKit)互動。

若想了解 SwiftUI 框架與 Flutter 的比較,請參閱 Flutter for SwiftUI developers

介紹 Flutter 套件(plugin)

#

Dart 稱包含平台專屬程式碼的函式庫為 plugin(套件),即「plugin package」的簡稱。 當你使用 Flutter 開發應用程式時,會透過 plugin 與系統函式庫互動。

在 Dart 程式碼中,你會使用該 plugin 的 Dart API 來呼叫所需的系統原生程式碼。這代表你只需撰寫呼叫 Dart API 的程式碼,API 會自動處理所有該 plugin 支援的平台。

想進一步了解 plugin,請參閱 Using packages。 雖然本頁面僅列出部分熱門 plugin,你可以在 pub.dev 上找到數千個 plugin 及其範例。 下表並不推薦任何特定 plugin。 如果你找不到符合需求的套件,可以自行開發,或直接在專案中使用平台通道(platform channels)。 欲了解更多,請參閱 Writing platform-specific code

將 plugin 加入你的專案

#

若要在原生專案中使用 Apple 框架,需在 Swift 或 Objective-C 檔案中匯入該框架。

若要新增 Flutter plugin,請在專案根目錄執行 flutter pub add package_name。 這會將相依性加入你的 pubspec.yaml 檔案。 加入相依性後,請在 Dart 檔案中加入該套件的 import 陳述式。

你可能還需要調整應用程式設定或初始化邏輯。 如有需要,請參閱該套件於 pub.dev 上的「Readme」頁面以取得詳細說明。

Flutter 套件與 Apple 框架對照表

#
使用情境Apple 框架或類別Flutter 套件(Plugin)
存取相簿PhotoKit,使用 PhotosPhotosUI 框架及 UIImagePickerControllerimage_picker
存取相機UIImagePickerController,使用 .camera sourceTypeimage_picker
使用進階相機功能AVFoundationcamera
提供應用程式內購買StoreKitin_app_purchase[1]
處理支付流程PassKitpay[2]
發送推播通知UserNotificationsfirebase_messaging[3]
取得 GPS 座標CoreLocationgeolocator
存取感測器資料[4]CoreMotionsensors_plus
執行網路請求URLSessionhttp
儲存鍵值資料@AppStorage 屬性包裝器及 NSUserDefaultsshared_preferences
資料庫持久化CoreData 或 SQLitesqflite
存取健康資料HealthKithealth
使用機器學習CoreMLgoogle_ml_kit[5]
文字辨識VisionKitgoogle_ml_kit[5:1]
語音辨識Speechspeech_to_text
擴增實境ARKitar_flutter_plugin
存取天氣資料WeatherKitweather[6]
存取與管理聯絡人Contactscontacts_service
主畫面顯示快速操作UIApplicationShortcutItemquick_actions
Spotlight 搜尋索引CoreSpotlightflutter_core_spotlight
設定、更新與與 Widget 溝通WidgetKithome_widget
透過 Siri/捷徑自動化應用程式操作AppIntentsintelligence

  1. Supports both Google Play Store on Android and Apple App Store on iOS. ↩︎

  2. Adds Google Pay payments on Android and Apple Pay payments on iOS. ↩︎

  3. Uses Firebase Cloud Messaging and integrates with APNs. ↩︎

  4. Includes sensors like accelerometer, gyroscope, etc. ↩︎

  5. Uses Google's ML Kit and supports various features like text recognition, face detection, image labeling, landmark recognition, and barcode scanning. You can also create a custom model with Firebase. To learn more, see Use a custom TensorFlow Lite model with Flutter. ↩︎ ↩︎

  6. Uses the OpenWeatherMap API. Other packages exist that can pull from different weather APIs. ↩︎