Skip to content

Instantly share code, notes, and snippets.

@AndrewDongminYoo
Created March 21, 2025 13:38
Show Gist options
  • Save AndrewDongminYoo/9f45283e392729deae705ce181d0cae6 to your computer and use it in GitHub Desktop.
Save AndrewDongminYoo/9f45283e392729deae705ce181d0cae6 to your computer and use it in GitHub Desktop.
현재 플랫폼을 감지하는 유틸리티 메서드를 제공합니다.
// 🐦 Flutter imports:
import 'package:flutter/foundation.dart';
/// 현재 플랫폼을 감지하는 유틸리티 메서드를 제공합니다.
sealed class PlatformUtil {
static TargetPlatform get current => defaultTargetPlatform;
/// 웹 플랫폼인지 여부를 반환합니다.
static bool get isWeb =>
kIsWeb && !isIOS && !isAndroid && !isWindows && !isMacOS && !isLinux;
static bool get isIOS => current == TargetPlatform.iOS;
static bool get isAndroid => current == TargetPlatform.android;
static bool get isWindows => current == TargetPlatform.windows;
static bool get isMacOS => current == TargetPlatform.macOS;
static bool get isLinux => current == TargetPlatform.linux;
/// 현재 플랫폼에 따라 다른 값을 선택합니다.
static T select<T>({
ValueGetter<T>? ios,
ValueGetter<T>? android,
ValueGetter<T>? web,
ValueGetter<T>? windows,
ValueGetter<T>? macos,
ValueGetter<T>? linux,
required ValueGetter<T> other,
}) {
if (isIOS && (ios != null)) {
return ios();
} else if (isAndroid && (android != null)) {
return android();
} else if (isWeb && (web != null)) {
return web();
} else if (isWindows && (windows != null)) {
return windows();
} else if (isMacOS && (macos != null)) {
return macos();
} else if (isLinux && (linux != null)) {
return linux();
} else {
return other();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment