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


当前回答

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

其他回答

您可以将这个扩展文件(platform_ext.dart)添加到项目中并调用任何对象

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

extension Target on Object {
  bool isAndroid() {
    return Platform.isAndroid;
  } 
  bool isIOS() {
    return Platform.isIOS;
  } 
  bool isLinux() {
  return Platform.isLinux;
  } 
  bool isWindows() {
  return Platform.isWindows; 
  }
  bool isMacOS() {
  return Platform.isMacOS; 
  }
  bool isWeb() {
  return kIsWeb; 
  }
  // ···
}

只需导入文件并调用它

   import 'platform_ext.dart';
        ....
        @override
          Widget build(BuildContext context) {
            return isAndroid()? Text("Android"):Text("Not Android");
          }

最“扑”的答案如下:

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

您可以将此扩展添加到项目中

import 'package:flutter/material.dart';

extension PlatformExtension on BuildContext {
  bool get isMobile =>
      Theme.of(this).platform == TargetPlatform.iOS ||
      Theme.of(this).platform == TargetPlatform.android;

  bool get isDesktop =>
      Theme.of(this).platform == TargetPlatform.macOS ||
      Theme.of(this).platform == TargetPlatform.windows ||
      Theme.of(this).platform == TargetPlatform.linux;
}

extension TargetPlatformExtension on TargetPlatform {
  bool get isMobile =>
      this == TargetPlatform.iOS || this == TargetPlatform.android;

  bool get isDesktop =>
      this == TargetPlatform.linux ||
      this == TargetPlatform.macOS ||
      this == TargetPlatform.windows;
}

现在您可以使用。

内置context => context.isDesktop 目标平台=> defaulttarded平台

建议从上下文访问平台。

要检测您的应用程序是否在浏览器上运行,您可以轻松地使用基础库中的kIsWeb常量。

import 'package:flutter/foundation.dart';

  log('$kIsWeb'); // will print true if the app is running on a browser

注意,如果你在桌面上运行web应用程序,isDesktop getter将返回true,对于移动平台也是如此。 为了避免这种情况。

import 'package:flutter/foundation.dart';

  if (!kIsWeb && defaultTargetPlatform.isDesktop) {
    // do stuff for desktop apps only
  }

  if (!kIsWeb && defaultTargetPlatform.isMobile) {
    // do stuff for mobile apps only
  }

您可以修改扩展以获得您喜欢的实现。

您可以使用通用平台包:

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');
import 'dart:io' as io;

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