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


当前回答

import 'dart:io' as io;

if(io.Platform.isAndroid){
 doSomething();
}else {
 doSomethingElse();
}

其他回答

import 'dart:io' show Platform;  //at the top

String os = Platform.operatingSystem; //in your code
print(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 '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

感谢科林,最终答案是:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
**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;