搜尋套件、儲存庫與 APP 分類

套件詳情v4.1.0

selector

支援多類型的平台選擇器,用於撰寫函數式程式設計。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
58
喜歡數
22
Pub 分數
160

安裝套件

flutter pub add selector

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個平台無關的選擇器套件,支援多種資料類型,設計用於在Flutter應用中啟用函數式程式設計模式。它提供一種乾淨、可組合的方式來跨平台管理狀態並推導值,增強程式碼的可讀性和可維護性。使用Dart建構,可順暢整合至Flutter專案,並支援所有主要平台,包括Android、iOS、Web、macOS、Windows與Linux。該套件強調類型安全性與函數式範式,以實現高效狀態處理。

適用情境

  • 複雜UI中的狀態推導
  • 函數式響應式程式設計
  • 跨平台狀態管理
  • 選擇邏輯的可重用性
  • 簡化派生狀態計算
  • 基於流的值選擇

主要特色

  • 多類型支援
  • 函數式程式設計風格
  • 平台無關設計
  • 類型安全的選擇
  • 可組合的選擇器
  • 內建平台相容性

適合對象

  • 使用函數式程式設計模式的開發者
  • 尋求可重用狀態邏輯的團隊
  • 跨平台 Flutter 應用
  • 需要複雜狀態推導的應用程式
  • 強調程式碼潔淨性與可測試性的專案

使用建議

  • 需要理解函數式程式設計
  • 可能為初學者帶來學習曲線
  • 最佳搭配狀態管理解決方案使用
  • 僅限 Dart/Flutter 生態系統
  • 不適用於簡單的狀態儲存

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Selector

Multi-type support Platform Selector for Flutter. This library helps you write platform-specific code in a clean and functional way.

Features

  • Basic Selector (selector): Choose a value based on the current platform (Android, iOS, macOS, Windows, Linux, Fuchsia, Web). All platform parameters are required. Throws UnsupportedError if the platform is not identified.
  • Optional Selector (optionalSelector): Similar to selector, but platform parameters are optional. Returns null if the current platform's value is not provided or if the platform is unidentified.
  • Device Selector (deviceSelector): Select a value based on device type (mobile, desktop, web). All device type parameters are required. Throws UnsupportedError if the device type is not identified.
  • Optional Device Selector (optionalDeviceSelector): Similar to deviceSelector, but device type parameters are optional. Returns null if the current device type's value is not provided or if the device type is unidentified.
  • OrElse Selector (selectOrElse): Provides a fallback orElse value if the current platform's value is not provided.
  • Query (isMobile, isDesktop): Convenient getters to check if the current device is mobile or desktop.

Usage

Basic selector (selector)

Use selector when you need to provide a value for every supported platform.

import 'package:selector/selector.dart';

String message = selector(
      web: 'Hello Web!',
      android: 'Hello Android!',
      ios: 'Hello iOS!',
      fuchsia: 'Hello Fuchsia!',
      linux: 'Hello Linux!',
      macos: 'Hello macOS!',
      windows: 'Hello Windows!',
);
print(message); // e.g., "Hello Android!" on an Android device
Optional selector (optionalSelector)

Use optionalSelector when you only need to provide values for a subset of platforms. If the current platform's value is not provided, it returns null.

import 'package:selector/selector.dart';

String? specialMessage = optionalSelector(
      android: 'Special message for Android!',
      ios: 'Special message for iOS!',
);
print(specialMessage); // "Special message for Android!" on Android, null on macOS
Device selector (deviceSelector)

Use deviceSelector to differentiate between mobile, desktop, and web environments.

import 'package:selector/selector.dart';

String deviceType = deviceSelector(
      mobile: 'This is a mobile device.',
      desktop: 'This is a desktop device.',
      web: 'This is a web browser.',
);
print(deviceType); // e.g., "This is a mobile device." on an iOS phone
Optional Device selector (optionalDeviceSelector)

Similar to deviceSelector, but device type parameters are optional. Returns null if the current device type's value is not provided.

import 'package:selector/selector.dart';

String? specificDeviceMessage = optionalDeviceSelector(
      mobile: 'Mobile specific UI.',
      web: 'Web specific UI.',
);
print(specificDeviceMessage); // "Mobile specific UI." on Android, null on Windows desktop
OrElse selector (androidOrElse, iosOrElse, etc.)

Use these platform-specific orElse functions for direct, explicit choices.

import 'package:selector/selector.dart';

String admobKey = androidOrElse('android-key', 'iOS-key');
String iosPreferredKey = iosOrElse('iOS-key', 'android-key');
String mobileSpecific = mobileOrElse('mobile_value', 'default_value');
String desktopSpecific = desktopOrElse('desktop_value', 'default_value');
String webSpecific = webOrElse('web_value', 'default_value');

Alternatively, use the more flexible selectOrElse for multiple platform options:

import 'package:selector/selector.dart

String apiKey = selectOrElse(
      android: 'YOUR_ANDROID_API_KEY',
      ios: 'YOUR_IOS_API_KEY',
      orElse: 'DEFAULT_API_KEY', // Used if platform is not Android or iOS
);
print(apiKey); // "YOUR_ANDROID_API_KEY" on Android, "DEFAULT_API_KEY" on Windows
Query

Convenient getters to check the device type.

import 'package:selector/selector.dart';

print('isDesktop: $isDesktop'); // true if on Windows, Linux, macOS
print('isMobile: $isMobile');   // true if on Android, iOS, Fuchsia

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。