├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example ├── .gitignore ├── .metadata ├── README.md ├── lib │ └── main.dart ├── pubspec.lock └── pubspec.yaml ├── lib ├── src │ ├── platform_io.dart │ └── universal_platform_locator.dart └── universal_platform.dart ├── pubspec.lock └── pubspec.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .flutter-plugins-dependencies 28 | .packages 29 | .pub-cache/ 30 | .pub/ 31 | /build/ 32 | 33 | # Web related 34 | lib/generated_plugin_registrant.dart 35 | 36 | # Symbolication related 37 | app.*.symbols 38 | 39 | # Obfuscation related 40 | app.*.map.json 41 | 42 | # Exceptions to above rules. 43 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 44 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.1.0 2 | - Add support for platform specific checks and desktopOrWeb 3 | - Fix analysis warnings 4 | 5 | ## 1.0.0 6 | - Migrate to nnbd 7 | 8 | ## 0.1.3 9 | - Removed flutter sdk dependency 10 | 11 | ## 0.1.2 12 | - Initial version 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 gskinner.com 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Universal Platform - A Web-safe Platform class 2 | [![pub package](https://img.shields.io/pub/v/universal_platform.svg)](https://pub.dev/packages/universal_platform) 3 | 4 | Currently, if you include the _dart.io.Platform_ anywhere in your code, your app will throw the following error on Web: 5 | ``` 6 | Unsupported operation: Platform._operatingSystem 7 | ``` 8 | 9 | With this plugin you can perform platform detection on all platforms, including Web, without errors. 10 | 11 | ## 🔨 Installation 12 | ```yaml 13 | dependencies: 14 | universal_platform: ^1.0.0+1 15 | ``` 16 | 17 | ### ⚙ Import 18 | 19 | Remove any usages of _dart.io.Platform_, and replace with this: 20 | ```dart 21 | import 'package:universal_platform/universal_platform.dart'; 22 | ``` 23 | 24 | ## 🕹️ Usage 25 | 26 | This acts as a drop-in replacement for dart.io.Platform, with a different name for improved clarity. 27 | 28 | ```dart 29 | //This will explode on Web 30 | bool isIos = Platform.isIOS; 31 | 32 | //This will not :) 33 | bool isIos = UniversalPlatform.isIOS; 34 | bool isWeb = UniversalPlatform.isWeb; 35 | ``` 36 | 37 | 38 | ## 🐞 Bugs/Requests 39 | 40 | If you encounter any problems feel open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are also welcome. 41 | 42 | ## 📃 License 43 | 44 | MIT License 45 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | analyzer: 2 | strong-mode: 3 | implicit-dynamic: false 4 | errors: 5 | unused_import: error 6 | unused_local_variable: error 7 | dead_code: error 8 | 9 | # Lint rules and documentation, see http://dart-lang.github.io/linter/lints 10 | linter: 11 | rules: 12 | - annotate_overrides 13 | - avoid_unused_constructor_parameters 14 | - await_only_futures 15 | - camel_case_types 16 | - cancel_subscriptions 17 | - directives_ordering 18 | - empty_catches 19 | - empty_statements 20 | - hash_and_equals 21 | - no_adjacent_strings_in_list 22 | - no_duplicate_case_values 23 | - non_constant_identifier_names 24 | - only_throw_errors 25 | - overridden_fields 26 | - prefer_collection_literals 27 | - prefer_conditional_assignment 28 | - prefer_contains 29 | - prefer_final_fields 30 | - prefer_final_locals 31 | - prefer_initializing_formals 32 | - prefer_interpolation_to_compose_strings 33 | - prefer_is_empty 34 | - prefer_is_not_empty 35 | - prefer_typing_uninitialized_variables 36 | - recursive_getters 37 | - slash_for_doc_comments 38 | - test_types_in_equals 39 | - throw_in_finally 40 | - type_init_formals 41 | - unawaited_futures 42 | - unnecessary_brace_in_string_interps 43 | - unnecessary_getters_setters 44 | - unnecessary_lambdas 45 | - unnecessary_new 46 | - unnecessary_null_aware_assignments 47 | - unnecessary_statements 48 | - unnecessary_this 49 | - unrelated_type_equality_checks 50 | - use_rethrow_when_possible 51 | - valid_regexps 52 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .flutter-plugins-dependencies 28 | .packages 29 | .pub-cache/ 30 | .pub/ 31 | /build/ 32 | 33 | # Web related 34 | lib/generated_plugin_registrant.dart 35 | 36 | # Symbolication related 37 | app.*.symbols 38 | 39 | # Obfuscation related 40 | app.*.map.json 41 | 42 | # Exceptions to above rules. 43 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 44 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 1d3fcfd6ca7e83c6c7d134fc99deb015839b0389 8 | channel: master 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # example 2 | 3 | An example of the universal_platform package. -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter/material.dart'; 4 | import 'package:universal_platform/universal_platform.dart'; 5 | 6 | void main() => runApp(MaterialApp(home: Demo())); 7 | 8 | class Demo extends StatelessWidget { 9 | @override 10 | Widget build(BuildContext context) { 11 | print(window.physicalSize); 12 | 13 | return Material( 14 | child: Center( 15 | child: Text( 16 | "Web: ${UniversalPlatform.isWeb} \n " 17 | "MacOS: ${UniversalPlatform.isMacOS} \n" 18 | "Windows: ${UniversalPlatform.isWindows} \n" 19 | "Linux: ${UniversalPlatform.isLinux} \n" 20 | "Android: ${UniversalPlatform.isAndroid} \n" 21 | "IOS: ${UniversalPlatform.isIOS} \n" 22 | "Fuschia: ${UniversalPlatform.isFuchsia} \n", 23 | )), 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /example/pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | characters: 5 | dependency: transitive 6 | description: 7 | name: characters 8 | url: "https://pub.dartlang.org" 9 | source: hosted 10 | version: "1.1.0" 11 | collection: 12 | dependency: transitive 13 | description: 14 | name: collection 15 | url: "https://pub.dartlang.org" 16 | source: hosted 17 | version: "1.15.0" 18 | flutter: 19 | dependency: "direct main" 20 | description: flutter 21 | source: sdk 22 | version: "0.0.0" 23 | meta: 24 | dependency: transitive 25 | description: 26 | name: meta 27 | url: "https://pub.dartlang.org" 28 | source: hosted 29 | version: "1.7.0" 30 | sky_engine: 31 | dependency: transitive 32 | description: flutter 33 | source: sdk 34 | version: "0.0.99" 35 | typed_data: 36 | dependency: transitive 37 | description: 38 | name: typed_data 39 | url: "https://pub.dartlang.org" 40 | source: hosted 41 | version: "1.3.0" 42 | universal_platform: 43 | dependency: "direct main" 44 | description: 45 | path: ".." 46 | relative: true 47 | source: path 48 | version: "1.0.0-nullsafety" 49 | vector_math: 50 | dependency: transitive 51 | description: 52 | name: vector_math 53 | url: "https://pub.dartlang.org" 54 | source: hosted 55 | version: "2.1.0" 56 | sdks: 57 | dart: ">=2.12.0 <3.0.0" 58 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: sized_context_example 2 | description: A new Flutter application. 3 | version: 1.0.0+1 4 | publish_to: none 5 | 6 | environment: 7 | sdk: ">=2.12.0 <3.0.0" 8 | 9 | dependencies: 10 | flutter: 11 | sdk: flutter 12 | 13 | universal_platform: 14 | path: ../ 15 | 16 | flutter: 17 | uses-material-design: true 18 | -------------------------------------------------------------------------------- /lib/src/platform_io.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import '../universal_platform.dart'; 3 | 4 | //Override default method, to provide .io access 5 | UniversalPlatformType get currentUniversalPlatform { 6 | if(Platform.isWindows) return UniversalPlatformType.Windows; 7 | if(Platform.isFuchsia) return UniversalPlatformType.Fuchsia; 8 | if(Platform.isMacOS) return UniversalPlatformType.MacOS; 9 | if(Platform.isLinux) return UniversalPlatformType.Linux; 10 | if(Platform.isIOS) return UniversalPlatformType.IOS; 11 | return UniversalPlatformType.Android; 12 | } 13 | -------------------------------------------------------------------------------- /lib/src/universal_platform_locator.dart: -------------------------------------------------------------------------------- 1 | import '../universal_platform.dart'; 2 | 3 | //Default to web, the platform_io class will override this if it gets imported. 4 | UniversalPlatformType get currentUniversalPlatform => UniversalPlatformType.Web; 5 | -------------------------------------------------------------------------------- /lib/universal_platform.dart: -------------------------------------------------------------------------------- 1 | library universal_platform; 2 | 3 | import 'src/universal_platform_locator.dart' if (dart.library.io) 'src/platform_io.dart'; 4 | 5 | export 'src/universal_platform_locator.dart' if (dart.library.io) 'src/platform_io.dart'; 6 | 7 | abstract class UniversalPlatform { 8 | static UniversalPlatformType get value => currentUniversalPlatform; 9 | 10 | static bool get isWeb => currentUniversalPlatform == UniversalPlatformType.Web; 11 | static bool get isMacOS => currentUniversalPlatform == UniversalPlatformType.MacOS; 12 | static bool get isWindows => currentUniversalPlatform == UniversalPlatformType.Windows; 13 | static bool get isLinux => currentUniversalPlatform == UniversalPlatformType.Linux; 14 | static bool get isAndroid => currentUniversalPlatform == UniversalPlatformType.Android; 15 | static bool get isIOS => currentUniversalPlatform == UniversalPlatformType.IOS; 16 | static bool get isFuchsia => currentUniversalPlatform == UniversalPlatformType.Fuchsia; 17 | 18 | static bool get isApple => UniversalPlatform.isIOS || UniversalPlatform.isMacOS; 19 | static bool get isMobile => UniversalPlatform.isIOS || UniversalPlatform.isAndroid; 20 | static bool get isDesktop => isLinux || isMacOS || isWindows; 21 | static bool get isDesktopOrWeb => UniversalPlatform.isDesktop || UniversalPlatform.isWeb; 22 | 23 | static String get operatingSystem { 24 | switch (value) { 25 | case UniversalPlatformType.Web: 26 | return "web"; 27 | case UniversalPlatformType.MacOS: 28 | return "macos"; 29 | case UniversalPlatformType.Windows: 30 | return "windows"; 31 | case UniversalPlatformType.Linux: 32 | return "linux"; 33 | case UniversalPlatformType.Android: 34 | return "android"; 35 | case UniversalPlatformType.IOS: 36 | return "ios"; 37 | case UniversalPlatformType.Fuchsia: 38 | return "fuchsia"; 39 | } 40 | } 41 | } 42 | 43 | enum UniversalPlatformType { 44 | Web, 45 | Windows, 46 | Linux, 47 | MacOS, 48 | Android, 49 | Fuchsia, 50 | IOS, 51 | } 52 | -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: {} 4 | sdks: 5 | dart: ">=2.12.0" 6 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: universal_platform 2 | description: Replacement for dart.io.Platform class which works on Web as well as Desktop and Mobile. Allows platform checks in your view/model layer easily. 3 | version: 1.1.0 4 | homepage: https://github.com/gskinnerTeam/flutter-universal-platform 5 | 6 | environment: 7 | sdk: '>=2.12.0 <4.0.0' 8 | 9 | dependencies: 10 | --------------------------------------------------------------------------------