如何从 Dart 代码中检测主机平台?



对于在iOSAndroid上应该略有不同的UI,即在不同的平台上,必须有一种方法可以检测应用程序在哪一个上运行,但我在文档中找不到它。这是什么?

import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}

所有选项包括:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

您还可以使用kIsWeb来检测您是否在 Web 上运行,这是一个全局常量,指示应用程序是否已编译为在 Web 上运行:

import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}
  • Platform文档:https://api.flutter.dev/flutter/dart-io/Platform-class.html
  • kIsWeb文档:https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

感谢科林,最终答案是:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;

虽然defaultTargetPlatform可以工作,但我建议使用Theme.of(context).targetPlatform.这样可以测试 iOS 行为(因为测试中始终TargetPlatform.androiddefaultTargetPlatform)。它还允许小部件的祖先通过将小部件包装在Theme小部件中来覆盖其目标平台。

import 'dart:io' show Platform;  //at the top
String os = Platform.operatingSystem; //in your code
print(os);

只需导入io库即可

import'dart:io' show Platform;
void main(){
if(Platform.isIOS){
return someThing();
}else if(Platform.isAndroid){
return otherThing();
}else if(Platform.isMacOS){
return anotherThing();
}

或以非常简单的方式

Platform.isIOS ? someThing() : anOther(),
if (Platform.isAndroid) {
// Android-specific code/UI Component
} else if (Platform.isIOS) {
// iOS-specific code/UI Component
}

不要忘记导入IO库。

import 'dart:io';

如果您在同一文件中也使用import 'dart:html';那么您必须指定平台定义,因为两个库都有"平台"的定义

在这种情况下,请使用平台特定的代码,如下所示:

import 'dart:io' as IO;
import 'dart:html';
if (IO.Platform.isAndroid) {
// Android-specific code/UI Component
} else if (IO.Platform.isIOS) {
// iOS-specific code/UI Component
}

如果您正在正确研究平台集成,我建议您在 Flutter 站点上使用完整示例: https://flutter.dev/docs/development/platform-integration/platform-channels

为网络和应用程序提供更简单的方法。试试这个

import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;

var platformName = '';
if (kIsWeb) {
platformName = "Web";
} else {
if (Platform.isAndroid) {
platformName = "Android";
} else if (Platform.isIOS) {
platformName = "IOS";
} else if (Platform.isFuchsia) {
platformName = "Fuchsia";
} else if (Platform.isLinux) {
platformName = "Linux";
} else if (Platform.isMacOS) {
platformName = "MacOS";
} else if (Platform.isWindows) {
platformName = "Windows";
}
}
print("platformName :- "+platformName.toString());

你可以做

defaultTargetPlatform == TargetPlatform.iOS
? kIOSTheme
: kDefaultTheme,

import 'package:flutter/foundation.dart';相比

大多数"颤振"答案如下:

import 'package:flutter/foundation.dart' show TargetPlatform;
//...
if(Theme.of(context).platform == TargetPlatform.android)
//do sth for Android
else if(Theme.of(context).platform == TargetPlatform.iOS)
//do sth else for iOS
else if(Theme.of(context).platform == TargetPlatform.fuchsia)
//even do sth else for Fuchsia OS

您可以将此扩展文件 (platform_ext.dart) 添加到项目中并调用任何对象

import 'dart:io';
import 'package:flutter/foundation.dart' show kIsWeb;
extension Target on Object {
bool isAndroid() {
return Platform.isAndroid;
} 
bool isIOS() {
return Platform.isIOS;
} 
bool isLinux() {
return Platform.isLinux;
} 
bool isWindows() {
return Platform.isWindows; 
}
bool isMacOS() {
return Platform.isMacOS; 
}
bool isWeb() {
return kIsWeb; 
}
// ···
}

只需导入文件并调用它

import 'platform_ext.dart';
....
@override
Widget build(BuildContext context) {
return isAndroid()? Text("Android"):Text("Not Android");
}

您可以使用通用平台包:

https://pub.dev/packages/universal_platform

import 'package:universal_platform/universal_platform.dart';
bool isIos = UniversalPlatform.isIOS;
bool isAndroid = UniversalPlatform.isAndroid;
bool isWeb = UniversalPlatform.isWeb;
print('iOS: $isIos');
print('Android: $isAndroid');
print('Web: $isWeb');
import 'dart:io' as io;
if(io.Platform.isAndroid){
doSomething();
}else {
doSomethingElse();
}

因此,这里有一个小实用程序,我用来检测平台和操作系统是否适当地做出反应。

import'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
class Util {
os getPlatform() {
if (kIsWeb) {
return os.Web;
} else if (Platform.isIOS) {
return os.IOS;
} else if (Platform.isAndroid) {
return os.Android;
} else if (Platform.isFuchsia) {
return os.Fuchsia;
} else if (Platform.isLinux) {
return os.Linux;
} else if (Platform.isMacOS) {
return os.MacOS;
} else if (Platform.isWindows) {
return os.Windows;
}
return os.Unknown;
}
bool isWeb() {
return (getPlatform()==os.Web);
}
bool isMobile() {
os platform = getPlatform();
return (platform == os.Android || platform == os.IOS || platform== os.Fuchsia);
}
bool isComputer() {
os platform = getPlatform();
return (platform == os.Linux || platform == os.MacOS || platform== os.Windows);
}
}
enum os { Unknown, Web, Android, Fuchsia, IOS, Linux, MacOS, Windows }
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}else if (Platform.isFuchsia) {
// Fuchsia-specific code
}else if (Platform.isLinux) {
// Linux-specific code
}else if (Platform.isMacOS) {
// MacOS-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}

对于网络

import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}

