platform_builder 3.0.0 copy "platform_builder: ^3.0.0" to clipboard
platform_builder: ^3.0.0 copied to clipboard

A Flutter library for performing platform checks and building widgets based on platform.

Platform Builder #

A Flutter library for performing platform checks and building widgets based on platform.

Platform checks #

import 'package:platform_builder/platform_builder.dart';

if (Platform.instance.isAndroid) {
  print('android');
} else if (Platform.instance.isWeb) {
  print('web');
}

Platform builders #

import 'package:platform_builder/platform_builder.dart';

class MyWidget extends StatelessWidget {
  @override
  build(context) {
    return PlatformBuilder(
      androidBuilder: (context) => Icon(Icons.android),
      iOSBuilder: (context) => Icon(Icons.apple),
    ),
  }
}

Platforms #

The libray provides builders for the following platforms:

  • android
  • iOS
  • macOS
  • linux
  • fuschia
  • windows
  • web
  • chrome extension

By default all platforms are enabled and the PlatformBuilder will throw an error if you forget to include an implementation for one of the supported platforms. To specify your preferred platforms, call the Platform.init to initialize the Platform singleton with the list of your application's supported platforms:

import 'package:platform_builder/platform_builder.dart';

Platform.init(
  supportedPlatforms: {
    Platforms.iOS,
    Platforms.android,
    Platforms.web,
  }
);

If a particular PlatformBuilder needs to override the global list of supported platforms, such as during active development, you can pass an override to the widget:

import 'package:platform_builder/platform_builder.dart';

class MyWidget extends StatelessWidget {
  @override
  build(context) {
    return PlatformBuilder(
      supportedPlatforms: {Platforms.iOS, Platforms.android},
      androidBuilder: (context) => Icon(Icons.android),
      iOSBuilder: (context) => Icon(Icons.apple),
    ),
  }
}

Builder precedence #

The precedence of builders is based on specificity. More specific builders take precedence over broader ones as shown below:

import 'package:platform_builder/platform_builder.dart';

class MyWidget extends StatelessWidget {
  @override
  build(context) {
    return PlatformBuilder(
      builder: (context) {...},
      nativeBuilder: (context) {...},
      androidBuilder: (context) {...},
    ),
  }
}

In this example on a web platform, all three builders are applicable, but the precedence would be:

  • androidBuilder
  • nativeBuilder
  • builder

Platform Resolvers #

If you're looking to resolve a non-widget value by Platform, the PlaformResolver API can be used to resolve the desired platform value. Consider this example // where we are resolving a value on Android:

import 'package:platform_builder/platform_builder.dart';

const platformResolver = PlatformResolver<String>(
  defaultResolver: () => "Unknown",
  androidResolver: () => "Android",
  nativeResolver: () => "Native",
);

print(platformResolver.current) // Android
print(platformResolver.resolve(Platforms.iOS)) // Native
print(platformResolver.resolve(Platforms.web)) // Unknown

FAQs #

  • Q: Don't we already have a way to check the current Platform?

  • A Yes! But the Platform library from dart:io has some quirks like the fact that calling native platforms like Platform.isIOS on web throws an exception and that there is no check for web. We address both those issues here as well expanding the platform helpers to include other helpful platform utilities like the following:

  • Q: Why a PlatformBuilder? Can't we just use if/else clauses in our build functions?

  • A: You definitely can! Here are some things we think a builder widget can help with:

    • It organizes branching your build functions by platform in a consistent way
    • It throws a runtime error if you forget to add a builder for one of your specified supported platforms so that you can catch that mistake in development
    • It abstracts having to repeat yourself with frequent platform checks throughout your application.

Tell us what you need #

Something missing? Let us know what additional platform utilities would be helpful for your workflow.

8
likes
160
points
39
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter library for performing platform checks and building widgets based on platform.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on platform_builder