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


当前回答

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

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;
    }
  }
}

其他回答

你可以这样做

defaultTargetPlatform == TargetPlatform.iOS
          ? kIOSTheme
          : kDefaultTheme,

从导入'包:flutter/foundation.dart';

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

所以这里有一个小工具,我用来检测平台和操作系统的反应适当。

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 }

如果你同时导入"dart:io"和"dart:html",它不知道要导入哪个平台并给出错误。导入其中一个。

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

如果您只是需要一个字符串来记录日志,您可以使用Platform。operatingSystem,返回操作系统名称为小写字符串。

import 'dart:io';
import 'package:flutter/foundation.dart';

String _getPlatform() {
  if (kIsWeb) return 'web';
  return Platform.operatingSystem;
}