如果您只需要一个字符串用于日志记录目的,则可以使用Platform.operatingSystem,它将操作系统名称作为小写字符串返回。

import 'dart:io';
import 'package:flutter/foundation.dart';
String _getPlatform() {
if (kIsWeb) return 'web';
return Platform.operatingSystem;
}
**multiple platform you check and run your code according specific platform **
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
bool isAndroid = Theme.of(context).platform == TargetPlatform.android;
bool islinux = Theme.of(context).platform == TargetPlatform.linux;
bool isfuchsia = Theme.of(context).platform == TargetPlatform.fuchsia;
bool isMacOS = Theme.of(context).platform == TargetPlatform.macOS;
bool iswindows = Theme.of(context).platform == TargetPlatform.windows;

您可以将此扩展添加到项目中

import 'package:flutter/material.dart';
extension PlatformExtension on BuildContext {
bool get isMobile =>
Theme.of(this).platform == TargetPlatform.iOS ||
Theme.of(this).platform == TargetPlatform.android;
bool get isDesktop =>
Theme.of(this).platform == TargetPlatform.macOS ||
Theme.of(this).platform == TargetPlatform.windows ||
Theme.of(this).platform == TargetPlatform.linux;
}
extension TargetPlatformExtension on TargetPlatform {
bool get isMobile =>
this == TargetPlatform.iOS || this == TargetPlatform.android;
bool get isDesktop =>
this == TargetPlatform.linux ||
this == TargetPlatform.macOS ||
this == TargetPlatform.windows;
}

现在,您可以使用以下命令访问主机平台。

  1. BuildContext => context.isDesktop

  2. TargetPlatform => defaultTargetPlatform.isDesktop

建议从上下文访问平台。

要检测您的应用程序是否在浏览器上运行,您可以轻松使用基础库中的 kIsWeb 常量。

import 'package:flutter/foundation.dart';
log('$kIsWeb'); // will print true if the app is running on a browser

请注意,如果您在桌面上运行 Web 应用程序,则 isDesktop getter 将返回 true,对于移动平台也是如此。 为了避免这种情况。

import 'package:flutter/foundation.dart';
if (!kIsWeb && defaultTargetPlatform.isDesktop) {
// do stuff for desktop apps only
}
if (!kIsWeb && defaultTargetPlatform.isMobile) {
// do stuff for mobile apps only
}

您可以修改扩展以获得首选实现。

检查 Dart 中的主机平台。

导入"dart:io"作为 IO;

_checkingHostPlatform(){
if(IO.Platform.isAndroid){
//Execute code for android
}else if(IO.Platform.isIOS){
//Execute code for iOS
}else{
//Execute code for other platforms
}
}

如果同时导入"dart:io">"dart:html">,它不了解要导入哪个平台并给出错误。所以导入其中之一。

import 'dart:io';
Platform.isIOS ? CupertinoWidget() : MaterialWidget()

这个自定义创建的类将帮助您检测平台:

import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;
enum Os {
web,
android,
ios,
macOS,
linux,
windows,
fuchsia,
}
class Platform {
const Platform();
/// Platform is Web.
static bool get isWeb => os == Os.web;
/// Platform is Android.
static bool get isAndroid => os == Os.android;
/// Platform is IOS.
static bool get isIOS => os == Os.ios;
/// Platform is Fuchsia.
static bool get isFuchsia => os == Os.fuchsia;
/// Platform is Linux.
static bool get isLinux => os == Os.linux;
/// Platform is MacOS.
static bool get isMacOS => os == Os.macOS;
/// Platform is Windows.
static bool get isWindows => os == Os.windows;
/// Platform is Android or IOS.
static bool get isMobile => isAndroid || isIOS;
/// Platform is Android or IOS or Fuchsia.
static bool get isFullMobile => isMobile || isFuchsia;
/// Platform is Linux or Windows or MacOS.
static bool get isDesktop => isLinux || isWindows || isMacOS;
/// Getting the os name.
static Os get os {
if (kIsWeb) {
return Os.web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return Os.android;
case TargetPlatform.iOS:
return Os.ios;
case TargetPlatform.macOS:
return Os.macOS;
case TargetPlatform.windows:
return Os.windows;
case TargetPlatform.fuchsia:
return Os.fuchsia;
case TargetPlatform.linux:
return Os.linux;
}
}
}

最新更新