-
-
Save DiegoCarvalho75/bcee795779e2c914c43acfa0e5c5dbec to your computer and use it in GitHub Desktop.
Flutter Universal Platform Check - That Works on Web too
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Fuchsia host in a Web browser, is not yet | |
/// supported. If running in a Web browser on PlatformIs.web will be true, but | |
/// PlatformIs.fuchsia will still be false. | |
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment