对于UI应该略有不同的iOS和Android,必须有检测到您正在运行哪一个的方式,但在文档我找不到它.它是什么?
Wes*_*y92 145
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}
Run Code Online (Sandbox Code Playgroud)
其他选择包括:
Platform.isFuchsia
Platform.isLinux
Platform.isMacOS
Platform.isWindows
kIsWeb // A global constant indicating if the application was compiled to run on the web
Run Code Online (Sandbox Code Playgroud)
文档:https://docs.flutter.io/flutter/dart-io/Platform-class.html
Gav*_*vin 48
感谢Collin,最终的答案是:
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
Run Code Online (Sandbox Code Playgroud)
osa*_*dar 25
很简单,只需要导入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();
}
Run Code Online (Sandbox Code Playgroud)
或以非常简单的方式
Platform.isIOS ? someThing() : anOther(),
Run Code Online (Sandbox Code Playgroud)
Ska*_*nir 19
if (Platform.isAndroid) {
// Android-specific code/UI Component
} else if (Platform.isIOS) {
// iOS-specific code/UI Component
}
Run Code Online (Sandbox Code Playgroud)
不要忘记导入 IO 库。
import 'dart:io';
Run Code Online (Sandbox Code Playgroud)
如果您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
}
Run Code Online (Sandbox Code Playgroud)
如果您正在正确研究平台集成,我建议在 Flutter 站点上使用完整示例:https : //flutter.dev/docs/development/platform-integration/platform-channels
Col*_*son 18
虽然defaultTargetPlatform会起作用,但我建议使用Theme.of(context).targetPlatform.这样可以测试iOS行为(因为defaultTargetPlatform它总是TargetPlatform.android在测试中).它还允许窗口小部件的祖先通过将其包装在Theme窗口小部件中来覆盖其目标平台.
shi*_*kla 12
对于网络和应用程序两者的更简单的方法。试试这个
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());
Run Code Online (Sandbox Code Playgroud)
Oma*_*oud 10
您可以将此扩展文件(platform_ext.dart)添加到项目中并调用任何对象
\nimport 'dart:io';\nimport 'package:flutter/foundation.dart' show kIsWeb;\n\nextension Target on Object {\n bool isAndroid() {\n return Platform.isAndroid;\n } \n bool isIOS() {\n return Platform.isIOS;\n } \n bool isLinux() {\n return Platform.isLinux;\n } \n bool isWindows() {\n return Platform.isWindows; \n }\n bool isMacOS() {\n return Platform.isMacOS; \n }\n bool isWeb() {\n return kIsWeb; \n }\n // \xc2\xb7\xc2\xb7\xc2\xb7\n}\nRun Code Online (Sandbox Code Playgroud)\n只需导入文件并调用它
\n import 'platform_ext.dart';\n ....\n @override\n Widget build(BuildContext context) {\n return isAndroid()? Text("Android"):Text("Not Android");\n }\nRun Code Online (Sandbox Code Playgroud)\n
小智 9
import 'dart:io' show Platform; //at the top
String os = Platform.operatingSystem; //in your code
print(os);
Run Code Online (Sandbox Code Playgroud)
你可以做
defaultTargetPlatform == TargetPlatform.iOS
? kIOSTheme
: kDefaultTheme,
Run Code Online (Sandbox Code Playgroud)
从 import 'package:flutter/foundation.dart';
多数“ Flutter”答案如下:
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
Run Code Online (Sandbox Code Playgroud)
小智 7
import 'dart:io' as io;
if(io.Platform.isAndroid){
doSomething();
}else {
doSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)
因此,这里有一个小实用程序,我用它来检测适当的反应平台和操作系统。
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 }
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用通用平台包:
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');
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
用于网络
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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25003 次 |
| 最近记录: |