Skip to content

Dart package that simplifies handling platform-specific operations and callbacks in your Dart/Flutter applications.

License

Notifications You must be signed in to change notification settings

RoundedInfinity/platform_specific

Repository files navigation

Platform Specific

style: very good analysis Powered by Mason License: MIT

Dart package that simplifies handling platform-specific operations and callbacks in your Dart/Flutter applications.

Features 🚀

  • Execute platform-specific callback functions based on the detected platform type.
  • Define custom platform types to handle specific platforms.
  • Supports a wide range of platforms out of the box including web, Windows, Linux, Android, macOS, Fuchsia, and iOS.

Usage 🧪

Use the Platforms.on method to execute the appropriate callback function based on the platform:

final myValue = Platforms.on<String>(
  {
    PlatformTypes.android: () {
      return '🤖';
    },
    PlatformTypes.windows: () {
      return '🪟';
    },
    PlatformTypes.oneOf([PlatformTypes.iOS, PlatformTypes.macOS]): () {
      return '🍎';
    }
  },
  orElse: () {
    return 'unknown';
  },
);

print(myValue); // Output: 🍎 (if running on iOS or macOS), 🤖 (if running on Android), 🪟 (if running on Windows), or 'unknown' (if running on any other platform)

Custom Platform Types

You can create custom platform types by extending the PlatformType class and overriding the isPlatform method. This allows you to handle additional platforms not covered by the predefined types.

class MyCustomPlatform extends PlatformType {
  const MyCustomPlatform();

  @override
  bool isPlatform(PlatformType? override) {
    // Add your custom platform detection logic here
    // Should return true if override == this
    ...
  }
}

About

Dart package that simplifies handling platform-specific operations and callbacks in your Dart/Flutter applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages