对于在iOS和Android上略有不同的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上运行,kIsWeb是一个全局常量,指示应用程序是否被编译为在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.
}

平台文档:https://api.flutter.dev/flutter/dart-io/Platform-class.html kIsWeb文档:https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

其他回答

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;