对于在iOS和Android上略有不同的UI,即在不同的平台上,必须有一种方法来检测应用程序在哪个平台上运行,但我在文档中找不到它。是什么?
当前回答
导入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(),
其他回答
导入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';
如果你使用导入'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 '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
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
}
}
import 'dart:io' as io;
if(io.Platform.isAndroid){
doSomething();
}else {
doSomethingElse();
}