Skip to content

Instantly share code, notes, and snippets.

@rydmike
Last active October 15, 2022 20:07
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rydmike/1771fe24c050ebfe792fa309371154d8 to your computer and use it in GitHub Desktop.
Save rydmike/1771fe24c050ebfe792fa309371154d8 to your computer and use it in GitHub Desktop.
Flutter Universal Platform Check - That Works on Web too
import 'universal_platform_web.dart'
if (dart.library.io) 'universal_platform_vm.dart';
/// A universal platform checker.
///
/// Can be used to check active physical Flutter platform on all platforms.
///
/// To check what host platform the app is running on use:
///
/// * PlatformIs.android
/// * PlatformIs.iOS
/// * PlatformIs.macOS
/// * PlatformIs.windows
/// * PlatformIs.linux
/// * PlatformIs.fuchsia
///
/// To check the device type use:
///
/// * PlatformIs.mobile (Android or iOS)
/// * PlatformIs.desktop (Windows, macOS or Linux)
///
/// Currently Fuchsia is not considered mobile nor desktop, even if it
/// might be so in the future.
///
/// To check if the Flutter application is running on Web you can use:
///
/// * PlatformIs.web
///
/// Alternatively the Flutter foundation compile time constant kIsWeb also
/// works well for that.
///
/// The platform checks are supported independently on web. You can use
/// PlatformIs windows, iOS, macOS, Android and Linux to check what the host
/// platform is when you are running a Flutter Web application.
///
/// Checking if we are running on a Fuchsia host in a Web browser, is not yet fully
/// supported. If running in a Web browser on Fuchsia, PlatformIs.web will be true, but
/// PlatformIs.fuchsia will be false. Future versions, when Fuchsia is released,
/// may fix this.
class PlatformIs {
PlatformIs._();
static bool get web => UniversalPlatform.web;
static bool get macOS => UniversalPlatform.macOS;
static bool get windows => UniversalPlatform.windows;
static bool get linux => UniversalPlatform.linux;
static bool get android => UniversalPlatform.android;
static bool get iOS => UniversalPlatform.iOS;
static bool get fuchsia => UniversalPlatform.fuchsia;
static bool get mobile => PlatformIs.iOS || PlatformIs.android;
static bool get desktop =>
PlatformIs.macOS || PlatformIs.windows || PlatformIs.linux;
}
import 'dart:io';
// NOTE:
// Never import this library directly in the application. The PlatformIs
// class and library uses conditional imports to only import this file on
// VM platform builds.
// UniversalPlatform for Flutter VM builds.
//
// We are using Dart VM builds, so we use dart:io Platform to
// get the current platform.
class UniversalPlatform {
UniversalPlatform._();
static bool get web => false;
static bool get macOS => Platform.isMacOS;
static bool get windows => Platform.isWindows;
static bool get linux => Platform.isLinux;
static bool get android => Platform.isAndroid;
static bool get iOS => Platform.isIOS;
static bool get fuchsia => Platform.isFuchsia;
}
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
// NOTE:
// Never import this library directly in the application. The PlatformIs
// class and library uses conditional imports to only import this file on
// Web platform builds.
final html.Navigator _nav = html.window.navigator;
// UniversalPlatform for Flutter WEB build.
//
// We can use dart:html Navigator to get the current platform.
//
// This function is borrowed, with minor modifications, from GetX utils library with MIT license.
// Credits for it belong to its author Jonny Borges https://github.com/jonataslaw
// https://github.com/jonataslaw/getx/blob/master/lib/get_utils/src/platform/platform_web.dart
//
class UniversalPlatform {
UniversalPlatform._();
static bool get web => true;
static bool get macOS => _nav.appVersion.contains('Mac OS') && !iOS;
static bool get windows => _nav.appVersion.contains('Win');
static bool get linux =>
(_nav.appVersion.contains('Linux') || _nav.appVersion.contains('x11')) &&
!android;
// Source: https://developer.chrome.com/multidevice/user-agent
static bool get android => _nav.appVersion.contains('Android ');
static bool get iOS {
// maxTouchPoints is needed to separate iPad iOS13 vs new MacOS
return _hasMatch(_nav.platform, '/iPad|iPhone|iPod/') ||
(_nav.platform == 'MacIntel' && _nav.maxTouchPoints! > 1);
}
// Theoretically we could be in a Web browser on Fuchsia too, but
// we have no info on how to get that info yet, so we return false.
static bool get fuchsia => false;
}
bool _hasMatch(String? value, String pattern) {
// ignore: avoid_bool_literals_in_conditional_expressions
return (value == null) ? false : RegExp(pattern).hasMatch(value);
}
@rydmike
Copy link
Author

rydmike commented Aug 27, 2021

A useful helper to check which Platform a Flutter Application is running on

If you are making a multiplatform app you might sometimes need to check if the app is running on Web or what platform on the VM.
The built in Platform in dart.io will throw if imported on web, so you cannot use it on Web at all. You have to use conditional import wrappers to avoid the error.

What if you want to know what the host platform is when you are running on web? The dart:html that can help you with that.

These three simple files shows one way of doing it, you do not really need a package for this thing. 😃


MIT License

Copyright (c) 2021 Mike Rydstrom

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment